C QueueLinkedList字符串变量不工作 #包括 结构客户 { int phoneNum; 字符*名称; 结构客户*nxt; }; 结构客户*initialNode(int i,char*n); int isEmpty(结构客户*头部,结构客户*尾部); void队列(int phNum1,char*cName1,struct customer*tail); 无效出列(结构客户*头,结构客户*尾); 无效显示(结构客户*头部,结构客户*尾部); int main() { int phNum; char*cName; 智力选择; 结构客户*负责人; 结构客户*tail; head=initialNode(0,NULL); tail=initialNode(0,NULL); 头部->nxt=尾部; 尾部->nxt=头部; while(选项!=4) { printf(“\n输入您的选项:”); printf(“\n1.让新客户排队。\n2.让客户排队。\n3.在队列中显示客户。\n4.退出。\n选项:”); scanf(“%d”,选择(&C); 开关(选择) { 案例1: { printf(“\n输入客户电话号码:”); scanf(“%d”和&phNum); printf(“\n输入客户名称:”); scanf(“%s”,cName); 排队(phNum、cName、tail); 打破 } 案例2: { 出列(头、尾); 打破 } 案例3: { 显示器(头部、尾部); 打破 } 违约: { 如果(选项4) printf(“\n无效选项!\n”); 打破 } } } } 结构客户*初始节点(int i,char*n) { struct customer*newCustomer=malloc(sizeof(struct customer)); newCustomer->phoneNum=i; newCustomer->name=n; newCustomer->nxt=NULL; 返回新客户; } int isEmpty(结构客户*头部,结构客户*尾部) { 如果(头部->nxt==尾部) 返回1; 其他的 返回0;//不能为已满,因为它不是数组,没有大小限制 } void队列(int phNum1,char*cName1,struct customer*tail) { 结构客户*nC=malloc(sizeof(结构客户)); nC=初始节点(phNum1,cName1); nC->nxt=尾部; 尾部->nxt->nxt=nC; 尾部->nxt=nC; printf(“\n成功将客户排队!\n”); } 无效出列(结构客户*头,结构客户*尾) { 结构客户*removedCustomer=malloc(sizeof(结构客户)); if(isEmpty(head,tail)==1) { printf(“\n队列为空!\n”); } 其他的 { removedCustomer=头部->nxt; printf(“\n第一个客户被删除。\n”); head->nxt=head->nxt->nxt; } } 无效显示(结构客户*头部,结构客户*尾部) { 结构客户*tempH=malloc(sizeof(结构客户)); tempH=头部; tempH=tempH->nxt; if(isEmpty(head,tail)==1) { printf(“\n队列为空!\n”); } 其他的 { printf(“\n==当前客户列表==”; while(tempH!=尾部) { printf(“\n\t客户电话号码:%d”,tempH->phoneNum); printf(“\n\t客户名称:%s\n”,tempH->name); tempH=tempH->nxt; } } }

