On geeks for geeks I saw a different way to create the Node for linked list.
struct Node{
int data;
Node* next;
Node(int x){
data = x;
next = NULL;
}
}
Can someone please explain me how that node is defined.
struct Node {
int data;
Node *next;
Node(int x) : data(x), next(NULL) {}
};
This is just a way to define structure with constructor in C++
You can use them simply like this
Node *node = new Node(4);
Related
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.
When I create a Node for Linked List, Stack, Queue, Trees, etc.
I have to create the NODE using
Node* x = new Node()
But instead, we can create the object for the Linked List, etc. using
LinkedList x;
for
class Node {
int data;
Node* next;
};
class LinkedList {
Node* head = NULL;
int size = 0;
bool push();
int pop();
};
I want to have a single instance of a sentinel node that other nodes can use as part of their constructor. I tried
struct Node;
static Node sentinel(0);
struct Node{
Node(int val): next(&sentinal), val(val) {}
Node *next;
int val;
};
int main() {
Node *node = new Node(42);
}
This doesn't work because the
'Node sentinel' has initializer but incomplete type
What is the proper way to create a single sentinel node for multiple value nodes?
You can make your sentinel a static member of Node.
struct Node{
Node(int val): next(&sentinel), val(val) {}
Node *next;
int val;
static Node sentinel;
};
Node Node::sentinel(0);
int main() {
Node *node = new Node(42);
}
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.
I have written a simple LinkedList class. I first have a Node class:
class Node
{
public:
Node* next;
int value;
Node(int val)
{
value = val;
next = NULL;
}
Node(int val, Node* y)
{
value = val;
next = y;
}
}
then implementation for LinkedList is straightforward, with a Node* head member and a addNode(int value) member function.
What are other methods to implement a linked list? could give other such implementations or hint at relevant doc?
Thanks and regards.
The standard library defines a doubly-linked list implementation you can use (see here, for example). I'd advise using that unless you have a very good reason not to.
Boost has some implementations:
http://www.boost.org/doc/libs/1_51_0/doc/html/intrusive/slist.html
http://www.boost.org/doc/libs/1_51_0/doc/html/intrusive/list.html
deleteNode
findNode
Mabe create an iterator.
Also better to use initialisation lists in the constructors and private data members. NULL is for C, use 0 instead.
i.e.
class Node
{
private:
Node* next;
int value;
public:
Node(int val) : next(0), value(val) {}
Node(int val, Node *n) : next(n), value(val) {}
int getVale() { return value}
};