C QueueLinkedList 字符串变量不起作用

标签 c struct linked-list

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

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

最佳答案

我发现代码存在以下问题。

  1. headtail 应初始化为 NULL。这表示一个空列表。
  2. initialnode 函数中,您直接将扫描的指针指定为 newCustomer->name = n。这是错误的。由于您已将结构声明为具有字符指针,因此您还需要为每个字符串单独分配空间。
  3. 入队和出队函数可以更改主函数中的headtail指针的值。您可以更改函数声明以为此传递双指针。
  4. 您还需要在出队时释放该结构。这在小程序中是绝对没有必要的,但无论如何都是一个很好的做法,可以避免较大程序中的内存泄漏。
  5. 在显示函数中,您有一个不必要的 malloc,需要将其删除。该函数不需要分配内存。
  6. main 中的变量 choice 未初始化。

请看下面的代码

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

关于C QueueLinkedList 字符串变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58654126/

相关文章:

generics - 如何使用消息包序列化或反序列化通用结构?

java - 为什么在 LinkedLists 中添加特定索引比在 ArrayLists 中慢

java - Java链表实现中的head是什么

C 编程检查缓冲区中的消息

c - 有没有一种可靠的方法可以知道哪些库可以在 elf 二进制文件中 dlopen()ed?

c - 使用地址在 gdb 中打印结构类型的值

C++ 模板链表链接器错误

c++ - 一个二年级的计算机本科生能做什么在未来可能被认为是有值(value)的?

C:遍历字符串数组

c - 将 C 或 C99 中的结构数组初始化为所有相同的值