2013-05-04 52 views
0

我無法從我的隊列中刪除所有項目。這是我想要做的,這裏是我所有的代碼。請我看不到我做錯了什麼,也想存儲我的隊列中有多少物品的數量。刪除我的隊列中的所有項目

#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) 
    { 
     free(var); 
    var = var->next; 
     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"); 
} 

回答

2

問題僅僅是在這裏:

free(var); 
var= var->next; 

你可以這樣做:

struct Node* buf=var->next; 
free(var); 
var=buf; 
+0

所以會說去while循環中: 節點* BUF = VAR->未來; free(var); var = buf; – user2133160 2013-05-04 01:02:42

+0

我不理解這三行代碼在我的程序中的位置。 – user2133160 2013-05-04 01:05:36

+0

您是否瞭解上述有問題的*兩個*行在「您的」程序中的位置? – WhozCraig 2013-05-04 01:06:45

相關問題