Linked list help

This is a discussion on Linked list help within the C Programming forums, part of the General Programming Boards category; Hello, I'm having a problem with inserting into a linked list... It seems when I'm trying to insert something into ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Post Linked list help

    Hello, I'm having a problem with inserting into a linked list... It seems when I'm trying to insert something into the head I get a runtime error. I assumed that I would be safe from the runtime error because the || statement is supposed to be short circuit evaluated. I'll bold the part I'm talking about.

    Code:
    #include <stdio.h>
    struct node{
           int data;       
           struct node* next;   
    };
    
    void insert(struct node** head, int data);
    void printlist(struct node* head);
    
    int main(void)
    {
        int choice;
        int data;
        struct node* head; // our LL.
        
        do
        {
               printf("Your choices: \n");
               printf("(1) Add to list \n");
               printf("(2) Print list \n");
               printf("(0) quit \n");
               scanf("%d",&choice);
               
               if (choice == 1)
               {
                  printf("What data would you like to insert?\n");
                  scanf("%d",&data);
                  insert(&head,data);      
               }
               if (choice == 2)
               {
                  printlist(head);        
               }
               
        }   while (choice != 0);
        printf("Goodbye\n");
        system("PAUSE");
    }
    
    //Inserts in order from smallest to largest
    void insert(struct node** head, int data)
    {
         struct node* current;
         struct node* temp;
         
         
         //If inserting into the beginning of the list
         if (*head == NULL || (*head)->data > data)
         {
            temp = malloc(sizeof(struct node));
            temp->data = data;
            temp->next = *head;
            *head = temp;
            return;
         }
         
    
         //Traverse list to find the spot we're in
         current = *head;
         while (current->next != NULL && current->next->data < data)
         { 
           current = current->next;
         }
         
         //Insert in middle or the end
         temp = malloc(sizeof(struct node));
         temp->data = data;
         temp->next = current->next;
         current->next = temp;
         return;
    
    }
    
    void printlist(struct node* head)
    {
         struct node* current;
         current = head;
         while (current->next != NULL)
         {
               printf("%d",current->data);          
               current = current->next;
         }     
    }
    I must be doing something wrong. Thanks for the help everyone.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    your head pointer is not initialized - so it contains garbage
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    I appreciate you pointing that out

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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