)
{
int n;
scanf("%d", &n);
while(n)
{
Push(S, n%8);
n = n/8;
}
while(!StackEmpty(S))
{
Pop(S, &e);
printf("%d", e);
}
}
2、用单链表结构实现一个队列,包含了队列的基本操作函数void EnQueue(LinkQueue *Q,int e); //入队操作 void DeQueue(LinkQueue *Q,int *e); //出队操作 请将函数EnQueue和DeQueue补充完整,使其能够正常实现队列的入队和出队操作。
void EnQueue(LinkQueue *Q, int e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if(!p) //内存分配失败
exit(0);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
}
void DeQueue(LinkQueue *Q, int *e)
{
QueuePtr p;
if(Q->front == Q->rear) //队列为空
{
return;
}
p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if(Q->rear == p) //删除最后一个元素
{
Q->rear = Q->front;
}
free(p);
}
3、给定一个字符串,使用栈来判断该字符串是否是对称的,即回文字符串。请根据下面的伪代码实现相应的函数isPalindrome。
bool isPalindrome(string s) {
stack<char> st;
int n = s.size();
for(int i=0; i<n/2; i++)
{
st.push(s[i]);
}
int j = (n+1)/2;
while(j<n)
{
if(s[j] != st.top())
{
return false;
}
st.pop();
j++;
}
return true;
}
以上是本次作业中涉及到的数据结构问题,包括栈的转换、队列的操作以及判断回文字符串。希望同学们认真完成,加深对数据结构的理解和应用。