News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Linked Lists and Typdef Struct

Started by maestroh, December 14, 2006, 05:54:05 PM

Previous topic - Next topic

maestroh

I'm trying to create a linked list, and due to the lack of support of 'typdef struct' I'm having trouble determining how I should code the list.

Here is some sample code which is similar to what I'm trying to do.

struct node
{
   char a;
   node *next;
};

typedef struct node *head;

int main()
{
  head na;
  na = new node;
  //na->   //This line doesn't display any members of the node struct.
}


Are there any workarounds to the typedef struct problem? Or am I just not defining 'node' and 'head' correctly?

Thanks


MortenMacFly

Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

maestroh

Hey Morton,

I'm glad you replied. I did find your post, and I was excited when I did. But when I went to implement the code, I realized that the name Flex is ambiguous becuase the name is used for both the struct definition and the type definition. If you change the type definition to something else, your code doesn't work. If you take the typedef out entirely is works the same as if the typedef were left in with the ambiguous definition. Once I realized that I was back at square one.

Any other suggestions?

Thanks

MortenMacFly

Quote from: maestroh on December 14, 2006, 06:26:17 PM
Any other suggestions?
If you insist to name a typedef different than the struct (why?) then unfortunately no, not at the moment.
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

maestroh

Quote
If you insist to name a typedef different than the struct (why?)

The reason I want to name the typedef different than the struct is to create a type that is a pointer to the struct type.

From my example:

struct node
{
   char a;
   node *next;
};

typedef struct node *head;



'head' is a pointer to the 'node' type. I guess my question is: Is there any way to create a type that's a pointer to the struct?

Thanks again. I appreciate your help Morten.


dmoore

sorry if this sounds insulting, but is the lack of code completion support the main reason why few people seem to use the STL (or boost for that matter)? I wouldn't have thought anyone would need to hand code their own linked lists in this day in age.
Python plugins: [url="https://github.com/spillz/codeblocks-python"]https://github.com/spillz/codeblocks-python[/url]
Code::Blocks Daily Builds -- Ubuntu PPA: [url="https://launchpad.net/~damien-moore/+archive/codeblocks"]https://launchpad.net/~damien-moore/+archive/codeblocks[/url]

maestroh

To be honest I haven't used the STL before. But I'll look into it, and I guess that would solve my problem.

dmoore

#7
sample code using the list container type from Josuttis (an STL god): http://www.josuttis.com/libbook/cont/list1.cpp.html

see also queue, deque, stack. also plenty of web resources of varying quality for the STL such as http://www.cppreference.com/index.html (personally, I use a couple of reference books)
Python plugins: [url="https://github.com/spillz/codeblocks-python"]https://github.com/spillz/codeblocks-python[/url]
Code::Blocks Daily Builds -- Ubuntu PPA: [url="https://launchpad.net/~damien-moore/+archive/codeblocks"]https://launchpad.net/~damien-moore/+archive/codeblocks[/url]

maestroh

Thanks dmoore. That gives me some direction.