Linked List

This is a discussion on Linked List within the C Programming forums, part of the General Programming Boards category; I am trying to create a linked list that has the user input five values, then displays them. After which ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19

    Question Linked List

    I am trying to create a linked list that has the user input five values, then displays them. After which the user is prompted to input an additional value and then all the values are displayed again. This is what I have, what is it that I am doing wrong. I am referencing my text book and doing almost the same thing but not getting good results. Thanks for helping me out.

    Code:
    #include <stdio.h>
    struct num
    {
    int value;
    struct num *nextnum;
    };
    
    void display (struct num *);
    
    int main ()
    {
    printf("Enter first value");
    scanf ("%d",struct num &n1);
    printf("Enter second value");
    scanf ("%d",struct num &n2);
    printf("Enter third value");
    scanf ("%d",struct num &n3);
    printf("Enter fourth value");
    scanf ("%d",struct num &n4);
    printf("Enter fifth value");
    scanf ("%d",struct num &n5);
    struct num * firtst;
    
    first = &n1;
    n1.nextnum = &n2;
    n2.nextnum = &n3;
    n3.nextnum = &n4;
    n4.nextnum = &n5;
    n5.nextnum = NULL;
    
    display (first);
    
    printf("Enter sixth value");
    scanf ("%d", struct num &n6);
    
    first = &n1;
    n1.nextnum = &n2;
    n2.nextnum = &n3;
    n3.nextnum = &n4;
    n4.nextnum = &n5;
    n5.nextnum = &n6;
    n6.nextnum = NULL;
    
    display (first);
    
    system ("PAUSE");
    
    return 0;
    }
    
    void display (struct num *contents)
    {
    while (contents != NULL)
          {
          printf("\n%d",contents->value);
          contents = contents -> nextnum;
          }
    return;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    it is not C but C++ or C99 - you mix statements and variable declarations

    try to move all variable declarations before other code,like
    [code]
    struct num n1;
    struct num n2;
    ...

    printf("Enter first value");
    scanf ("%d",&n1.value);
    ...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    TX
    Posts
    19
    Thanks, that cleared it up to the point I could makes heads and tails of it.

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, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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