link list

dfdsfds
Last edited on
Good for you! Now... what do you want us to do with it?

-Albatross
dfdsfds
Last edited on
simply, the base idea looks like this.

1
2
3
4
5
6
7
8
9
10
// class AccountBase -- this can be used too...
struct AccountBase
{
public:
        string firstname;
        string lastname;
        double balance;
        AccountBase *NextAccount; // the pointer for the linked list.
};


I have seen this problem a dozen times on this board since I have been participating on it. this is the base structure of most Linked Lists. How you approach this from here is choices you need to make and that is program design.

Last edited on
THANK you for your reply.

When I declare the struct

1
2
3
4
5
6
7
8
struct AccountBase
{
public:
        string firstname;
        string lastname;
        double balance;
        AccountBase *NextAccount; // the pointer for the linked list.
};


and when i read in the list of stuff from the input file I did this

1
2
3
for(count = 0; !feof(file); count++){
        fscanf(file,"%s %s %lf \n", firstname, lastname, &balance);
    } 


it give me an error, what am I doing wrong
Are we doing c++ or c? They are two different approaches and this is a c++ board.

I didn't expect to see the c style read of a file for a c++ program.

Do you know the concepts of a Linked List? Do you realize you are doing dynamic Allocations, and would have different language syntax to handle that read?
This program is in C language. I know the concept behind linked list. so this is what I did so far with the problem am having above and i still get an error

1
2
3
4
5
6
 struct account{
        char  first[MAX_LENGTH];
        char  last[MAX_LENGTH];
        double balance;
        struct account *next; 
};


and to read in the file i did this

1
2
3
4
5
6
struct account people [MAXCHAR]
	
for(count = 0; !feof(file); count++){
		fscanf(file,"%s %s %lf \n", people.last, people.first, &people.balance);
}
		 

Last edited on
what I see in the code pieces you have supplied is the structure that looks like it is defined correctly.

1
2
   struct account people [MAXCHAR]; // this doesn't make any since, do we want a linked list or array?
   account people[MaxChar];  // this would set up an array of Accounts. 


A linked list would track the Head of the list, like:
1
2
3
4
5
6
7
8
9
10
// this indicates I have a empty list, no elements.
account *HeadAccount = NULL; // I don't remember if this is legal in c99....

// creating the head element would look like, this is the dynamic allocation
// I would expect something similar for every iteration of account in the file I read.  But it would be on the next pointer....
HeadAccount = malloc(sizeof(account));

//accessing the head account would look like
//it would look similar for each iteration of the file but doing it on a working pointer.
fscanf(file,"%s %s %lf \n", HeadAccount->last, HeadAccount->first, HeadAccount->balance);


Lets see if you understand what I put there.

Last edited on
dfds
Last edited on
closed account (zb0S216C)
johnhuge wrote:
when i print the code it doesn't execute (sic)

Care to be more specific?

On a side note, I would rearrange you structure (for padding reasons), like so:

1
2
3
4
5
6
7
struct accountNode
{
    double balance;
    struct accountNode *next;
    char lastName[MAX_LENGTH ];
    char firstName[MAX_LENGTH];
};

Of course, this ia a recommendation but it's also beyond the scope of your question, so this is optional :)

Wazzak
Last edited on
Topic archived. No new replies allowed.