Insertion Linked List Help

This is a discussion on Insertion Linked List Help within the C Programming forums, part of the General Programming Boards category; Basically, I need to be able to insert values into a blank list and able to print them out once ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Insertion Linked List Help

    Basically, I need to be able to insert values into a blank list and able to print them out once the user has finished all the inputs.

    Heres what I done so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
      struct Node *link;
      int value;
    } Node;
    
    int insert_list(Node **linkp, int new_value);
    void print_list(Node *list);
    
    int main(int argc, char **argv)
    {
      
      int *root;
      root = malloc(sizeof(Node));
      int value;
      int i = 0;
      /* Insert statements here */
      while ((i = scanf("%d",&value)) != 0)
          insert_list(root,value);
    
      print_list(root);
    
      free(root);
      return 0;
    }
    
    void print_list(Node *list)
    {
      Node *current;
      
      current = list;
    
      while (current->value != NULL)
        {
          printf("%d ",current->value);
          current = current->link;
        }
    }
    
    int insert_list(Node **linkp, int new_value)
    {
      
      Node *current;
      Node *previous;
      Node *new;
    
      /* Point to the first node */
      previous = NULL;
      current = *linkp;
    
      /* Look down the list until the node has a value has a smaller value
         than the new_value variable */
    
      while (current != NULL && current->value < new_value)
        {
          previous = current;
          current = current->link;
        }
    
      /* Allocate a new node and store the new value */
    
      new = (Node *)malloc(sizeof(Node));
      if (new == NULL)
        {
          printf("Not enough memory available!\n");
          return 1;
        }
      new->value = new_value;
    
      /* Insert the new node to the list */
    
      new->link = current;
      if (previous == NULL)
        *linkp = new;
      else
        previous->link = new;
      return 0;
    }
    I'm quite stumped on what the error is, I'm pretty sure my insert function is correct and my print function looks alright as well. Not sure if I'm using it properly.. any help would be awesome

    Thanx in advance..

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    If it helps any bit, the error I get when I execute is a segmentation fault..

    Also I'm aware that I'm not free'ing the list properly and I'll fix that soon, even with that it should work.. shouldnt it?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int *root;
    root = malloc(sizeof(Node));
    What are you doing? Why are you using a pointer to an int and mallocing a Node for it? That's what we like to call a BadThingTM.

    I'll make this quick because I'm starving. Your print function is wrong. You should check to see that the node is not NULL before you check any of its values. There's probably more, but that'll do in a pinch.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Ok, I've managed to solve the insertion and the print function which works now. So I wanted to add more functions to modify the list, specifically delete and searching. I'm having trouble deleting 1 of the remaining nodes.

    For example, if I input values of say 5, 2, 9, 6. The program will print them in order 2, 5, 6, 9. When I delete it only manages to delete the 5, 6, 9 and not the 2. The first number of the list never gets deleted

    Heres what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define FALSE 0
    #define TRUE 1
    
    typedef struct NODE {
            struct NODE *link;
            int    value;
    } Node;
    
    // Function Prototypes
    int sll_insert(register Node **linkp);
    int view_list(register Node *linkp);
    int delete_whole(Node *linkp);
    int search_list(register Node *linkp, int value);
    int delete_specific(register Node *linkp, int value);
    
    // Main Function
    int main(int argc, char **argv)
    {
        int input;
        int c;
        Node *root = NULL;
    
        do
        {
             printf("1. Add value\n");
             printf("2. View list\n");
             printf("3. Delete whole list\n");
             printf("4. Searching\n");
             printf("5. Delete specific node(s)\n");
             printf("6. Exit\n\n");
    
             printf("Your current option is: ");
             scanf("%d",&input);
    
             switch(input){
             case 1:
                  sll_insert(&root);
                  break;
             case 2:
                  view_list(root);
                  break;
             case 3:
                  delete_whole(root);
                  break;
             case 6:
                  printf("Now exiting!\n");
                  exit(0);
             default:
                  printf("Invalid key.. Now exiting!\n");
                  exit(0);
                  break;
             }
        } while (1);
        return 0;
    }
    
    int sll_insert(register Node **linkp)
    {
        register Node *current;
        register Node *new;
    
        int input_value = 0;
        printf("Input a number: ");
        scanf("%d",&input_value);
    
        // Look for the right place until we reach a node whose value is greater
        // than or equal to the input_value variable
    
        while((current = *linkp) != NULL && current->value < input_value)
                       linkp = &current->link;
    
    
        // Allocate a new node and store the new value into it
    
        new = (Node *)malloc(sizeof(Node));
        if (new == NULL)
           return FALSE;
        new->value = input_value;
    
        // Insert the new node into the list and return TRUE
    
        new->link = current;
        *linkp = new;
        return TRUE;
    }
    
    int view_list(register Node *linkp)
    {
        register Node *current;
    
        if (linkp == NULL) {
    	printf("There is nothing in the list!\n");
        return 1;
        }
    
        current = linkp;
    
        while (current->link != NULL)
        {
          printf("%d ",current->value);
          current = current->link;
        }
    
        // Used to print out the last one
        printf("%d ",current->value);
        printf("\n");
    }
    
    int delete_whole(Node *linkp)
    {
        Node *current;
    
        if (linkp == NULL) {
        printf("There is nothing to delete!\n");
        return 1;
        }
    
        // Start deleting
        current = linkp;
        while(current != NULL)
        {
           free(current);
           current = current->link;
        }
        // Delete remaining
        free(current);
        printf("\n\nThe list has been fully deleted!\n\n");
    
    }

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    127
    I assume you're having problems with delete_whole because delete_specific hasn't been implemented yet. The algorithm is incorrect. You attempt to access a node after releasing its memory, a very bad thing to do. This works fine.
    Code:
    int delete_whole(Node *linkp)
    {
      Node *current;
      Node *save;
    
      if (linkp == NULL) {
        printf("There is nothing to delete!\n");
        return 1;
      }
    
      // Start deleting
      current = linkp;
      while(current != NULL)
      {
        save = current->link;
        free(current);
        current = save;
      }
      printf("\n\nThe list has been fully deleted!\n\n");
    }
    Notice that the last call to free wasn't needed because at that point current was null. Freeing a null node is legal, but why bother when you know that it's null? You should also consider setting root to null after the function returns. That way you can avoid trying to access an invalid pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21