我的程序有问题。我创建了一个链接列表队列,当我用我的delQueue
函数清除队列时,我的队列消失了,我再也不能推动任何东西了。链表C编程队列
我该如何解决这个问题?我的推送功能正常工作,除非我从队列中删除所有内容。
这里是我的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int count = 0;
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *var=rear;
while(var!=NULL)
{
struct Node* buf=var->next;
free(var);
count = count + 1;
}
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements in queue are: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty\n");
}
当你'推'你正在更新'前'。当你推动时不应该更新'后部'?队列先进先出...所以当你推动时,'后部'指针应该被更新。 – Bill 2013-05-04 02:16:18
非常感谢大家,您一直非常乐于助人。我非常感谢我在这里收到的所有帮助,并且我肯定了解了很多关于队列的知识。比你再次。上帝保佑你们所有人 – user2133160 2013-05-04 02:43:47
你应该接受你认为最好回答你的问题的答案;) – Bill 2013-05-04 02:51:58