Thread: Linked List program help

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

    Linked List program help

    I don't understand linked list very well and am a bit confused. I searched and tried to learn how to use them correctly on the internet, but I doubt this is correct.

    I'm trying to get the program to make a linked list with, First name, last name, SSN, and DOB.

    Any feedback on what to do, or help with code would be greatly appreciated.

    if you know of a good tutorial on linked list, because I want to learn and understand them in greater depth, please share it with me.

    thanks in advanced

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    main()
    {
          struct student
          { 
            char firstName[20];
            char lastName[20];
            int socialSecurity;
            int dob;
            student * next;
          }newStudent; structures
          
          student * temp;
          student * start;
          student * end;
          
          temp = new student;
          temp ->lastName[0] = 'Donald';
          temp ->firstName[0] = 'Mat';
          temp -> socialSecurity = 354-56-5632;
          temp -> dob = 10-18-87;
          temp ->next = NULL;
          
          start = temp;
          end = temp;
          temp -> next = start;
              
          temp = new student;
          temp ->lastName[0] = 'Brown';
          temp ->firstName[0] = 'Tom';
          temp -> socialSecurity = 352-33-5111;
          temp -> dob = 12-25-83;
    
          temp ->next = NULL;
          
          end ->next = temp;
              
              for(student *s = start; s; s = s->next)
                printf("name %s\n", s->lastName);
          
          system("pause");  
          
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Your concept on strings are a bit confused and the way you have SocialSecurity is set up should be a string since you have '-' characters in them.... also temp= new student??? Look into Mallochttp://en.wikipedia.org/wiki/C_dynam...ory_allocation
    Last edited by camel-man; 02-08-2012 at 10:48 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: "new" is a C++ keyword not a C keyword.
    Which language are you programming?

    Tim S.
    "...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

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to decide if this is a C or C++ program. The "new" is a C++ keyword not available in C. In C you would use malloc and free for dynamically allocating memory. Also your structure usage is incorrect for C. See this link for information on how to use structures in C.

    Jim

  5. #5
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Hey dob should be character or structure same Socialsecurity and the way u r initializing the firstname and last name is also not proper. You can use pointer to character to store the names..

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    I'm going between C and C++ and this is C++. Sorry for posting in the wrong place.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If this is C++ you should stop using the C-strings and use std::string instead. And as already pointed out all the fields should really be strings.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Need help with this c program about linked list
    By fm_hyudin in forum C Programming
    Replies: 2
    Last Post: 01-17-2009, 12:22 PM
  4. linked list program
    By dlcp in forum C Programming
    Replies: 4
    Last Post: 04-17-2007, 09:58 AM
  5. Linked List program need help
    By ykchua in forum C Programming
    Replies: 3
    Last Post: 08-05-2004, 10:18 AM