Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I was going through dsa doubly circular list, and was practicing insertion of elements to it, after entering the first element my program ends abruptly.....is there a mistake in my insertafterfunction.i get a segmentation fault error .just help me through I cant find what has gone wrong..
#include<iostream>
struct Node{
int data;
Node* next;
Node* prev;
};
Node* headnode;
void insert(int data){
Node* newnode = new Node;
newnode->data= data;
newnode->next=headnode;
newnode->prev=headnode;
headnode=newnode;
}
void insertafter(int data){
Node* newnode=new Node;
newnode->data=data;
newnode->next=headnode;
Node* existingnode = headnode;
while(existingnode->next!=headnode){
existingnode=existingnode->next;
}
existingnode->next=newnode;
newnode->prev= existingnode;
headnode->prev=newnode;
}
void printnode(){
Node* newnode=headnode;
while (newnode->next!=headnode){
std::cout<<newnode->data<<"->";
newnode=newnode->next;
}
std::cout<<"\n";
}
int main(){
headnode=NULL;
int x,data;
std::cin>>x;
for(int i=0;i<x;i++)
{
std::cin>>data;
if(i==0)
{
insert(data);
}
else
{
insertafter(data);
}
printnode();
}
}
For example this while loop within the function insertafter
while(existingnode->next!=headnode){
existingnode=existingnode->next;
}
invokes undefined behavior because after calling the function insert data members prev and next of the head node are equal to nullptr.
See the function insert
void insert(int data){
Node* newnode = new Node;
newnode->data= data;
newnode->next=headnode; // here headnode is equal to nullptr
newnode->prev=headnode; // And here headnode is equal to nullptr
headnode=newnode;
}
It seems you mean at least the following function definition
void insert(int data){
Node* newnode = new Node;
newnode->data= data;
headnode=newnode;
newnode->next=headnode;
newnode->prev=headnode;
}
That is after calling the function data members prev and next of the head node will point to the head node itself.
In general the separate function insert does not make a sense because it may be called (provided that it will be written correctly) only once.
Also the function printnode will output nothing if the list contains only the head node due to the condition in the while statement
Node* newnode=headnode;
while (newnode->next!=headnode){
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Why my code is giving an Infinite loop and i have checked my code many times and I don't know where the mistake is but my output is 34 in an infinite loop and it is a code for doubly linked list!
So please tell me where the mistake is as well as why my loop is showing 34 instead of 77 in my code and is there any problem in my print function?
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node *prev;
};
class Linked
{
public:
Node *head;
Node *tail;
Linked()
{
head=NULL;
tail=NULL;
}
void add(int data,int position)
{
Node *n=new Node;
Node *n1=new Node;
n->data=data;
n->next=NULL;
n->prev=NULL;
if(head==NULL)
{
head=n;
tail=n;
cout<<"Linked list formed"<<endl;
}
if(position==0)
{
head->prev=n;
n->next=head;
head=n;
cout<<"Linked list created"<<endl;
}
else
{
n1=head;
int posi=position;
while(posi>1)
{
n1=n1->next;
posi--;
}
n->next=n1->next;
n->next->prev=n;
n1->next=n;
n->prev=n1;
}
}
void print()
{
Node *n=new Node;
n=head;
while(n!=NULL)
{
cout<<n->data<<endl;
n=n->next;
}
}
};
int main()
{
Linked l;
l.add(34,0);
l.add(77,0);
// l.add(44,1);
// l.add(90,2);
l.print();
return 0;
}
add (data,position) has 2 problems.
For the very first node in Doubly Linked List , once head and tail are pointed to the same node, we are done. Position do not matter, when there is no node.
So stop add logic after "Linked List Formed". Add Else and move rest all code inside, It works as expected.
Code should be able to assert Head-> Prev and Tail-> next to be null.
Also while adding a single new node, you do not want to allocate memory for 2x Nodes.
Do not call new Node () for n1.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Here's my C++ code of a simple structured linklist.
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node* prev;
Node(){
data=-1;
next=NULL;
prev=NULL;
}
Node(int d,Node *nnext){
data=d;
next=nnext;
}
void add(Node* nnext){
next=nnext;
nnext->prev=this;
}
};
void print(Node* head){
Node* cNode;
cNode=head;
while (cNode!=NULL){
cout <<"["<<cNode->data<<"]" << endl;
cNode=cNode->next;
}
}
void insertAfter(Node* pNode, Node* nNode){
nNode->next = pNode->next;
pNode->next = nNode;
pNode->next->prev = nNode;
}
void deleteNode(Node* b){
Node* c=b->next;
Node* a=b->prev;
a->next=c;
c->prev=a;
delete b;
}
void main(){
Node* head;
head=new Node();
head->data=1;
Node * currentNode=head;
for (int i=2;i<=5;i++){
Node* nNode=new Node(i,NULL);
currentNode->add(nNode);
currentNode=nNode;
}
cout << currentNode->data << endl;
print(head);
insertAfter(head, new Node(99,NULL));
//deleteNode(currentNode);
print(head);
}
The case checking is unnecessary because I just need the concept of the linklist. If you have another version of these kind of simple linklist code, please let me know! Thank you!
Your deleteNode() function does not check whether c and a are non-NULL, but immediately dereferences them. This means that your program will crash if you try to delete the first or last node of the list.
Your insertAfter() function also sets pointers in the wrong order, causing nNode->prev to point to itself.
Your deleteNode function is not taking care of the case when you delete the "head" element.
Let's take this list as an example:
head -> n1 -> n2 -> n3 (each of this nodes also has a link to the node before it, just as you did in your code)
If you call deleteNode(head), the following will happen:
Node* c=b->next; // b = head, c = head.next = n1
Node* a=b->prev; // b = head, a = head.prev = NULL
a->next=c; // a = NULL, then NULL->next
You can't access to a variable of a NULL object, that's why you are receiving that runtime error.
P.S: If you want to improve you understanding of data structures I recomend you this book: https://mitpress.mit.edu/books/introduction-algorithms
I am aware that "Reversing a doubly linked list" has been asked and answered before, e.g.:
Reversing a Doubly Linked List
But my question a little different.
All the methods that I could find online use a "current node (curr)" iterator and do the swapping on that as follows:
Node* Reverse(Node* head)
{
Node* curr=head;
while(curr)
{
swap(curr->next, curr->prev);
head=curr;
curr=curr->prev;
}
return head;
}
where the node is of type:
struct Node
{
int data;
Node *next;
Node *prev
}
Now the question is, I tried to simplify that code by omitting the curr iterator since it looks totally unnecessary to me. Here is the new code:
Node* Reverse(Node* head)
{
while(head)
{
swap(head->next, head->prev);
if(head->prev) head=head->prev;
}
return head;
}
This works on paper however When I test it in an online compiler I get time limit exceeded error: http://www.mycodeschool.com/problems/reverse-a-doubly-linked-list
I believe that we can make all the swapping on the head node and then iterate it to the prev node.
Do you see any logical problems with this code?
Node* Reverse(Node* head)
{
while(head)
{
swap(head->next, head->prev);
if(head->prev) head=head->prev;
else break; // !!!
}
return head;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
In this case, I need to implement addFront() method which is to keep adding a integer in front of a linked-list.
class Node{
public:
int data;
Node* next;
Node* prev;
}
class List {
void addFront(int item);
protected:
Node* dummyNode;
int numItems; //number of item is the linkedlist
};
Below is what I tend to implement addFront():
void addFront(int data){
Node* head = new Node();
if(numItems == 0) //if no item in the list.
{
//Set the head node to be dummyNode
head = dummyNode;
//Because the next node the head is pointing to is NULL.
head -> next = NULL;
}
//Create a new node.
Node* newNode = new Node();
//Set value
newNode->data = data;
//Let the previous pointer of dummyNode points to newNode.
head->prev = newNode;
//Re-set head to be newNode.
head = newNode;
numItems++;
}
Am I doing correctly? If not, why? If yes, is there any better way to do this?
I won't go into too many details as this appears to be a homework assignment, but the short answer is, no.
Node* head = new Node();
if(numItems == 0) //if no item in the list.
{
//Set the head node to be dummyNode
head = dummyNode;
//...
}
You have a memory leak in the code above.
First of all name dummyNode which denotes the beginning of the list looks strange. It would be much better to substitute it for head. Also you need a variable that will point to the tail of the list.
As for your function then it is simple
void addFront( int data )
{
Node *head = new Node();
head->data = data;
head->next = dummyNode;
dummyNode->prev = head;
dummyNode = head;
numItems++;
}
Also it would be not bad if class Node had a constructor with parameters that to accept data and pointers. Class list also has to have an explicitly defined default constructor or its data members have to be initialized when they are defined.
Insert is a method which appends an item to the end of my linked list.
Can't figure out how to code for the case where Node is null, and I just want to add to it.
struct Node{
int data;
Node *next;
Node(int data):data(data),next(NULL){}
void insert(int data){
if (this==NULL){
this=new Node(data); // compiler is complaining here.
// how would I go about setting the value of this (which is presently NULL) to a new Node?
}
}
}
you can not assign a value to this pointer which is a special keyword and should always point to a valid block of memory. by looking at your usage, could you be trying to mean this:
void insert(int data){
if (!next){
next = new Node(data);
}
Something like this:
void insert(int data)
{
Node* newNode = new Node(data);
if (next!=NULL)
newNode->next = next;
next = newNode;
}
You cannot assign directly to 'this'; what you need to consider is how to represent an empty list, most likely by:
Node* head = 0;
So you add the first node by
head = new Node(data);