Brian Var
Brian Var

Reputation: 6227

Declaration of a doubly linked list in a structure

Is this the correct procedure in declaring a doubly linked list within a structure.I'm using the structure to read in a text file if this has any relevance.

#define MAX 20
//struct to order contactList
struct contact
{
    string firstName[MAX],lastName[MAX],email[MAX],companyName[MAX];
    long phoneNum[MAX];
    struct listelement *link
    struct node *next;
    struct node *prev;

};

Upvotes: 0

Views: 1292

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

Probably not. Normally, you'd use struct contact *next; struct contact *prev; since the items in the list are (probably) struct contact and not struct node.

Hence, I'd expect to see:

#define MAX 20
//struct to order contactList
struct contact
{
    string              firstName[MAX];
    string              lastName[MAX];
    string              email[MAX];
    string              companyName[MAX];
    long                phoneNum[MAX];    /* 20 phone numbers per contact — wow! */
    struct listelement *link;             /* Semi-colon added.  What is this for? */
    struct contact     *next;
    struct contact     *prev;
};

The email element of the structure is too short (my main email address wouldn't fit, for example; you need 32 bytes at least, I suggest). That's an awful lot of phone numbers for a single contact. Also, long will not store all 10-digit phone numbers.

It is not clear what the link element is for. It presumably is part of a singly-linked list involving these structures.

Upvotes: 4

Related Questions