程序更新链接列表

时间:2018-10-14 03:25:05

标签: c

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

Tom -> David -> John...

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

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

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

有人可以帮我解决我的问题吗?谢谢您的宝贵时间。

编辑

这是我的源代码:

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

2 个答案:

答案 0 :(得分:1)

您可以为此声明一个新功能。因为每次调用此函数头都会被重新声明。

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

答案 1 :(得分:1)

  

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

问题在于,您必须声明一个指针,该指针会使while循环之外的列表的头疼。

NodePointer head = NULL; 
do {
     ....

     switch (choice) {
     case 1: {
          char name[NAME_SIZE] = "";
          while(0 != strcmp(name, EXIT)){

              ....
          }
     ....
     }
} while(choice != 2);

请注意,您已经在case内的块作用域中声明了变量。参见Scope rules in C
在作用域的末尾,该变量不再可用,并且其内容丢失。下次“访问”代码时,您将获得一个全新的初始化变量。