Linked lists in C and C++.

  • From: "Graham Hardy" <graham.hardy@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 10 Oct 2007 16:37:45 -0700

Hi all,

I am creating a linked list in a C++ program, which I can envision as
follows.

typedef struct
{
 /* ... */
 token_t *next;
}
  token_t;

(Yes, this list only needs to be linked in one direction.) Clearly the
problem is that when I declare the pointer to the next object, though it is
a pointer of the type of the members in my list, since that type has not
been declared yet, I don't think I can do that. My first instinct told me to
use void pointers. Alternatively, I suppose I could declare the struct
giving the typename after the keyword 'struct' and before the '{', as,
struct token_t { /* ... */ }; Does this method change anything in the way
that types are concerned? That is, what is the diference between struct A
{/*...*/}; and typedef struct {/*...*/} B;?

And one more question. Despite the 'size' of a given data type being
explicit (as, say, a char being 1 byte), I am led to believe that where the
actual heap is concerned the alignment is not quite the same, such as 4 or 8
bytes for a char. I believe one can use the directives such as--

#pragma pack (...)

or something akin to that. If so, how does the syntax of this pack function
work? It seems that it begins with a keyword of some sort, as to whether the
previous alignment should be retained for the future, and then a new value.
Could someone enlighten me?

Thanks in advance,

Graham.

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: