Ok, since I had trouble implementing and populating my created lists (list within a list) and since I didn't find any valuable information on the web, I decided, after some hours of sheer struggling, to register an account here and ask for some help before I go on and vent off on my keyboard again.
This is a lab assignment for my Data Strustures course as you will no doubt figure out as you go on reading. Or maybe you've figured it out already. Anyway...
I created a list of accounts (of type Accounts) which holds an account_id as well as a list of Bills (class).
1 2 3 4 5 6
|
class Accounts
{
public:
int account_id;
list<Bills> bills;
};
|
1 2 3 4 5 6 7
|
class Bills
{
public:
int billing_id;
double debt;
int days_left;
};
|
In the main section, I went on and declared the list with the name "accounts".
list<Accounts> accounts;
If I am not mistaken, "accounts" is now a list and contains an "account_id"(int) as well as a list of Bills which by itself holds three more variables : "billing_id(int)", "debt"(double) and "days_left"(int).
"accounts"
__________________
| account_id |
| _____________ |
| | billing_id | |
| | debt | |
| | days_left | |
| |____________ | |
|__________________|
Note that "accounts" can hold information about many accounts and each account can hold many bills. That in mind, I decided to create two iterators. The first iterator, "a", will point to the beginning of the Account list and the second one, b", pointing to the beginning of the Bills list.
list<Accounts>::iterator a = accounts.begin();
list<Bills>::iterator b = a->bills.begin();
So far so good and I went on to the populating part.
Among other things, the program needs to check if an account_id already exists. If not, the program will need to create a new Account block and populate it accordingly. Here lies the problem. Although I create a new block to store the account information :
Accounts* new_account = new Accounts;
new_account->account_id = account_id;
I can't find a way to link a new bill block
inside that account and populate that as well.
1 2 3 4 5
|
Bills* new_bill = new Bills;
new_bill->billing_id = billing_id;
new_bill->days_left = days_left;
new_bill->debt = debt;
//link to the created account??
|
I find it to be a little weird that I can't guide the newly created "new_account" to point to the desired values that are to be populated, either via the "new_account" direclty or via the Accounts:iterator "a".
Several of my tries failed to work:
a->bills->account_id;
(error : expression must have pointer type)
new_account->bills->account_id;
(same error)
new_account->bills = new_bill;
(error : no operator matches these operands).
My question is, how can I create a new block inside a newly created block? In other words, how can I link those blocks together?
My second question is more like a "yes or no" one.
With this code :
1 2 3 4 5 6 7 8 9 10
|
while(a!=accounts.end())
{
if(a->account_id==account_id)
{
// check if an existing billing_id exists
// create a new bill and populate it and bla bla bla
break;
}
a++;
}
|
If I break the while loop, the iterator points to the account block on which there was a match? If so, if I want to perform many searches inside the lists per program run, do I need to reset the iterator to the beginning of the list?
Thanks for reading my long post... (sigh)...