Linked List Program Error

This is a discussion on Linked List Program Error within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a simple linked list implementation program... But im facing a problem in printing the contents of ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Question Linked List Program Error

    Hello,
    I am writing a simple linked list implementation program... But im facing a problem in printing the contents of list, here is the code..
    Code:
    #include<stdio.h> 
     
    void insert(int value); 
    int delete(); 
    void showList(); 
     
    struct node 
    { 
       int data; 
       struct node *nextNode; 
    }; 
    
    //start pointer node 
    struct node *start = NULL; 
     
    int main() 
    { 
     
       int c=0,value; 
       printf("\nThis Program demonstrates simple Linked List\n"); 
       while(c != 4) 
       { 
          printf("Choices:\n"); 
          printf("Press 1 to insert into Linked List\n"); 
          printf("Press 2 to delete from Linked List\n"); 
          printf("Press 3 to display Linked List\n"); 
          printf("Press  to quit\n"); 
          scanf("%d",&c); 
     
          switch(c) 
          { 
             case 1: 
                printf("\n Enter Value that you want to insert:\n"); 
                scanf("%d",&value); 
                insert(value); 
                break; 
             case 2: 
                printf("\n Deleting first element from List:\n"); 
                printf("\n%d \n", delete()); 
                break; 
             case 3: 
                printf("\n Linked List:\n"); 
                    break; 
     
            case 4: 
                printf("\nQuitting.....\n");
                exit(0); 
     
             default: 
                printf("\nNot a valid choice..\n"); 
     
          } 
       } 
     
       return 0; 
    } 
     
    void insert(int value) 
    { 
         
       struct node *n; 
        n = (struct node *)malloc(sizeof(struct node)); 
       n->data = value; 
        if(start == NULL) 
        { 
            printf("start null\n"); 
            start = n; 
            n->nextNode = NULL; 
         
        } 
        else 
        { 
            n->nextNode = start; 
            start = n; 
        } 
    } 
     
    int delete() 
    { 
        return 0;     
    } 
    
    //this portion is creating problem...  
    void showList() 
    { 
        struct node *t; 
        t = start; 
     
        while(t!= NULL) 
        { 
            printf("%d , ",t->data); 
            t = t->nextNode; 
        } 
    }
    Whenever i choose 3, that is list the contents of list.. it shows blank..
    I don't understand where is the fault..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Well the first problem is, you're not even calling showList() anywhere in main.

    When you're done inside your loop, calling printf("\n"); would be good as well, to ensure you see output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Thumbs up

    Quote Originally Posted by Salem View Post
    Well the first problem is, you're not even calling showList() anywhere in main.

    When you're done inside your loop, calling printf("\n"); would be good as well, to ensure you see output.
    LOL... i forgot tht sorry....
    Now it's working.. Thnx!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double-linked list error in image smoothing program
    By mwhar in forum C Programming
    Replies: 4
    Last Post: 04-27-2011, 07:56 PM
  2. linked list error....
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-09-2009, 09:56 PM
  3. Help with some error...(linked list)
    By google@ in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 08:29 AM
  4. Linked LIst error
    By Armatura in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 11:40 PM
  5. linked list error
    By breanne in forum C++ Programming
    Replies: 2
    Last Post: 08-13-2002, 01:43 AM

Tags for this Thread


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