链表C程序设计中的队列

链表C程序设计中的队列,c,linked-list,queue,C,Linked List,Queue,我的程序有问题。我已经创建了一个链表队列,当我用delQueue函数清除队列时,我的队列消失了,我不能再推任何东西了 我怎样才能解决这个问题?我的推送功能可以正常工作,除非我从队列中删除所有内容 这是我的密码: #include <stdio.h> #include <time.h> #include <stdlib.h> int count = 0; struct Node { int Data; struct Node* next; }

我的程序有问题。我已经创建了一个链表队列,当我用
delQueue
函数清除队列时,我的队列消失了,我不能再推任何东西了

我怎样才能解决这个问题?我的推送功能可以正常工作,除非我从队列中删除所有内容

这是我的密码:

#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");
}
#包括
#包括
#包括
整数计数=0;
结构体类型
{
int数据;
结构节点*下一步;
}*后,*前;
void delQueue()
{
结构节点*var=后部;
while(var!=NULL)
{
结构节点*buf=var->next;
自由(var);
计数=计数+1;
}
}
无效推送(int值)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=值;
if(front==NULL)
{
前=温度;
前->下一步=空;
后=前;
}
其他的
{
前->下一步=温度;
前=温度;
前->下一步=空;
}
}
无效显示()
{
结构节点*var=后部;
如果(var!=NULL)
{
printf(“\n队列中的元素为:”);
while(var!=NULL)
{
printf(“\t%d”,变量->数据);
var=var->next;
}
printf(“\n”);
} 
其他的
printf(“\n队列为空\n”);
}
释放“var”后(再次循环时),您将看到它。您的意思是在delQueue()中的循环中指定“var=buf”吗

另外,不要忘记在push()例程中检查malloc()返回值是否为NULL。即使这只是一个小的学习计划,你也应该学会经常检查

void delQueue()
{
    while(rear != NULL) {
        struct Node* var=rear->next;
        free(rear);
        count = count + 1;
        rear = var;         /* update rear */
    }
    front = NULL; /* clear the front */
}

由于它是一个队列,我更喜欢从前面删除元素,而不是从后面删除元素。

您必须在delQueue()的末尾添加以下行


后=前=零

当您按下
按钮时,您正在更新
front
。按下按钮时,是否应更新后
?队列是先进先出的…所以当你们推的时候,
指针应该会被更新。非常感谢你们一直以来的帮助。我真的很感谢在这里得到的所有帮助,我确实学到了很多关于排队的知识。比你多一次。上帝保佑你们,你们应该接受你们认为最能回答你们问题的答案;)我的队列现在存在,但我无法添加任何内容。它不会在队列中推送任何内容,但队列已存在且为空。请参阅更新,可能是因为您没有重置
前端
。由于它是一个队列,我更喜欢从前端删除,如我的回答所示。否则你就失去了排队的意义,FIFOit工作得很完美,但我似乎记不清我删除了多少项。我要把它放在哪里??
 int delQueue()
    {
   int count = 0;
    while ( front != NULL )
    {
    struct Node * temp = front;
    front = front -> next;
    free (temp);
    count++;
    }
    rear= NULL;

    return count;
    }