2016-08-27 44 views
0

我試圖使用以下結構使用鏈接列表實現隊列。但在排隊功能,就在評論下面,我得到了上面提到的錯誤。這個錯誤已經發生,而同樣的在這個鏈接http://quiz.geeksforgeeks.org/queue-set-2-linked-list-implementation/給出的代碼中使用。有一個我用來運行代碼的評論。但我不明白爲什麼我得到這個錯誤。請幫忙 !'結構隊列'沒有名爲'next'的成員。

#include<stdio.h> 
    #include<stdlib.h> 

    struct node 
    { 
     int data; 
     struct node *next; 
    }; 

    struct queue 
    { 
     struct queue *front; 
     struct queue *rear; 
    }; 

    struct queue* createQueue(); 
    void enqueue(struct queue *q,int info); 
    int dequeue(struct queue *q); 
    void display(struct queue *q); 
    int isEmpty(struct queue *q); 
    void menu(); 

    struct queue* createQueue() 
    { 
     struct queue *new_queue; 
     new_queue = (struct queue*) malloc(sizeof(struct queue)); 
     if(new_queue != NULL) 
     { 
      return ; 
     } 
     new_queue->front = new_queue->rear = NULL; 
     return new_queue; 
    } 
    int isEmpty(struct queue *q) 
    { 
     if(q->front == NULL) 
      return 1; 
    } 

    void enqueue(struct queue *q,int info) 
    { 
     struct node *temp,*temp1; 
     temp = (struct node*) malloc(sizeof(struct node)); 
     if(temp == NULL) 
      return ; 
     temp->data = info; 
     temp->next = NULL; 
     if(q->front == NULL && q->rear == NULL) 
     { 
      q->front = temp; 
      q->rear = temp; 
     } 
     if(q->rear != NULL) 
     { 
      //temp1 = q->rear; 
      q->rear->next = temp; 
      q->rear = temp; 
     } 
    } 

    int dequeue(struct queue *q) 
    { 
     if(isEmpty(q) == 1) 
     { 
      printf("Empty list!"); 
     } 
     struct node *temp = q->front; 
     q->front = temp->next; 
     int data = temp->data; 
     free(temp); 
     return (data); 
    } 

    void display(struct queue *q) 
    { 
     struct node *start,*end = NULL; 
     start = q->front; 
     end = q->rear; 
     if(isEmpty(q) == 1) 
     { 
      printf("Empty List!"); 
     } 
     while(start != NULL) 
     { 
      printf("%d",start->data); 
      start = start->next; 
     } 
    } 

    void menu() 
    { 
     int choice = 0; 
     int input = 0; 
     struct queue *q; 
     while(1) 
     { 
      printf("\t\tMain Menu\n"); 
      printf("\t0.Create Queue\n"); 
      printf("\t1.Enqueue\n"); 
      printf("\t2.Dequeue\n"); 
      printf("\t3.Display\n"); 
      printf("\t4.Exit\n"); 
      printf("\tEnter the desired choice:"); 
      scanf("%d",&choice); 
      switch(choice) 
      { 
       case 0: 
        q = createQueue(); 
        break; 
       case 1: 
        printf("\tEnter the data:"); 
        scanf("%d",&input); 
        enqueue(q,input); 
        break; 
       case 2: 
        printf("%d",dequeue(q)); 
        break; 
       case 3: 
        display(q); 
        break; 
       case 4: 
        exit(0); 
       default: 
        printf("\tThe value is invalid!"); 
      } 
     } 

    } 

    int main() 
    { 
     menu(); 
     return 0; 
    } 

回答

3

你寫

q->rear->next = temp; 

rearqueue結構,而不是一個node結構。

你寫這樣的:

struct queue 
{ 
    struct queue *front; 
    struct queue *rear; 
}; 

但是從條款你用我認爲應該是這樣的:

struct queue 
{ 
    struct node *front; 
    struct node *rear; 
}; 

(當然,誤差線將成爲確定的,但你可能有其他周圍的問題:))

+0

謝謝你的解決方案。雖然這是一個愚蠢的錯誤,很尷尬!但是,你能告訴我你剛纔提到的其他錯誤! – nile

