Author Topic: Linked Lists and Typdef Struct  (Read 6669 times)

maestroh

  • Guest
Linked Lists and Typdef Struct
« on: December 14, 2006, 05:54:05 pm »
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.
Code
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


Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Linked Lists and Typdef Struct
« Reply #1 on: December 14, 2006, 06:13:26 pm »
A search through the forum would have revealed that: ;-)
http://forums.codeblocks.org/index.php?topic=4523.msg35742#msg35742
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

maestroh

  • Guest
Re: Linked Lists and Typdef Struct
« Reply #2 on: December 14, 2006, 06:26:17 pm »
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

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Linked Lists and Typdef Struct
« Reply #3 on: December 14, 2006, 06:29:57 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: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

maestroh

  • Guest
Re: Linked Lists and Typdef Struct
« Reply #4 on: December 14, 2006, 07:59:15 pm »
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:
Code
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.


Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Linked Lists and Typdef Struct
« Reply #5 on: December 14, 2006, 08:03:29 pm »
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.

maestroh

  • Guest
Re: Linked Lists and Typdef Struct
« Reply #6 on: December 14, 2006, 08:25:22 pm »
To be honest I haven't used the STL before. But I'll look into it, and I guess that would solve my problem.

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: Linked Lists and Typdef Struct
« Reply #7 on: December 14, 2006, 08:37:31 pm »
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)
« Last Edit: December 14, 2006, 08:42:21 pm by dmoore »

maestroh

  • Guest
Re: Linked Lists and Typdef Struct
« Reply #8 on: December 14, 2006, 09:13:13 pm »
Thanks dmoore. That gives me some direction.