c - 程序更新链表

标签 c

我正在尝试创建一个带有名称的链接列表,例如:

汤姆 -> 大卫 -> 约翰...

在我的主函数中,我有一个switch菜单,程序会询问您是否要创建新列表或退出。

当用户选择1时,程序会调用insertIntoLinkedList(name, &head)函数,用户可以在其中添加名称或输入“end”退出。

一切正常,但是如果用户输入end并再次选择选项1,程序会创建一个新的链接列表,而我想要将名称添加到现有列表。

有人可以帮我解决我的问题吗?感谢您抽出时间。

编辑

这是我的源代码:

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

#define NAME_SIZE 30
#define EXIT "end"

// Node structure
struct node {
  char name[NAME_SIZE];
  struct node *next;
};

typedef struct node Node;
typedef struct node* NodePointer;

int userChoice(void);
void insertIntoLinkedList(char [], NodePointer *);
void displayNames(NodePointer);

int nodeCounter = 0;

int main(void) {
  int choice = 99;

  do {
      printf("\n--- MENU ---\n\n");
      printf("1.\tCreate a new friend list\n");
      printf("2.\tExit o_O");
      printf("\n\n------------\n");
      printf("Go to:\t");
      choice = userChoice();

      switch (choice) {
          case 1: {
              char name[NAME_SIZE] = "";
              NodePointer head = NULL;
              while(0 != strcmp(name, EXIT)){
                  printf("Enter new friend name or \"%s\" to return back to the main menu: ", EXIT);
                  scanf("%s", name);
                  if(0 != strcmp(name, EXIT)){
                      insertIntoLinkedList(name, &head);
                      displayNames(head);
                  }
              }
              displayNames(head);
              break;
          }
          case 2: {
              printf("\n\nYou have %d node(s) in your linked list. Have a great day.\n\n", nodeCounter);
              break;
          }
          default:
              printf("There is no such option. Please choose one of the option from 1 to 2.\n");
      }
  } while(choice != 2);


  return 0;
}

int userChoice() {
  int num;
  scanf("%d", &num);
  return num;
}

void insertIntoLinkedList(char word[], NodePointer *head){
  NodePointer newNode = NULL;
  NodePointer previous = NULL;
  NodePointer current = *head;

  newNode = malloc(sizeof(Node));
  if(NULL != newNode){
      strcpy(newNode -> name, word);
      while(NULL != current && strcmp(word, current -> name) > 0){
          previous = current;
          current = current -> next;
      }

      if(NULL == previous){
          newNode -> next = current;
          *head = newNode;
      } else {
          previous -> next = newNode;
          newNode -> next = current;
      }
  }
}

void displayNames(NodePointer current) {
  nodeCounter = 0;
  if(NULL == current){
      printf("Friend list is empty... I am sorry :(\n\n");
      return;
  } else {
      printf("\nCurrent friend list: ");
      while(NULL != current){
          nodeCounter++;
          printf("%s → ", current -> name);
          current = current -> next;
      }
      printf("\nNumber of friends in your current list:\t%d\n\n", nodeCounter);
  }
}

最佳答案

那么你可以为此声明一个新函数。因为每次调用这个函数头都会被重新声明。

E.g case 3:printf("\nEnter A New Friend Name:\n");
       scanf("%s",name); 
       insertIntoLinkedList(name, &head);
       displayNames(head);
       break;

关于c - 程序更新链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799223/

相关文章:

c - 如何在 Eclipse 中编写不同的 C 小程序,所有这些程序都有自己的 main()?

c - scanf ("%[^\n]s",a) 设置字符串大小

c - 通过查看与 c 中的源代码相反的二进制文件,您可以获得什么?

c - recvfrom : Bad address, sendto: 协议(protocol)不支持的地址族

c - Scanf 和一个手动函数一起工作时获取字符串

C TCP Server 关闭前不发送数据

c - 数组结构和内存访问模式

c - 为什么整数溢出在 if 语句条件内部的工作方式不同?

c - 在 C 中初始化 int 数组而不分配值

c - System V AMD64 ABI 浮点可变参数顺序