Thread: linked list of structure

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    20

    linked list of structure

    The requirement I have to follow is that the base structure should have two string members to hold the last name and first name of the account holder and a double member to hold the account balance.

    The following is in the input file:

    James John 5.50
    Gave Alber 456.67
    Hope Jone 89.43
    Napa Paris 3943.90

    This is in my header file:

    Code:
    struct name{
            char *first[];
            char *last[];
            int    *balance[]; 
    }
    The problem I am having is that how can i read in the above input file into the arrays and dynamically allocate them according to their string count.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Your structure definition is broke.
    2. Do you know what forum you are in?
    3. What language are you programming in?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    The program I am doing is in C program.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Then you should have posted in the C board, not the C# board. Are you attempting to make a linked list? If so, take a read through Lesson 15: Linked List.

    I am sure someone will move this soon, so don't repost.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    Thanks for the reply. If you could answer a quick question. How would i read the input file to the following and store it to the array first[], last[], balance and allocate enough memory to hold the info. in from the input file.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You actually need to define the size of your char arrays in your structure. Just make them something big, such as 100. As for your balance just define that as a number. Use something like:
    Code:
    struct node{
         char first[100];
         char last[100];
         double balance;
         struct node *next;
    };
    For the file operations take a look at File operations.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,792
    Moved to C board

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by johnhuge View Post
    Thanks for the reply. If you could answer a quick question. How would i read the input file to the following and store it to the array first[], last[], balance and allocate enough memory to hold the info. in from the input file.
    How many people you got on that list?

    Arrays are only good if you have a known, small number of records to work with... up to say, 100 or so.
    Linked lists are good when you have a small to medium but unknown number of records ... again up to maybe 200 or so.
    What you really need for any kind of reasonable implementation is random access filing... handle tens of thousands of records with a single struct.

    Plus, your struct is broken to begin with...
    Code:
    struct name{         
        char *first[];         
        char *last[];         
        int    *balance[];  
    }
    Means you have to allocate memory for every first and last name... which can be a major source of errors in linked lists and won't work at all in random access files... You probably also want to store an account number so...

    Redo your struct like this...
    Code:
    struct name{         
        char first[32];
        char last[32];
        unsigned int account;
        int  balance;  }
    Now your structure has built in storage for the strings, an account number, and the balance value. Because of the last bit ambiguity problem with floats and doubles, it is often better to work with integers for financial records, and do all your "accounting" in pennies...
    Last edited by CommonTater; 09-19-2011 at 10:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list inside a structure
    By jerepops in forum C Programming
    Replies: 5
    Last Post: 12-14-2009, 08:22 PM
  2. structure and a linked list help
    By mikecool291 in forum C Programming
    Replies: 1
    Last Post: 05-10-2009, 10:39 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. what does a linked list structure look like?
    By MalickT in forum C Programming
    Replies: 9
    Last Post: 05-26-2008, 05:19 PM
  5. Linked list w/ structure
    By 5rjK in forum C Programming
    Replies: 10
    Last Post: 12-12-2006, 06:18 AM