C QueueLinkedList字符串变量不工作 #包括 结构客户 { int phoneNum; 字符*名称; 结构客户*nxt; }; 结构客户*initialNode(int i,char*n); int isEmpty(结构客户*头部,结构客户*尾部); void队列(int phNum1,char*cName1,struct customer*tail); 无效出列(结构客户*头,结构客户*尾); 无效显示(结构客户*头部,结构客户*尾部); int main() { int phNum; char*cName; 智力选择; 结构客户*负责人; 结构客户*tail; head=initialNode(0,NULL); tail=initialNode(0,NULL); 头部->nxt=尾部; 尾部->nxt=头部; while(选项!=4) { printf(“\n输入您的选项:”); printf(“\n1.让新客户排队。\n2.让客户排队。\n3.在队列中显示客户。\n4.退出。\n选项:”); scanf(“%d”,选择(&C); 开关(选择) { 案例1: { printf(“\n输入客户电话号码:”); scanf(“%d”和&phNum); printf(“\n输入客户名称:”); scanf(“%s”,cName); 排队(phNum、cName、tail); 打破 } 案例2: { 出列(头、尾); 打破 } 案例3: { 显示器(头部、尾部); 打破 } 违约: { 如果(选项4) printf(“\n无效选项!\n”); 打破 } } } } 结构客户*初始节点(int i,char*n) { struct customer*newCustomer=malloc(sizeof(struct customer)); newCustomer->phoneNum=i; newCustomer->name=n; newCustomer->nxt=NULL; 返回新客户; } int isEmpty(结构客户*头部,结构客户*尾部) { 如果(头部->nxt==尾部) 返回1; 其他的 返回0;//不能为已满,因为它不是数组,没有大小限制 } void队列(int phNum1,char*cName1,struct customer*tail) { 结构客户*nC=malloc(sizeof(结构客户)); nC=初始节点(phNum1,cName1); nC->nxt=尾部; 尾部->nxt->nxt=nC; 尾部->nxt=nC; printf(“\n成功将客户排队!\n”); } 无效出列(结构客户*头,结构客户*尾) { 结构客户*removedCustomer=malloc(sizeof(结构客户)); if(isEmpty(head,tail)==1) { printf(“\n队列为空!\n”); } 其他的 { removedCustomer=头部->nxt; printf(“\n第一个客户被删除。\n”); head->nxt=head->nxt->nxt; } } 无效显示(结构客户*头部,结构客户*尾部) { 结构客户*tempH=malloc(sizeof(结构客户)); tempH=头部; tempH=tempH->nxt; if(isEmpty(head,tail)==1) { printf(“\n队列为空!\n”); } 其他的 { printf(“\n==当前客户列表==”; while(tempH!=尾部) { printf(“\n\t客户电话号码:%d”,tempH->phoneNum); printf(“\n\t客户名称:%s\n”,tempH->name); tempH=tempH->nxt; } } },c,struct,linked-list,C,Struct,Linked List,该代码假设将用户输入其电话号码和姓名的客户列表列为一个队列,代码运行良好,但当我输入新客户时,之前的客户姓名都会以某种方式成为最新客户的姓名。。 我如何解决这个问题?我是否声明了一个2D字符串数组来收集不同的客户名称集?我看到了下面的代码问题 head和tail应初始化为NULL。这表示一个空列表 在initialnode函数中,您直接将扫描的指针指定为newCustomer->name=n。这是错误的。由于您已将结构声明为具有字符指针,因此还需要分别为每个字符串分配空间 入队和出队函数可以更改

该代码假设将用户输入其电话号码和姓名的客户列表列为一个队列,代码运行良好,但当我输入新客户时,之前的客户姓名都会以某种方式成为最新客户的姓名。。
我如何解决这个问题?我是否声明了一个2D字符串数组来收集不同的客户名称集?

