Thread: Linked List

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    66

    Linked List

    I am currently learning linked list and our proff gave us this code but I can't seem to compile it, what's the problem here?


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node
    {
           int x;
           struct node *next;
           };
           
           int main()
           {
              struct node *root;
              struct node *conductor;
              conductor=root;
              root = malloc(sizeof(struct node));
              root->next=0;
              root->x=5;
           
               while(conductor->next!=0);
               {
                  printf("%d",conductor->x);
                  conductor=conductor->next;
                                           
                                           }
    
               system("pause");
               }
    I can't seem to print the value of x :| why is it like that?


    (EDIT Done with the tagging )
    Last edited by kyel_dai92; 03-19-2011 at 03:54 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,661
    Read the intro threads - especially one that should be read BEFORE posting code.

    Specifically, code tags.
    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
    Mar 2011
    Posts
    66
    Bump

    Bump
    Last edited by kyel_dai92; 03-19-2011 at 03:54 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kyel_dai92 View Post
    I am currently learning linked list and our proff gave us this code but I can't seem to compile it, what's the problem here?


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node
    {
           int x;
           struct node *next;
           };
           
           int main()
           {
              struct node *root;
              struct node *conductor;
              conductor=root;
              root = malloc(sizeof(struct node));
              root->next=0;
              root->x=5;
           
               while(conductor->next!=0);
               {
                  printf("%d",conductor->x);
                  conductor=conductor->next;
                                           
                                           }
    
               system("pause");
               }
    I can't seem to print the value of x :| why is it like that?


    (EDIT Done with the tagging )
    Take a close look at the first couple of pointers before malloc()... conductor=root ... what is the value of root at that point?

    If this is unmodified code from your instructor, you need to ask him if the bugs in it were deliberate...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    what if I want to add another block? or node?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kyel_dai92 View Post
    what if I want to add another block? or node?
    Well, first you need to fix the bugs in what you've got.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Quote Originally Posted by CommonTater View Post
    Well, first you need to fix the bugs in what you've got.
    I've fixed it here it is

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node
    {
           int x;
           struct node *next;
           };
           
           int main()
           {
              struct node *root;
              struct node *conductor;
              root = malloc(sizeof(struct node));
              conductor=root;
              root->next=0;
              root->x=5;
           
               while(conductor->next!=NULL);
               {
                  printf("%d",conductor->x);
                  conductor=conductor->next;
                                           
                                           }
               
               system("pause");
               }
    now how to add anotehr value and block? so that It will print 2 then (the value i inserted )

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I take it you're on like day 1 of this part of your course... (which is fine)

    Basically you need to create a new struct in memory and get a pointer to it.
    Then you need to insert whatever data
    Finally you need to add it to the end of your list by modifying the next pointer in your previous last record.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Quote Originally Posted by CommonTater View Post
    I take it you're on like day 1 of this part of your course... (which is fine)

    Basically you need to create a new struct in memory and get a pointer to it.
    Then you need to insert whatever data
    Finally you need to add it to the end of your list by modifying the next pointer in your previous last record.
    How am I going to do it? my knowledge about linked list is only 10% or even lower how do i do that? I am not gonna modify, I am just gonna add, they are gonna show an output of 5 then 2.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In a linked list each record provides you with a pointer to the next record.

    1.next -> 2.next -> 3

    So if you want to add #4 you you have to make #3 point to the new entry...

    1.nest -> 2.next -> 3.next -> 4

    That means modifying the "next" pointer in your last record to point to the new one

    I'd suggest you give this a read...
    Cprogramming.com Tutorial: Linked Lists

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    so it'll be like this?
    Code:
    
    
    struct node *root;
              struct node *conductor;
              conductor=root;
              root = malloc(sizeof(struct node));
              root->next=0;
              root->x=5;
    root->next-next=malloc(sizeof(struct node));
    root->x=4;
    like this?

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's still wrong! You've ignored CommonTater's question:

    Take a close look at the first couple of pointers before malloc()... conductor=root ... what is the value of root at that point?
    which is extremely important. Don't put the cart before the horse looking to add stuff to what's currently quite broken.

    There are a bazillion tutorials and explanations of linked lists on the net. Surely you can Google some up and try to get a better understanding of them?

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Quote Originally Posted by rags_to_riches View Post
    It's still wrong! You've ignored CommonTater's question:



    which is extremely important. Don't put the cart before the horse looking to add stuff to what's currently quite broken.

    There are a bazillion tutorials and explanations of linked lists on the net. Surely you can Google some up and try to get a better understanding of them?
    well I didn't ignored, I actually read it and used it for my code

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    Bump

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kyel_dai92 View Post
    Bump
    Don't just type "bump". If you have a question, ask a question, and if you want code looked at, post it in code tags. Otherwise, let your thread die.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM