1

I am implementing Priority Queue of int in C. I have a struct Node in my header

typedef struct NodeTag {
  int priority;
  int value;
  struct Node *next;
}Node;

Now in my C file I am trying to add a new element while traversing the queue and comparing the priorities values of each element inside with the new one. So I try to do this

Node *prev = head;
while(prev->next && prev->next->priority >= priority){
   prev=prev->next;
}
temp->next=prev->next;
prev->next=temp;

But I get a compiler error saying:

incomplete definition of type 'struct Node'

On my while condition. How to access the property of priority of the next node of prev?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ada
  • 41
  • 1
  • 4
  • 1
    This is a common mistake, especially in beginner's code here on SO. The difficulty is finding a good question to make this one a duplicate of. – Jonathan Leffler Oct 22 '15 at 02:32
  • Can take a look about "struct" in C language. try use struct Node *prev = head [browser](http://stackoverflow.com/questions/2242696/c-structure-and-c-structure) – Jerry Oct 22 '15 at 02:37
  • actually if you ever try to read c std you will know why `struct` in a type is for – Jason Hu Oct 22 '15 at 02:37
  • And another very relevant Q&A is [typedef a structure containing a pointer to the same type](https://stackoverflow.com/questions/7474774). – Jonathan Leffler Oct 22 '15 at 03:08

2 Answers2

2

I think you might have mixed up which is the type name, and which is the struct name.

See here.

Either declare the next field as Node * or as struct NodeTag *.

Here's a compiling snippet:

typedef struct NodeTag Node;
struct NodeTag {
  int priority;
  int value;
  Node *next;
};
Community
  • 1
  • 1
Kenney
  • 9,003
  • 15
  • 21
  • Thank you! When I declare it as Node * I get an error saying "must use 'struct' tag to refer to type 'Node'" – ada Oct 22 '15 at 02:22
  • @Aida updated answer. I had to split the typedef off from the struct, otherwise it doesn't know type Node yet for the `next` field. – Kenney Oct 22 '15 at 02:32
  • @AidaZhumabekova: There are multiple ways to fix it, including (1) `typedef struct Node { int priority; int value; struct Node *next; } Node;` and (2) `typedef struct Node Node; struct Node { int priority; int value; Node *next; };` and (3) `typedef struct NodeTag { int priority; int value; struct NodeTag *next; } Node;` — any of which will work just fine. – Jonathan Leffler Oct 22 '15 at 02:34
  • @JonathanLeffler I'm afraid that my suggestion *"or as `struct NodeTag`"* in the answer has the same problem as (3): `prev=prev->next` would assign a `struct NodeTag` to type `Node`, which the compiler probably won't like... – Kenney Oct 22 '15 at 02:38
  • Once the semicolon at the end of (3) is reached, `Node` is an alias for `struct NodeTag` and there is no problem. The difficulty is when you're in the middle, as in (1), where the is no type `Node` at the point when `next` is defined, so you have to use `struct Node` in that example. (2) gets around the problem by saying `Node` is an alias for `struct Node`, and then defining what a `struct Node` is. The structure body can use just `Node` (the typedef name) as shown, or `struct Node`; the result is the same. – Jonathan Leffler Oct 22 '15 at 02:44
1
typedef struct NodeTag {
  int priority;
  int value;
  struct Node *next; /*this should be struct NodeTag, 
                      at this point Node is still not yet typed*/

  }Node; /*now it is*/

to get over this you should do this:

typedef struct Node Node;

struct Node{
     int priority;
     int value;
     Node *next; 
 };
milevyo
  • 2,165
  • 1
  • 13
  • 18