Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Debugging a linked list / node program

$
0
0
Okay, I'm doing an assignment in which I need to take a code for a linked list with a directory that adds nodes, removes them, ends the program, etc. and change it to work with the 'char' data type. The menu should work when you type in letters instead of numbers and you should be able to add characters as nodes rather than numbers. And while I have gotten that to work, I now have a glitch where, when the program runs, it allows me to do what I want to do the first time I use the menu and then it goes to the program quit option without being triggered, as I can see.

The program is minimally modified from the original I was given - I have changed int input and int num to char data types, and made appropriate changes that follow that such as changing %d to %c, and added some comments to show my Prof the parts I understand in the code. I can show the original one though if it helps to provide advice.

#include "stdio.h"
#include "stdlib.h"

struct NODE { //establish the linked list
 int number;
 struct NODE *next; //pointer of type struct node points to int number
};

//prototypes
int  search_value(struct NODE *llist, char num);
void append_node(struct NODE *llist, char num);
void display_list(struct NODE *llist);
void delete_node(struct NODE *llist, char num);

int main(void) {
 char num = 0; //establish variables in main function
 char input = 1; //num and input become char
 int retval = 0;
 struct NODE *llist;
   
 llist = (struct NODE *)malloc(sizeof(struct NODE));
 llist->number = 0; //default first value is zero
 llist->next = NULL;
   
 while(input != 0) { //while loop to allow choices that repeat until user ends
  printf("\n-- Menu Selection --\n"); //menu has letters...
  printf("Q) Quit\n"); // ...instead of numbers for options
  printf("I) Insert\n");
  printf("D) Delete\n");
  printf("S) Search\n");
  printf("P) Display\n");
  scanf("%c", &input); // like in this, change %d to %c throughout code

  switch(input) {
   case 'Q': //option Q simply quits
   default:
    printf("Goodbye ...\n");
    input = 0;
    break;
   case 'I': //option I scans input num and adds a node
    printf("Your choice: `Insertion'\n");
    printf("Enter the value which should be inserted: ");
    scanf("%c", &num);
    append_node(llist, num);
    break;
   case 'D': //option D removes a node
    printf("Your choice: `Deletion'\n");
    printf("Enter the value which should be deleted: ");
    scanf("%c", &num);
    delete_node(llist, num);
    break;
   case 'S': //option S finds a specific value
    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%c", &num);
    if((retval = search_value(llist, num)) == -1) //response if value not found
     printf("Value `%c' not found\n", num);
    else
     printf("Value `%c' located at position `%d'\n", num, retval);
    break;
   case 'P': //option P displays the full list
    printf("You choice: `Display'\n");
    display_list(llist);
    break;
   } /* switch */
  } /* while */

 free(llist);
 return(0);
}

void display_list(struct NODE *llist) {
 while(llist->next != NULL) {
  printf("%d ", llist->number);
  llist = llist->next;
 }

 printf("%d", llist->number);
}

void append_node(struct NODE *llist, int num) {
 while(llist->next != NULL)
  llist = llist->next;

 llist->next = (struct NODE *)malloc(sizeof(struct NODE));
 llist->next->number = num;
 llist->next->next = NULL;
}

void delete_node(struct NODE *llist, char num) {
 struct NODE *temp;
 temp = (struct NODE *)malloc(sizeof(struct NODE));

 if(llist->number == num) { //if user input is equivalent to node value, delete
  /* remove the node */
  temp = llist->next;
  free(llist);
  llist = temp;
 } else {
  while(llist->next->number != num)
   llist = llist->next;

  temp = llist->next->next;
  free(llist->next);
  llist->next = temp;
 }   
}

int search_value(struct NODE *llist, char num) {
 int retval = -1;
 int i = 1;

 while(llist->next != NULL) {
  if(llist->next->number == num)
   return i;
  else
   i++;

  llist = llist->next;
 }

 return retval;
}


My specific question is: why is this happening? What can I modify to make this program streamlined and allow the while loop to function properly? Right now I am just reading through my textbook and trying to understand really deeply how linked lists work (because some of the stuff at the bottom is confusing to me due to inexperience.) I figure that when I have a very deep understanding it will be easier to troubleshoot but I would love to hear your ideas. I would also REALLY appreciate any in-depth explanations about how linked lists work because my textbook can be on the vague side, and that alone will help me so much to do better. :)

Viewing all articles
Browse latest Browse all 51036

Trending Articles