#include
#include
#include
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct
{
Node *front;
Node *rear;
}lqueuetp;
void InitQueue(lqueuetp *lq)
{
//设置一个空的链队列lq
lq->front = (Node*)malloc(sizeof(Node));
lq->front->next = NULL;
lq->front = lq->rear; //链队列空的判断条件
}
void QueueEmpty(lqueuetp *lq)
{
if(lq->front = lq->rear)
printf("链队列为空");
else
printf("链队列不为空");
}
int Head(lqueuetp *lq) //读链队列队头元素
{
//若链队列lq不为空,则返回队头元素值,否则返回0
if(lq->front == lq->rear)
return 0;
else
return(lq->front->next->data);
}
void EnQueue(lqueuetp *lq, int x)
{
//在链队列lq中,插入x为新的队尾元素
Node *p;
p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = NULL;
lq->rear->next = p; //尾插
lq->rear = p;
}
int DelQueue(lqueuetp *lq)
{
//若链队列lq不为空,则删去队头元素并返回元素值,否则返回0
int x;
Node *q;
q = (Node*)malloc(sizeof(Node));
if(lq->front == lq->rear)
return 0;
else
{
q = lq->front->next;
lq->front->next = q->next;
if(q->next == NULL)
lq->rear = lq->front; //当链队列中仅有一个结点时,出队时还需修改尾指针
x = q->data;
free(q);
return x;
}
}
int main()
{
int x,i,n;
printf("链队列长度:");
scanf("%d",&n);
lqueuetp *lq;
lq = (lqueuetp*)malloc(sizeof(lqueuetp));
InitQueue(lq);
printf("入队列:\n");
for(i = 1; i <= n;i++)
{
scanf("%d",&x);
EnQueue(lq,x);
}
QueueEmpty(lq);
printf("读取队列对头元素:%d\n",Head(lq));
printf("出队列:%d\n",DelQueue(lq));
return 0;
}
这个代码哪里出现问题了?为什么入队列时不成功?
修改如下,改动处见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct
{
Node *front;//队头指针
Node *rear; //队尾指针
}lqueuetp;
void InitQueue(lqueuetp *lq)
{
//设置一个空的链队列lq
//lq->front = (Node*)malloc(sizeof(Node)); 修改
//lq->front->next = NULL; 修改
lq->front = lq->rear = NULL; //链队列空的判断条件
}
void QueueEmpty(lqueuetp *lq)
{
if(!lq->front) //if(lq->front = lq->rear) 修改
printf("链队列为空\n");
else
printf("链队列不为空\n");
}
int Head(lqueuetp *lq) //读链队列队头元素
{
//若链队列lq不为空,则返回队头元素值,否则返回0
if(!lq->front) //if(lq->front == lq->rear) 修改
return 0;
else
return lq->front->data ; //(lq->front->next->data);修改
}
void EnQueue(lqueuetp *lq, int x)
{
//在链队列lq中,插入x为新的队尾元素
Node *p;
p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = NULL;
if (!lq->front) //修改
lq->front = p; //修改
else //修改
lq->rear->next = p;//尾插
lq->rear = p; //修改
}
int DelQueue(lqueuetp *lq)
{
//若链队列lq不为空,则删去队头元素并返回元素值,否则返回0
int x;
//Node *q; //修改
//q = (Node*)malloc(sizeof(Node));//修改
if(!lq->front) //if(lq->front == lq->rear)//修改
return 0;
//else //修改
//{ //修改
x = lq->front->data;
//q = lq->front->next; //修改
//lq->front->next = q->next; //修改
if(lq->front->next == NULL){ //只剩一个结点
free(lq->front);
lq->front = lq->rear = NULL;
}
else{ //多个结点
Node *q = lq->front->next;
free(lq->front);
lq->front = q;
}
//lq->rear = lq->front; //当链队列中仅有一个结点时,出队时还需修改尾指针
//x = q->data;
//free(q);
return x;
//}
}
int main()
{
int x,i,n;
printf("链队列长度:");
scanf("%d",&n);
lqueuetp *lq;
lq = (lqueuetp*)malloc(sizeof(lqueuetp));
InitQueue(lq);
printf("入队列:\n");
for(i = 1; i <= n;i++)
{
scanf("%d",&x);
EnQueue(lq,x);
}
QueueEmpty(lq);
printf("读取队列对头元素:%d\n",Head(lq));
x = DelQueue(lq); //修改
printf(x ? "出队列:%d\n" : "队空\n",x); //修改
x = DelQueue(lq); //修改
printf(x ? "出队列:%d\n" : "队空\n",x); //修改
return 0;
}
个人修改,仅供参考,若有问题多谢指出。
注意出队列函数调用一次出队一个,多次调用多次依次出队。
#include <stdio.h>
#include <stdlib.h>
/*#include <malloc.h>该行不需要*/
typedef struct node
{
int data;
struct node *next;
} Node;
typedef struct
{
Node *front;
Node *rear;
} lqueuetp;
void InitQueue(lqueuetp * lq)
{
lq->front = NULL;
lq->rear = NULL;
}
int QueueEmpty(lqueuetp * lq)
{
if (lq->front == NULL)
{
printf("链队列为空");
return 0;
}
else
printf("链队列不为空");
return 1;
}
int Head(lqueuetp * lq)
// 读链队列队头元素
{
// 若链队列lq不为空,则返回队头元素值,否则返回0
if (lq->front == NULL)
return 0;
else
return (lq->front->data);
}
void EnQueue(lqueuetp * lq, int x)
{
// 在链队列lq中,插入x为新的队尾元素
Node *p;
p = (Node *) malloc(sizeof(Node));
p->data = x;
p->next = NULL;
if (lq->front == NULL)
// p 是队列的第一个节点
{
lq->front = p;
lq->rear = p;
}
else
// 队列是一个非空的队列
{
lq->rear->next = p;
lq->rear = p;
}
}
int DelQueue(lqueuetp * lq)
{
// 若链队列lq不为空,则删去队头元素并返回元素值,否则返回0
int x;
Node *q = NULL;
if (lq->front == NULL)
return 0;
q = lq->front;
x = q->data;
// 当链队列中仅有一个结点时
if (lq->front->next == NULL)
{
lq->front = NULL;
lq->rear = NULL;
}
else
{
lq->front = q->next;
}
free(q);
return x;
}
int main()
{
int x, i, n;
printf("链队列长度:");
scanf("%d", &n);
lqueuetp *lq;
InitQueue(lq);
printf("入队列:\n");
for (i = 1; i <= n; i++)
{
scanf("%d", &x);
EnQueue(lq, x);
}
// 判断队列是否为空
(void)QueueEmpty(lq);
puts(" ");
printf("读取队列对头元素:%d\n", Head(lq));
printf("出队列:\n");
for (i = 1; i <= n; i++)
{
printf("%d ", DelQueue(lq));
}
return 0;
}