Thread: My code is not working why ?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    65

    My code is not working why ?

    My code is not working as it supposed to do. code compile with no errors but it doesn't give the output as expected

    What's wrong with the code

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    struct node    
    {
       int x;
       struct node *next;   
    };
    
    
    int main ()   
    {
       struct node *current = NULL; 
       struct node *last = NULL; 
       struct node *head = NULL;
    	
       current = malloc(sizeof(*current)); 
       last = malloc(sizeof(*last)); 
      
       
       while ( current!= NULL)
       {
    	   current->x = 10;
    	   current->next = last;
    	   
    	   current->x = 30;
    	   current->next = NULL;
       }
       
      head = current;
      
      while (head != NULL)
      {
    	  printf("%d\n", head->x);
          head = head-> next;
      }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    835
    You haven't said what you expect the code to do, but assuming that you want to insert a few nodes into a linked list and then print them out, how about:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct Node Node; 
     
    struct Node {
        int   data;
        Node *next;
    };
     
    int main() {
        Node *head = NULL;
     
        for (int i = 0; i < 5; ++i) {
            Node *nd = malloc(sizeof *nd);
            nd->data = i * 10;
            nd->next = head;
            head = nd;
        }
     
        for (const Node *nd = head; nd; nd = nd->next)
            printf("%d ", nd->data);
        putchar('\n');
     
        return 0;
    }
    It's a good idea to write a function to create the nodes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    typedef struct Node Node; 
     
    struct Node {
        int   data;
        Node *next;
    };
     
    Node *new_node(int data, Node *next) {
        Node *nd = malloc(sizeof *nd);
        if (!nd) {
            perror("new_node");
            exit(EXIT_FAILURE);
        }
        nd->data = data;
        nd->next = next;
        return nd;
    }
     
    int main() {
        Node *head = NULL;
     
        for (int i = 0; i < 5; ++i)
            head = new_node(i * 10, head);
     
        for (const Node *nd = head; nd; nd = nd->next)
            printf("%d ", nd->data);
        putchar('\n');
     
        return 0;
    }
    Last edited by john.c; 01-03-2020 at 07:14 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    65
    Quote Originally Posted by john.c View Post
    You haven't said what you expect the code to do, but assuming that you want to insert a few nodes into a linked list and then print them out, how about:
    I want to add two nodes into linked list and then print out them

    I wanted to know whats wrong with my code Why it's not printing two nodes. Whats wrong with code ?

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Quote Originally Posted by gajya View Post
    I want to add two nodes into linked list and then print out them

    I wanted to know whats wrong with my code Why it's not printing two nodes. Whats wrong with code ?
    Code:
       while ( current!= NULL)
       {
           current->x = 10;
           current->next = last;
            
           current->x = 30;         /* You're immediately writing over the same node */
    
           current->next = NULL;    /* with different values */
    perhaps you meant

    Code:
           current->x = 10;
           current->next = last;
            
           last->x = 30;
           last->next = NULL;
    I have no idea what you're trying to do with the while loop... current is never going to become NULL so it's an infinite loop. Get rid of it

    But if you know you only ever want 2 nodes you'd use an array. Otherwise you'd write something along the lines of what john.c wrote

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    65
    Quote Originally Posted by Hodor View Post
    I have no idea what you're trying to do with the while loop... current is never going to become NULL so it's an infinite loop. Get rid of it

    But if you know you only ever want 2 nodes you'd use an array. Otherwise you'd write something along the lines of what john.c wrote
    following code works for me

    Code:
    #include<stdio.h>#include<stdlib.h>
      
    struct node    
    {
       int x;
       struct node *next;   
    };
     
    int main ()   
    {
       struct node *current = NULL; 
       struct node *last = NULL; 
       struct node *head;
         
       current = malloc(sizeof(*current)); 
       last = malloc(sizeof(*last)); 
       
       current->x = 10;
       current->next = last;
            
       last->x = 30;
       last->next = NULL;
      
       head = current;
       
      while (head != NULL)
      {
          printf("%d\n", head->x);
          head = head-> next;
      }
      free(current);
      free(last);
      free(head);
      return 0;
    }
    I think there is one problem in the code I never check weather memory is allocated or not allocated that should I check before

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the following code not working?
    By Sonam Agarwal in forum C Programming
    Replies: 6
    Last Post: 01-22-2014, 04:21 AM
  2. why is my code not working from here it looks ok
    By Lisa_townsend in forum C Programming
    Replies: 19
    Last Post: 12-05-2009, 12:08 PM
  3. code not working as it should..
    By transgalactic2 in forum C Programming
    Replies: 21
    Last Post: 12-05-2008, 02:10 PM
  4. working set code
    By chinu in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2008, 08:44 PM
  5. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM

Tags for this Thread