为什么打印队列元素没有显示,跪求大佬帮助
#include<stdio.h>#include<stdlib.h>
#define ok 1
#define error 0
#define overlow -1
typedef char elemtype;
typedef struct Qnode
{
elemtype data;
struct Qnode* next;
}qnode;
typedef struct
{
qnode *front;
qnode *rear;
}linkqnode;
int Initqnode(linkqnode* q)
{
q->front = q->rear = (qnode*)malloc(sizeof(qnode));
if (!q->front)
exit(overlow);
printf("队列初始化成功..\n");
q->front->next = NULL;
return ok;
}
int EnQueue(linkqnode* q, elemtype e)
{
if (q->rear == q->front)
return error;
qnode *p;
p=(qnode*)malloc(sizeof(qnode));
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
return ok;
}
int DeQueue(linkqnode* q, elemtype* e)
{
qnode *p;
p=(qnode*)malloc(sizeof(qnode));
if( q->front == q->rear )
return error;
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if( q->rear == p )
{
q->rear = q->front;
}
free(p);
}
int DestroyQueue(linkqnode* q)
{
while (q->front)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
return ok;
}
int main()
{
linkqnode Q;
elemtype e;
Initqnode(&Q);
printf("请输入一窜字符串并以'#'结束:\n");
scanf("%c", &e);
while (e != '#')
{
EnQueue(&Q, e);
scanf("%c", &e);
}
printf("打印队列中的元素:");
while( Q.front != Q.rear )
{
DeQueue( &Q, &e );
printf("%c", e);
}
DeQueue( &Q, &e );
return 0;
}
啊啊啊啊,跪求大佬帮助,为什么打印队列元素没有显示。 你的入队函数有问题。帮你标注出来了
#include<stdio.h>
#include<stdlib.h>
#define ok 1
#define error 0
#define overlow -1
typedef char elemtype;
typedef struct Qnode
{
elemtype data;
struct Qnode* next;
}qnode;
typedef struct
{
qnode *front;
qnode *rear;
}linkqnode;
int Initqnode(linkqnode* q)
{
q->front = q->rear = (qnode*)malloc(sizeof(qnode));
if (!q->front)
exit(overlow);
printf("队列初始化成功..\n");
q->front->next = NULL;
return ok;
}
int EnQueue(linkqnode* q, elemtype e)
{
//if (q->rear == q->front)//队列刚刚初始化的时候,q->rear和q->front本来就是一样的,你这样写,队列是无法插进去任何元素的。所以输出的时候什么也没有。
// return error;
qnode *p;
p=(qnode*)malloc(sizeof(qnode));
if(!p)
return error;
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
return ok;
}
int DeQueue(linkqnode* q, elemtype* e)
{
qnode *p;
p=(qnode*)malloc(sizeof(qnode));
if( q->front == q->rear )
return error;
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if( q->rear == p )
{
q->rear = q->front;
}
free(p);
return 0;
}
int DestroyQueue(linkqnode* q)
{
while (q->front)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
return ok;
}
int main()
{
linkqnode Q;
elemtype e;
Initqnode(&Q);
printf("请输入一窜字符串并以'#'结束:\n");
scanf("%c", &e);
while (e != '#')
{
EnQueue(&Q, e);
scanf("%c", &e);
}
printf("打印队列中的元素:");
while( Q.front != Q.rear )
{
DeQueue( &Q, &e );
printf("%c", e);
}
DeQueue( &Q, &e );
return 0;
} sunrise085 发表于 2020-4-26 09:12
你的入队函数有问题。帮你标注出来了
大哥,非常感谢啊,我一直以为是我的出队函数有问题,啊,非常感谢
页:
[1]