Thread: Simple linked list program need help

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    Simple linked list program need help

    I'm trying to create linked list and display the element in list

    I've written this code

    Code:
    #include<stdio.h>
    #include<stdlib.h> 
      
    struct Node  
    { 
      int data; 
      struct Node *next; 
    }; 
      int main() 
    { 
      struct Node* head = NULL; 
      struct Node* second = NULL; 
      struct Node* third = NULL; 
           
      head  = (struct Node*)malloc(sizeof(struct Node));  
      second = (struct Node*)malloc(sizeof(struct Node)); 
      third  = (struct Node*)malloc(sizeof(struct Node)); 
       
      head->data = 1; //assign data in first node 
      head->next = second; // Link first node with second    
       
      second->data = 2; //assign data to second node 
      second->next = third;   
       
      third->data = 3; //assign data to third node 
      third->next = NULL; 
      
      return 0; 
    }
    I don't understand How to print the element in list ?
    Last edited by abhi143; 10-10-2018 at 03:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your basic list operation to visit every node is.

    Node *temp = head;
    while ( temp ) temp = temp->next;
    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.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    Your basic list operation to visit every node is.

    Node *temp = head;
    while ( temp ) temp = temp->next;
    Code:
    #include<stdio.h>#include<stdlib.h> 
       
    struct Node  
    { 
      int data; 
      struct Node *next; 
    }; 
      int main() 
    { 
      struct Node* head = NULL; 
      struct Node* second = NULL; 
      struct Node* third = NULL; 
      struct Node *temp = head;
      
      head  = (struct Node*)malloc(sizeof(struct Node));  
      second = (struct Node*)malloc(sizeof(struct Node)); 
      third  = (struct Node*)malloc(sizeof(struct Node)); 
        
      head->data = 1; //assign data in first node 
      head->next = second; // Link first node with second    
        
      second->data = 2; //assign data to second node 
      second->next = third;   
        
      third->data = 3; //assign data to third node 
      third->next = NULL; 
      
     
       while ( temp )
           printf("%d\n", temp->next);
    	   temp = temp->next;
       
      return 0; 
    }
    It give following results


    72196967219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    7219696
    Last edited by abhi143; 10-10-2018 at 11:24 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well....

    1. You initialise temp to be head, before you've even set head up to point to anything.

    2. Your while loop lacks braces to group all the statements together.
    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.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    Well....

    1. You initialise temp to be head, before you've even set head up to point to anything.

    2. Your while loop lacks braces to group all the statements together.
    okay

    Code:
    #include<stdio.h>#include<stdlib.h> 
        
    struct Node  
    { 
      int data; 
      struct Node *next; 
    }; 
      int main() 
    { 
      struct Node* head = NULL; 
      struct Node* second = NULL; 
      struct Node* third = NULL; 
      struct Node *temp = NULL;
      
       
      head  = (struct Node*)malloc(sizeof(struct Node));  
      second = (struct Node*)malloc(sizeof(struct Node)); 
      third  = (struct Node*)malloc(sizeof(struct Node)); 
         
      head->data = 1; //assign data in first node 
      head->next = second; // Link first node with second    
         
      second->data = 2; //assign data to second node 
      second->next = third;   
         
      third->data = 3; //assign data to third node 
      third->next = NULL; 
       
       temp = head;
      while (temp != NULL) 
      { 
         printf(" %d ", temp->data); 
         temp = temp->next; 
      }
          
      return 0; 
    }
    Now I am able to create link linked list for three elements

    What to do if I want to create linked list for 50 elements ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What to do if I want to create linked list for 50 elements ?
    Write a function.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    what about:
    temp->next = malloc(..);
    temp = temp->next;
    ----
    ....
    temp->next = NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-30-2013, 08:44 PM
  2. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  3. create a simple program using linked list
    By tillu in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2011, 12:53 AM
  4. A simple linked list program
    By glanzvoll in forum C Programming
    Replies: 7
    Last Post: 03-03-2010, 05:54 AM
  5. simple linked list
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 12-01-2005, 11:27 AM

Tags for this Thread