Anton S.
Anton S.

Reputation: 1029

Program updates Linked List

I am trying to create a linked list with names, for example:

Tom -> David -> John...

In my main function, I have a switch menu where the program asks if you want to create a new list or exit.

When the user choose 1, the program calls insertIntoLinkedList(name, &head) function where the user can add name(s) or type "end" to exit.

Everything works fine, however if the user enter end and choose option 1 again, the program creates a new linked list whereas I want to add names to an existing list.

Can someone please help me to figure out my problem? Thank you for your time.

EDIT

Here is my source code:

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

Upvotes: 2

Views: 83

Answers (2)

Rabbid76
Rabbid76

Reputation: 210889

Everything works fine, however if the user enter end and choose option 1 again, the program creates a new linked list whereas I want to add names to an existing list.

The issue is that you have to declare the pointer which sores the head of the list outside the while loop.

NodePointer head = NULL; 
do {
     ....

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

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

Note you have declared the variable in the block scope inside the case. See Scope rules in C.
At the end of the scope the variable is not longer accessible and its content is lost. When you "reach" the code the next time, the you get a completely new initialized variable.

Upvotes: 1

Amcodes
Amcodes

Reputation: 21

Well U Can Just Declare A New Function For That. Because Every Time You Call This Function Head Is Re-declared .

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

Upvotes: 2

Related Questions