Steven Meow De Claude
Steven Meow De Claude

Reputation: 380

Creating Struct node on c++

I having some doubt with struct.

  struct node
    {
        int data;
        node* next;
    }node; <---- what does this actually do? 

Thanks.

add on::

Hi, trying to fix this error..

Line 11: error: expected constructor, destructor, or type conversion before '*' token compilation terminated due to -Wfatal-errors.

#include <iostream>

using namespace std;

struct node
{
    int data;
    node* next;
}node;

node* nodeNew(int newData, node* newNext) // line 11
{
    node* n= new node;
    n->data= newData;
    n->next= newNext;
    return n;
}

void listPrint(node* p)
{
    while( p!=NULL )
    {
        cout << p->data << " "; p= p->next;
    }
}

int main()
{


}

Is happens when i add that "node" in the struct.

Upvotes: 1

Views: 34643

Answers (2)

Patrick Collins
Patrick Collins

Reputation: 10574

The final line:

}node;

creates a variable with the type struct node, named node. It's equivalent to:

  struct node {
        int data;
        node* next;
   };
   struct node node;

EDIT: In response to your edit, the line:

node* nodeNew(int newData, node* newNext)

is erroring because node isn't a type. Either change it to:

struct node* nodeNew(int newData, struct node* newNext)

or change the structure declaration to:

  typedef struct node node;
  struct node {
        int data;
        node* next;
   };

Upvotes: 4

Doonyx
Doonyx

Reputation: 590

To be exact, it creates an object from given struct in given scope. Word 'variable' is a too generic term.

Upvotes: 1

Related Questions