Linkedlist Node in C+ - c++

I am learning a book on data structures, and complied their node in linked list example, and I receive this error:
and Everything.cpp|7|error: expected unqualified-id before "int"|
and Everything.cpp|7|error: expected `)' before "int"|
||=== Build finished: 2 errors, 0 warnings ===|
The code for the node is:
typedef struct Node
{
struct Node(int data) //Compile suggest problem is here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;
I am not familiar with structs and I am using Code::blocks to compile. Does anyone know whats wrong?

The code sample is wrong. There should not be the keyword struct in front of the constructor declaration. It should be:
typedef struct Node
{
Node(int data) // No 'struct' here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;

Related

why cannot we initialize a node without using a pointer?

I have recently started learning data structure and as a beginner, I have a query while implementing linked list nodes, why do we have to initialize node using a pointer only?
class node{
public:
int data;
node* next;
node(int val){
data = val;
next = NULL;
}
};
int main(){
node* head = NULL;
node head = NULL; // this throws an error which i cannot understand
}
Actually you can initialize the node by value. If you want to initialize a node with value, according to your constructor node(int val), you have to code like below:
class node{
public:
int data;
node* next;
explicit node(int val){
data = val;
next = NULL;
}
};
int main(){
int value = 777;
//node* head = NULL; // Initialize head pointers to null
node head(value); // By your constructor definition
}
EDIT: By the way, marking a constructor as explicit is a very good habit to have, as it prevents unexpected conversion, as Duthomhas commented.

C++ struct was not declared this scope

Using struct like linked list creates problem.
When I declare 2 values (my struct),
it returns error:
this VALUE_NAME was not declared this scope
This is part of the source.
struct _node
{
int val;
_node* prev;
_node* next;
};
typedef struct _node node;
node* HEAD;
void deleteALLNode()
{
node* node = HEAD;
node* _tmp;
//
// here _tmp value return error
//
}
You cannot name your variable with same name, as type (you can one time maybe, but than you cannot use the type without compilation error anymore). Just change name of node* node = HEAD; variable to something else, like node* pNode = HEAD;
If it's C++, you can use _node directly, no need to typedef. typedef a struct is C style.
struct _node
{
int val;
_node* prev;
_node* next;
};
_node* HEAD;
void deleteALLNode()
{
_node* node = HEAD;
_node* _tmp;
}

Linked List in C

Heres a snippet from the code I am trying to complete for building a linked list. for some reason I keep getting the error "error: expected ‘;’, identifier or ‘(’ before ‘struct’ " when trying to compile the code. Can someone help me out.
struct node;
struct node* buildList(int x);
void push(struct node** headRef, int data);
int findLen(struct node** headRef);
struct node{
int data;
struct node* next;
}
struct node* buildList(int x){
struct node* head = NULL;
head = malloc(sizeof(struct node));
head->data = x;
head->next = NULL;
return head;
}
Try putting a semicolon after the struct declaration
struct node{
int data;
struct node* next;
};

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.
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;
};
To be exact, it creates an object from given struct in given scope. Word 'variable' is a too generic term.

new operator (Error :expected expression before struct)

struct node* NewNode(int data)
{
struct node* node = new(struct node);
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
I am getting this error in first line of the function. Cant figure out whats wrong?
Thanks.
The "new" keyword hints at this being C++. In C++ the "struct TYPENAME" construct is largely obsolete, you can simply use TYPENAME instead.
The C way of typedefing a type name from a named struct is implicit in C++.
node* NewNode(int data)
{
node* pnode = new node;
pnode->data = data;
pnode->left = NULL;
pnode->right = NULL;
return(pnode);
}
should work just fine if this is C++. Please note that using the same name for a type and a variable is not a good idea. Some naming convention (hungarian or anything) helps.
This code compiles perfectly fine under Comeau try-it-out:
#define NULL 0
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* NewNode(int data)
{
struct node* node = new(struct node);
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}