【问题标题】:'struct queue' has no member named 'next'.“结构队列”没有名为“下一个”的成员。
【发布时间】:2016-08-27 07:12:26
【问题描述】:

我尝试使用以下结构使用链表来实现队列。但是在入队函数中,就在评论下方,我得到了上述错误。发生此错误时,此链接http://quiz.geeksforgeeks.org/queue-set-2-linked-list-implementation/ 中给出的代码中使用了相同的错误。有一条我用来运行代码的注释。但我不明白为什么我会收到这个错误。请帮忙!

    #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;
    }

【问题讨论】:

    标签: c data-structures queue


    【解决方案1】:

    你在写

    q->rear->next = temp;
    

    rearqueue 结构,而不是 node 结构。

    这是你写的:

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

    但从您使用的术语来看,我认为应该是这样的:

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

    (当然错误行会变好,但你可能还有其他问题:))

    【讨论】:

    • 感谢您的解决方案。虽然这是一个愚蠢的错误,而且非常尴尬!但是你能告诉我你刚才提到的其他错误吗!
    【解决方案2】:

    答案就在你面前。

    您正在定义带有前后的队列结构,并且其中没有名为next 的成员。

    你可能使用了队列类型的指针,成员nextstruct node的成员,而不是struct queue的成员

    这是错误

    q->rear->next = temp;
    

    【讨论】:

      【解决方案3】:

      这就是我将如何重写你的代码(见下面的注释)

      #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;
      }
      

      注意事项

      • 在结构queue 中添加了字段data
      • 您在取消引用q 时没有检查它是否为NULL!这可能会导致您segmentation fault
      • 代码中根本不需要struct node!考虑一下

      【讨论】:

      • 不错的提议。我会添加一些const queue * 用于咨询方法(检查空,显示),以告诉用户该对象不是通过调用修改的。
      猜你喜欢
      • 2013-05-23
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多