我看到了下面的代码问题

  • head
    tail
    应初始化为
    NULL
    。这表示一个空列表
  • initialnode
    函数中,您直接将扫描的指针指定为
    newCustomer->name=n
    。这是错误的。由于您已将结构声明为具有字符指针,因此还需要分别为每个字符串分配空间
  • 入队和出队函数可以更改主函数中
    指针的值。您可以更改函数声明以为此传递双指针
  • 您还需要在出列时释放结构。这在小程序中是绝对不必要的,但无论如何,这是一个很好的实践,可以避免较大程序中的内存泄漏
  • 在显示功能中,需要删除不必要的
    malloc
    。此函数不需要分配内存
  • main中的变量
    choice
    未初始化
  • 请参阅下面的代码

    #include<stdlib.h>
    
    struct customer
    {
      int phoneNum;
      char *name;
      struct customer *nxt;
    };
    
    struct customer *initialNode(int i, char *n);
    int isEmpty(struct customer *head, struct customer *tail);
    void enqueue(int phNum1, char *cName1, struct customer *tail);
    void dequeue(struct customer *head, struct customer *tail);
    void display(struct customer *head, struct customer *tail);
    
    int main()
    {
      int phNum;
      char *cName;
      int choice;
    
      struct customer *head;
      struct customer *tail;
      head = initialNode(0, NULL);
      tail = initialNode(0, NULL);
      head->nxt = tail;
      tail->nxt = head;
    
      while (choice !=4)
      {
        printf("\nEnter your option:");
        printf("\n1. Enqueue new customer. \n2. Dequeue customer. \n3. Display customer in queue. \n4. Exit. \nOption: ");
        scanf("%d", &choice);
    
        switch(choice)
        {
          case 1: 
          {
            printf("\nEnter customer phone number: ");
            scanf("%d", &phNum);
            printf("\nEnter customer name: ");
            scanf("%s", cName);
            enqueue(phNum, cName, tail);
            break;
          }
          case 2:
          { 
            dequeue(head, tail);
            break;
          }
          case 3: 
          {
            display(head, tail);
            break;
          }
          default: 
          {
            if (choice < 1 || choice > 4)
              printf("\nInvalid option!\n");
            break;
          }
        }
      }
    
    }
    
    struct customer *initialNode(int i, char *n)
    {
      struct customer *newCustomer = malloc(sizeof(struct customer));
      newCustomer->phoneNum = i;
      newCustomer->name = n;
      newCustomer->nxt = NULL;
      return newCustomer; 
    }
    
    int isEmpty ( struct customer *head, struct customer *tail)
    {
      if (head->nxt == tail)
        return 1;
      else
        return 0;   //cannot is full because it is not array, have no size limitation
    }
    
    void enqueue(int phNum1, char *cName1, struct customer *tail)
    {
      struct customer *nC = malloc(sizeof(struct customer));
      nC = initialNode(phNum1, cName1);
      nC->nxt = tail;
    
      tail->nxt->nxt = nC;
      tail->nxt = nC;
    
      printf("\nSuccessfully enqueue a customer!\n");
    }
    
    void dequeue(struct customer *head, struct customer *tail)
    {
      struct customer *removedCustomer = malloc(sizeof(struct customer));
      if (isEmpty(head, tail) == 1)
      {
        printf("\nThe queue is empty!\n");
      }
      else
      {
        removedCustomer = head->nxt;
        printf("\nFirst customer is removed.\n");
    
        head->nxt = head->nxt->nxt;
      }
    }
    
    void display(struct customer *head, struct customer *tail)
    {
      struct customer *tempH = malloc(sizeof(struct customer));
      tempH = head;
      tempH = tempH->nxt;
      if (isEmpty(head, tail) == 1)
      {
        printf("\nThe queue is empty!\n");
      }
      else
      {
        printf("\n===Current customer list===");
        while (tempH != tail)
        {
          printf("\n\tCustomer phone number : %d", tempH->phoneNum);
          printf("\n\tCustomer name: %s\n", tempH->name);
          tempH = tempH->nxt;
        }
      }
    }
    
    #包括
    #包括
    #包括
    结构客户
    {
    int phoneNum;
    字符*名称;
    结构客户*nxt;
    };
    结构客户*initialNode(int i,char*n);
    int isEmpty(结构客户*头部,结构客户*尾部);
    无效排队(int phNum1,char*cName1,结构客户**头,结构客户**尾);
    作废出列(结构客户**头,结构客户**尾);
    无效显示(结构客户*头部,结构客户*尾部);
    内部主(空)
    {
    int phNum;
    char cName[51];//支持的最大名称长度
    int-choice=0;
    结构客户*负责人;
    结构客户*tail;
    head=NULL;
    tail=NULL;
    while(选项!=4)
    {
    printf(“\n输入您的选项:”);
    printf(“\n1.让新客户排队。\n2.让客户排队。\n3.在队列中显示客户。\n4.退出。\n选项:”);
    scanf(“%d”,选择(&C);
    开关(选择)
    {
    案例1:
    {
    printf(“\n输入客户电话号码:”);
    scanf(“%d”和&phNum);
    printf(“\n输入客户名称:”);
    scanf(“%s”,cName);
    排队(phNum、cName、head和tail);
    打破
    }
    案例2:
    { 
    出列(头部和尾部);
    打破
    }
    案例3:
    {
    显示器(头部、尾部);
    打破
    }
    违约:
    {
    如果(选项<1 | |选项>4)
    printf(“\n无效选项!\n”);
    打破
    }
    }
    }
    返回0;
    }
    结构客户*初始节点(int i,char*n)
    {
    内伦;
    struct customer*newCustomer=malloc(sizeof(struct customer));
    newCustomer->phoneNum=i;
    len=strlen(n);
    newCustomer->name=malloc(len+1);
    strcpy(新客户->名称,n);
    newCustomer->nxt=NULL;
    返回新客户;
    }
    int i
    
    #include<stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    struct customer
    {
      int phoneNum;
      char *name;
      struct customer *nxt;
    };
    
    struct customer *initialNode(int i, char *n);
    int isEmpty(struct customer *head, struct customer *tail);
    void enqueue(int phNum1, char *cName1, struct customer **head, struct customer **tail);
    void dequeue(struct customer **head, struct customer **tail);
    void display(struct customer *head, struct customer *tail);
    
    int main(void)
    {
      int phNum;
      char cName[51]; // max name length supported
      int choice = 0;
    
      struct customer *head;
      struct customer *tail;
      head = NULL;
      tail = NULL;
    
      while (choice !=4)
      {
        printf("\nEnter your option:");
        printf("\n1. Enqueue new customer. \n2. Dequeue customer. \n3. Display customer in queue. \n4. Exit. \nOption: ");
        scanf("%d", &choice);
    
        switch(choice)
        {
          case 1: 
          {
            printf("\nEnter customer phone number: ");
            scanf("%d", &phNum);
            printf("\nEnter customer name: ");
            scanf("%s", cName);
            enqueue(phNum, cName, &head, &tail);
            break;
          }
          case 2:
          { 
            dequeue(&head, &tail);
            break;
          }
          case 3: 
          {
            display(head, tail);
            break;
          }
          default: 
          {
            if (choice < 1 || choice > 4)
              printf("\nInvalid option!\n");
            break;
          }
        }
      }
      return 0;
    }
    
    struct customer *initialNode(int i, char *n)
    {
      int len;
      struct customer *newCustomer = malloc(sizeof(struct customer));
      newCustomer->phoneNum = i;
      len = strlen(n);
      newCustomer->name = malloc(len +1);
      strcpy(newCustomer->name, n);
      newCustomer->nxt = NULL;
      return newCustomer; 
    }
    
    int isEmpty ( struct customer *head, struct customer *tail)
    {
      if ((head == NULL) && (tail == NULL))
        return 1;
      else
        return 0;   //cannot is full because it is not array, have no size limitation
    }
    
    void enqueue(int phNum1, char *cName1, struct customer **head, struct customer **tail)
    {
      struct customer *nC = malloc(sizeof(struct customer));
      nC = initialNode(phNum1, cName1);
      if (isEmpty(*head,*tail))
      {
        *head = nC;
        *tail = nC;
      }
      else if (*head == *tail)  // single element
      {
        (*head)->nxt = nC;
        *tail = nC;
      }
      else
      {
        (*tail)->nxt = nC;
        *tail = nC;
      }
      printf("\nSuccessfully enqueue a customer!\n");
    }
    
    void dequeue(struct customer **head, struct customer **tail)
    {
      struct customer *removedCustomer = malloc(sizeof(struct customer));
      if (isEmpty(*head, *tail) == 1)
      {
        printf("\nThe queue is empty!\n");
      }
      else if (*head == *tail) // single element
      {
        removedCustomer = *head;
        free(removedCustomer->name);
        free(removedCustomer);
        *head = NULL;
        *tail = NULL;
      }
      else
      {
        removedCustomer = *head;
        *head = (*head)->nxt;
        free(removedCustomer->name);
        free(removedCustomer);
        printf("\nFirst customer is removed.\n");
      }
    }
    
    void display(struct customer *head, struct customer *tail)
    {
      struct customer *tempH;
      tempH = head;
      if (isEmpty(head, tail) == 1)
      {
        printf("\nThe queue is empty!\n");
      }
      else
      {
        printf("\n===Current customer list===");
        while (tempH != NULL)
        {
          printf("\n\tCustomer phone number : %d", tempH->phoneNum);
          printf("\n\tCustomer name: %s\n", tempH->name);
          tempH = tempH->nxt;
        }
      }
    }