What's the proper way to create a static sentinel node - c++

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);
}

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.

Why do we require Node* x = new Node() instead of Node x;

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();
};

Different type of Node created

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);

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.

Differences between struct and class and constructing a node for double linked lists?

I'm attempting to create my own double linked list for learning experience. My book showed the node struct below and I was wondering is that equivalent to my Node class I created? Is that function in the struct just a type of constructor assigning values to each data type in the struct?
//===== Struct =====
struct Node
{
Node *next;
Node *prev;
std::string val;
Node(const std::string &value, Node *nextVal = NULL, Node *prevVal = NULL) :
val(value), next(nextVal), prev(prevVal) {}
};
//===== Class ====
class Node
{
public:
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL);
virtual ~Node(void);
protected:
Node *next;
Node *prev;
std::string val;
};
Node(std::string value = "", Node *pVal = NULL, Node *nVal = NULL)
{
next = nVal;
prev = pVal;
val = value;
}
Yes - that's exactly what it is.
Here's a page with an example of a struct constructor.
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++Structures.html
This is called a constructor initializer list and it is meant to initialize the attributes of a struct or a class.
This is usually the preferred way of initializing attributes. here's a discussion explaining why:
Is it possible to defer member initialization to the constructor body?
Long story short, if you don't explicitly initialize an attribute in the initializer list, it is initialized implicitly using the default constructor and therefor, you will be initializing the variable twice.
Also, you need accessors for your pointer.
class Node
{
public:
Node():next(NULL),prev(NULL),val("") {};
Node(std::string value):next(NULL),prev(NULL),val(value) {};
Node(std::string value, Node *pVal):next(NULL),prev(pVal),val(value) {};
Node(std::string value, Node *pVal, Node *nVal):next(nVal),prev(pVal),val(value) {};
virtual ~Node(void);
std::string getValue()
{
return val;
}
void setValue(std::string v)
{
val = v;
}
Node * getNext()
{
return next;
}
void setNext(Node * n)
{
next = n;
}
Node * getPrevious()
{
return prev;
}
void setPrevious(Node * n)
{
prev= n;
}
protected:
Node *next;
Node *prev;
std::string val;
};