code A:
#include<iostream>
class node{
private:
node* nxt;
public:
node(){}
void push();
};
void node::push(){
node* newNode = new node();
this->nxt = newNode;
}
int main(){
node hello;
hello.push();
}
code B:
#include<iostream>
class node{
private:
node* nxt;
public:
node(){}
void push();
};
void node::push(){
node* newNode = new node();
this->nxt = newNode;
}
int main(){
node* hello;
hello->push();
}
I don't understand why I'm getting segmentation fault while running code B,
Whereas Code A executes without any issue.
And what's the right way to execute code B without any issue?
In code B, hello node pointer wasn't pointing to any address (segmentation fault) because i didn't initialized it.
instead of
node* hello;
it should have been
node* hello = new node();
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.
class Node
{
public:
int value;
Node *next;
};
class LinkedList
{
private:
Node *head;
public:
LinkedList(void) { head = NULL; }
~LinkedList(void){};
void insertAtHead(int);
void insertAtLocation(int, int);
void Delete(int);
void displayList();
int countList();
};
void LinkedList::insertAtHead(int new_value)
{
struct Node *newNode, *NodePtr = head;
newNode = new Node;
newNode->value = new_value;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
newNode->next = NodePtr;
head = newNode;
}
cout << "Node successfully inserted at the head\n\n";
}
I didn't create any structure for the node, rather I made a class for it. But I don't know why my code is working properly when I make a newNode in the insertAtHead function by writing struct at the start of initialization, What is happening there? no, compile and run time error when the struct is written before Node. What is the concept behind this work?
There is no difference between struct and class that is relevant to this code. So you can switch them around as you wish with no consequences.
This
struct Node *newNode, *NodePtr = head;
is a C-ism. In C++ you would write:
Node *newNode, *NodePtr = head;
Moreover a struct is a class in C++. struct and class are just two different keywords. Only for default access they make a difference when used to define a class. See here for more details on that: When should you use a class vs a struct in C++?.
I have fixed some of the problems but still can't get past the segmentation fault in while loop of append function where the next of temp is updated.i have seen the segmentation fault in debugger when i stepped into the particular line. also a side note while posting here it gives many errors any way i can fix those
#include <iostream>
using namespace std;
class node{
public:
int data;
node* next;
node()
{
data=0;
next=nullptr;
}
node(int value)
{
data=value;
}
void setnext(node* obj)
{
next=obj;
}
};
class linkedlist{
public:
node* head;
node* tail;
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void prepend(int data)
{
node* temp=new node(data);
temp->setnext(head); //updates the new next pointer
head=temp; //updates the head with new address
}
void append(int data)
{
node* temp=new node(data);
if(head==nullptr)
{
cout<<"the linked list is empty";
head=temp;
tail=temp;
temp=nullptr;
}
else {
while (temp!= nullptr)
{
temp = temp->next; //segmentation fault here
}
tail->setnext(temp);
tail = temp; //updates the tail with new address
}
}
void display()
{
node* temp=head;
while(temp!=nullptr)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<"NULL";
}
};
int main()
{
linkedlist obj;
obj.prepend(1);
obj.append(2);
obj.append(3);
obj.display();
}
After creating a new linkedlist, the default constructor of linkedlist should have initialized node* head and node* tail to nullptr.
Thus, the call obj.prepend(1); after linkedlist obj; does not prepend 1 into obj. As a result, node* head and node* tail are still both nullptr.
After that, if you call obj.append(2);, then tail->setnext(temp); results in a Segmentation Fault as tail is nullptr.
You should have checked whether the linkedlist is empty in linkedlist::prepend and linkedlist::append, if so, node* head and node* tail need to be properly initialized.
In your append() function you're using the 'tail' member before you've assigned a value to it.
the following code crashes at runtime but works perfectly fine if struct node* a[10] is declared globally.Where does the problem lie.Any insight would be appreciated.
Thank you!
#include<bits/stdc++.h>
using namespace std;
int rear=-1;
int front=-1;
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newnode(int d){
struct node* node1=new node;
node1->data=d;
node1->left=NULL;
node1->right=NULL;
return(node1);
}
void enqueue(struct node* a[],struct node* tempnode){
rear++;
a[rear]=tempnode;
}
struct node* dequeue(struct node* a[]){
front++;
return a[front];
}
void bfs(struct node* root,struct node* a[]){
struct node *tempnode=root;
while(tempnode){
cout<<tempnode->data;
if(tempnode->left)
enqueue(a,tempnode->left);
if(tempnode->right)
enqueue(a,tempnode->right);
tempnode=dequeue(a);
}
}
main() {
struct node* a[10];
struct node* root=newnode(1);
root->left=newnode(2);
root->right=newnode(3);
root->left->left=newnode(-1);
root->left->right=newnode(0);
bfs(root,a);
}
http://www.geeksforgeeks.org/level-order-tree-traversal/
Initialize the array "a" -
int main() {
struct node* a[10] = {NULL};
The problem is not occurring when struct node* a[10] is declared globally because global variables are initialized automatically.
You forget to initialize a:
struct node* a[10]{};
so your dequeue will indeed return nullptr once your queue is empty.
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.