33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#define MaxSize 10
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q)
{
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
bool QueueEmpty(SqQueue *q)
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
if((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
if(q->front==q->rear)
return false;
q->front-(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
bool StackEmpty(SqStack *&s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if(s->top==MaxSize-1)
return false;
s->top++;
s->data[s->top]=e;
return true;
}
bool Pop(SqStack *&s,ElemType e)
{
if(s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
void Reverse(SqQueue *&qu)
{
ElemType e;
SqStack *st;
InitStack(st);
while(!QueueEmpty(qu))
{
deQueue(qu,e);
Push(st,e);
}
InitQueue(qu);
while(!StackEmpty(st))
{
Pop(st,e);
enQueue(qu,e);
}
DestroyStack(st);
}
main()
{
ElemType e;
SqQueue *q;
InitQueue(q);
enQueue(q,1);
enQueue(q,2);
enQueue(q,3);
enQueue(q,4);
enQueue(q,5);
printf("环形队列倒置前元素顺序:\n");
while (!QueueEmpty(q))
{ deQueue(q,e);
printf("%d ",e);
}
printf("\n");
Reverse(q);
printf("环形队列倒置后元素顺序:\n");
while (!QueueEmpty(q))
{ deQueue(q,e);
printf("%d ",e);
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
//#include <malloc.h>
#define MaxSize 10
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SqStack;
typedef struct
{
ElemType data[MaxSize];
int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q)
{
q = (SqQueue *)malloc(sizeof(SqQueue));
if (!q)
exit(0);
q->front = q->rear = 0;
}
bool QueueEmpty(SqQueue *q)
{
return(q->front == q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
if((q->rear+1)%MaxSize == q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
if(q->front==q->rear)
return false;
q->front = (q->front+1)%MaxSize;
e = q->data[q->front];
return true;
}
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
if (!s)
exit(0);
s->top=-1;
}
bool StackEmpty(SqStack *&s)
{
return(s->top==-1);
}
bool Push(SqStack *&s,ElemType e)
{
if(s->top == MaxSize-1)
return false;
s->top++;
s->data[s->top] = e;
return true;
}
bool Pop(SqStack *&s,ElemType &e)
{
if(s->top==-1)
return false;
e = s->data[s->top];
s->top--;
return true;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
void Reverse(SqQueue *&qu)
{
ElemType e;
SqStack *st;
InitStack(st);
while(!QueueEmpty(qu))
{
deQueue(qu,e);
printf("e = %d\n", e);
Push(st,e);
}
free(qu);
InitQueue(qu);
while(!StackEmpty(st))
{
Pop(st,e);
enQueue(qu,e);
}
DestroyStack(st);
}
int main()
{
ElemType e;
SqQueue *q;
InitQueue(q);
enQueue(q,1);
enQueue(q,2);
enQueue(q,3);
enQueue(q,4);
enQueue(q,5);
#if 0
printf("环形队列倒置前元素顺序:\n");
while (!QueueEmpty(q))
{ deQueue(q,e);
printf("%d ",e);
}
printf("\n");
#endif
Reverse(q);
printf("环形队列倒置后元素顺序:\n");
while (!QueueEmpty(q))
{ deQueue(q,e);
printf("%d ",e);
}
printf("\n");
}
参考一下吧;
问题主要是:
1. 没有意义的表达式
q->front-(q->front+1)%MaxSize;
应该为:
q->front = (q->front+1)%MaxSize;
2. pop第二个参数应该加引用:
bool Pop(SqStack *&s,ElemType &e)
因为你要pop后用到出栈的数据;
3. 重新初始化队列时,要先把上一个队列释放掉,不然会导致内存泄漏;
void Reverse(SqQueue *&qu)
{
ElemType e;
SqStack *st;
InitStack(st);
while(!QueueEmpty(qu))
{
deQueue(qu,e);
printf("e = %d\n", e);
Push(st,e);
}
free(qu); /*释放旧的的队列*/
InitQueue(qu);
while(!StackEmpty(st))
{
Pop(st,e);
enQueue(qu,e);
}
DestroyStack(st);
}
4.main函数中的出队列操作,即在倒置之前,还没倒置就将队列数据完全出队列了,那么倒置再出队列数据从哪来?
因此倒置前,把出队列操作注释掉;
其他问题,建议对比代码看一下;