Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using linked list

Status
Not open for further replies.

ykfc

Programmer
Mar 6, 2004
66
AU
Although I've programmed a lot in different languages but I'm new to VC++. A typical requirement is: I need to build a list for some customers. Each customer record has a list of item purchased. So I think I could define a customer record as:

typedef struct CustRcd {
char *name;
int num_purchases;
struct Purchases *purchased_item;
//points to the first item
struct CustRcd *nextCust;
} CustRcd;

typedef struct Purchases {
char purchaseItemName[LINESIZE];
struct Purchases *nextItem;
} Purchases;

Assume a customer could be appended to the list before making the first purchase. Question is: should I make purchased_item point to NULL when the customer has not purchased anything? I have tried to use this method but I experienced some problems. Eventually I end up a solution by making each newly added customer purchase one dummy item!! I think I've missed something but still struggling with no idea. Can someone enlighten me?

If the above question is hard to comprehen, can someone point me to a link (something like a tutorial/exercise with an answer) where it could show me how codes should be structured? Thanks a lot.
 
>I need to build a list for some customers

You can have a list for free by using STL. You can also get string management for free.

Code:
#include <list>
#include <string>

struct Purchase {
    std::string mName; 
};

typedef std::list<Purchase> Purchases;

struct Customer {
    std::string mName;
    Purchases mPurchases; 
} ;

typedef std::list<Customer> Customers;

See
for info on what you can do with it.


/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
I would set purchased_item to NULL if the customer hasn't purchased anything. What problems did you get into by doing this? I'm thinking, maybe when you access purchased_item in other parts of the code, you aren't checking for NULL before trying to dereference it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top