2

答案是在你面前。

您正在使用前面和後面定義隊列結構,並且其中沒有名爲next的成員。

你可能已經使用隊列類型的指針與成員next這是struct node的成員,而不是struct queue

這裏的錯誤

q->rear->next = temp; 
1

這是怎麼了,我會重新編寫代碼(見注波紋管)

#include<stdio.h> 
#include<stdlib.h> 
#define EMPTY 1 
// No need for struct node at all! 
struct queue 
{ 
    int data; 
    struct queue *front; 
    struct queue *rear; 
}; 

struct queue* createQueue(); 
void enqueue(struct queue *q,int info); 
int dequeue(struct queue *q); 
void display(struct queue *q); 
int isEmpty(struct queue *q); 
void menu(); 

struct queue* createQueue() 
{ 
    struct queue *new_queue; 
    new_queue = (struct queue *) malloc(sizeof(struct queue)); 
    if(new_queue != NULL) 
    { 
     return NULL; 
    } 
    new_queue->front = new_queue->rear = NULL; 
    return new_queue; 
} 
int isEmpty(struct queue *q) 
{ 
    if(q == NULL|| q->front == NULL) //`q` might be NULL also, so that q->front won't cause segmentation fault. 
     return EMPTY; //check the #define above. 
} 

void enqueue(struct queue *q,int info) 
{ 
    if (q == NULL) return; /*ALWAYS check for null pointer before dereference*/ 
    struct queue *temp,*temp1; // changed node to queue 
    temp = (struct queue*) malloc(sizeof(*temp)); 
    if(temp == NULL) return ; 
    temp->data = info; 
    temp->front = NULL; //changed `next' to `front', check if this is OK. 
    if(q->front == NULL && q->rear == NULL) 
    { 
     q->front = temp; 
     q->rear = temp; 
    } 
    if(q->rear != NULL) 
    {    
     q->rear->front = temp; //changed `next' to `front', check if this is OK. 
     q->rear = temp; 
    } 
} 

int dequeue(struct queue *q) 
{ 
    if(isEmpty(q) == EMPTY) 
    { 
     printf("Empty list!"); 
    } 
    struct queue *temp = q->front; //changed node to queue 
    q->front = temp->front;//changed `next' to `front', check if this is OK. 
    int data = temp->data; 
    free(temp); 
    return (data); 
} 

void display(struct queue *q) 
{ 
    if (!q) return; 
    struct queue *start,*end = NULL; //changed node to queue 
    start = q->front; 
    end = q->rear; 
    if(isEmpty(q) == 1) 
    { 
     printf("Empty List!"); 
    } 
    while(start != NULL) 
    { 
     printf("%d",start->data); 
     start = start->front;//changed `next' to `front', check if this is OK. 
    } 
} 

void menu() 
{ 
    int choice = 0; 
    int input = 0; 
    struct queue *q; 
    while(1) 
    { 
     printf("\t\tMain Menu\n"); 
     printf("\t0.Create Queue\n"); 
     printf("\t1.Enqueue\n"); 
     printf("\t2.Dequeue\n"); 
     printf("\t3.Display\n"); 
     printf("\t4.Exit\n"); 
     printf("\tEnter the desired choice:"); 
     scanf("%d",&choice); 
     switch(choice) 
     { 
      case 0: 
       q = createQueue(); 
       break; 
      case 1: 
       printf("\tEnter the data:"); 
       scanf("%d",&input); 
       enqueue(q,input); 
       break; 
      case 2: 
       printf("%d",dequeue(q)); 
       break; 
      case 3: 
       display(q); 
       break; 
      case 4: 
       exit(0); 
      default: 
       printf("\tThe value is invalid!"); 
     } 
    } 

} 

int main() 
{ 
    menu(); 
    return 0; 
} 

NOTES

  • 加場data在結構queue
  • 你不檢查,如果是NULL或不被取消引用q!這可能會導致您segmentation fault
  • 代碼中根本不需要struct node!想想吧
+0

不錯的建議。我會爲諮詢方法(檢查空白,顯示)添加一些'const queue *'來告訴用戶對象沒有被通話修改。 –