Thread: second node doesn't print

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    second node doesn't print

    Why the second node in list doesn't print

    Code:
    #include<stdio.h>#include<stdlib.h> 
    
    
    struct node{
      int Number;
      struct node *next;
    };
    
    
    struct node* newNode(int number, struct node *next) {
        struct node *new = malloc(sizeof(*new));
               new->Number = number;
               new->next = next;
    	return new;
    }
    
    
    void show(struct node *head){
         struct node *c;
         c = head;
         while (c!=NULL){
               printf("%d\n",c->Number);
               c = c->next;
               }
     
         }
    	 
    int main (void ) 
    {
        struct node *head = NULL;
    	struct node *first = NULL;
    	struct node *second = NULL;
    
    
    	
    	first = newNode(10, head);
        head = first;
        second = newNode(20, head);
        head = first;
    	show(head);
    	
    	return 0;
    }
    10

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    head = first;
    show(head);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by stahta01 View Post
    Code:
    head = first;
    show(head);
    Thanks for point out

    Code:
        struct node *head = NULL;  
        struct node *first = NULL;
        struct node *second = NULL;
        struct node *third = NULL;
    
        first = newNode(10, head);
        head = first;
        second = newNode(20, head);
        head = second;
        third = newNode(30, head);
        head = third;
        show(head);
    30
    20
    10

    I do not want that list

    I want to make following
    10
    20
    30

    What should I do to make that output ?
    Last edited by vajra11; 11-11-2019 at 07:03 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,659
    You only need ONE variable for your list.

    You're confusing yourself with all that first, second, third stuff.
    Code:
    struct node *head = NULL;  
     
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    show(head);
    If you want to print 10,20,30, then you need a different newNode() function.
    The one you have pushes things to the front of the list.

    If you want to append to the list, then you need to write a different 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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    You only need ONE variable for your list.

    You're confusing yourself with all that first, second, third stuff.
    [code]
    In this case

    Code:
    int main (void ) {
       struct node *head = NULL;  
      
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    show(head);
         
        return 0;
    }
    What are the steps to delete the node in list I want to make a function that can delete any node

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code doesn't print array value
    By vajra11 in forum C Programming
    Replies: 2
    Last Post: 10-16-2019, 10:24 AM
  2. doesn't print the real value
    By jotask in forum C Programming
    Replies: 2
    Last Post: 11-24-2015, 03:49 PM
  3. doesn't print what it's supposed to
    By fane in forum C Programming
    Replies: 5
    Last Post: 02-01-2014, 07:31 PM
  4. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM

Tags for this Thread