Linked list program, don't understand it fully

This is a discussion on Linked list program, don't understand it fully within the C Programming forums, part of the General Programming Boards category; my program doesn't work like it should. I know whats not working, I just don't know how to fix it. ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    7

    Linked list program, don't understand it fully

    my program doesn't work like it should. I know whats not working, I just don't know how to fix it. In the program I keep replacing start with temp. I'm new to the concept and pretty confused.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    main()
    {
        struct node
        {    
            int value;
            
            node *next;
        };   
           
          node * temp;
          node * start;
          node * end;
                
          temp = new node;
          temp ->value =5;
          temp ->next = NULL;
          
          start = temp;
              
          temp = new node;
          temp ->value =2;
          temp ->next = NULL;
          
                
          start = temp;
         
          temp = new node;
          temp ->value =7;
          temp ->next = NULL;
          
          start ->next = temp;
              
              for(node *s = start; s; s = s->next)
                {
                    
                  printf("Number : %d\n", s->value);
                }
     
          system("pause");  
          
    }
    I am aware I don't use "end" in the program. I have been changing things and haven't used it in the current version.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Quote Originally Posted by pwnsyoshi33 View Post
    I know whats not working, I just don't know how to fix it.
    And you chose to keep this info from us? It's hard for us to find your problem if you don't explain what is wrong. What output do you get? What output are you expecting? In a linked list, all the nodes need to be linked. Why are you setting temp->next to NULL?

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    After I create a new value, I place it in start. I just don't know how to link it together.

    as in my code
    Code:
    temp = new node;      temp ->value =5;
          temp ->next = NULL;
           
          start = temp;
               
          temp = new node;
          temp ->value =2;
          temp ->next = NULL;
           
                 
          start = temp;
    I assign the new values to start. I don't know how to link the values together.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Mods, please move.

    Ugh. This is C++ (using new instead of malloc()), and it's in the C forum. Out of the 4 threads you started, 3 were in the wrong forum, and one was a duplicate. You've failed pretty hard at basic forum use so far. Please read the following:
    Announcements - C Programming
    How To Ask Questions The Smart Way

    Learn to use the forums properly if you want help.

    You need to link the nodes like so:
    Code:
    // first node -- assumes you allocated and set it's value
    temp->next = NULL  // set it's next element to NULL, i.e. the last node in the list
    start = temp;  // set start to the start of the list
    
    // all subsequent nodes -- assumes you allocated and set it's value
    temp->next = start;  // prepend new node to beginning of list
    start = temp;  // set start to the new beginning
    That builds your list by adding nodes to the beginning. Adding them to the end is a bit more tricky, so given the troubles you were having in your previous post, I think you need to play with linked lists this way before moving on.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Indeed. pwnsyoshi33, do you intend to write this program in C or C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice, program no longer fully functional.
    By bchaffin72 in forum Linux Programming
    Replies: 3
    Last Post: 10-21-2010, 03:51 PM
  2. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  3. Dont understand something in linked list
    By elwad in forum C Programming
    Replies: 19
    Last Post: 09-21-2009, 07:26 PM
  4. cant fully understand what these lines mean?
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 01-26-2009, 02:08 PM
  5. I don't understand this line in a linked list
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 04:15 PM

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