I've written a destructor for a doubly linked list which is not working properly. There's a loop to delete the values. The line-of-control comes out of the that loop but the program doesn't finish. I mean, the main does not end.
Never mind, I figured it out.
You can write the destructor cimpler. For example
LinkedList::~LinkedList()
{
for ( ListItem *node = head; node; )
{
ListItem *temp = node;
node = node->next;
delete temp;
}
head = nullptr;
}
Or
LinkedList::~LinkedList()
{
while ( head )
{
ListItem *temp = head;
head = head->next;
delete temp;
}
head = nullptr;
}
First: move delete node; line.
If only head exist it makes error.
LinkedList::~LinkedList() {
ListItem* node = head;
ListItem* temp = NULL;
if (head != NULL && head->next == NULL) {
delete head;
} else {
while (node->next) {
temp = node;
node = node->next;
delete temp;
}
delete node;
}
}
Or remove if statment.
LinkedList::~LinkedList() {
ListItem* node = head;
ListItem* temp = NULL;
while (node->next) {
temp = node;
node = node->next;
delete temp;
}
delete node;
}
Related
int main(){
node** head = nullptr;
push(&head,1);
push(&head,2);
push(&head,3);
printlist(&head);
return 0;
}
void push(node*** head_ptr,int data){
node* new_node = new node;
new_node->data = data;
if(*head_ptr != nullptr){
node* temp = **head_ptr;
while(temp != nullptr){
temp = temp->next;
}
temp->next = new_node;
}
else{
*head_ptr = &new_node;
}
}
Whenever node* new_node = new node; executes it overwrites head_ptr or head.
Is it because new is allocating same memory block everytime?
You assign the address of a local variable, and then use that value after the function has returned, which means your program's behaviour is undefined.
*head_ptr = &new_node;
Having fixed that, you dereference a null pointer, which means your program's behaviour is still undefined.
node* temp = *head_ptr;
while(temp != nullptr){
temp = temp->next;
}
temp->next = new_node; // you have just looped until temp is null
You don't need ***. You shouldn't use **. If you want to modify something, pass it by reference, not pointer.
#include <iostream>
struct node {
int data;
node * next;
};
void printlist(node* n) {
for (; n; n = n->next) {
std::cout << n->data;
}
}
void push(node*& head,int data){
node* new_node = new node{ data, nullptr };
if(head != nullptr){
node* temp = head;
while(temp->next != nullptr){
temp = temp->next;
}
temp->next = new_node;
}
else{
head = new_node;
}
}
int main(){
node* head = nullptr;
push(head,1);
push(head,2);
push(head,3);
printlist(head);
return 0;
}
There are several issues:
There is one level of indirection too much. You return the pointer to new_node but you need to return new_node itself. Once the function has ended, the local variable new_node doesn't exist anymore.
You need to initialize the next pointer to nullptr somewhere. I'm not sure if you did this, because you didn't post all of your relevant code.
while (temp != nullptr) is wrong, once the loop has ended, temp is obviously nullptr but you want the last element of the list. You need while (temp->next != nullptr).
struct node
{
int data;
node* next = nullptr; // initialize next to nullptr
};
void push(node** head_ptr, int data) { // only 2 stars
node* new_node = new node;
new_node->data = data;
if (*head_ptr != nullptr) {
node* temp = *head_ptr;
while (temp->next != nullptr) { // use temp->next instead of temp
temp = temp->next;
}
temp->next = new_node;
}
else {
*head_ptr = new_node; // return new_node instead of a pointer
} // to new_node
}
int main() {
node* head = nullptr;
push(&head, 1);
push(&head, 2);
push(&head, 3);
printlist(&head); // you didn't show printlist
// so there might be problems there too
return 0;
}
Bonuses:
Your code is very inefficient, because for each push you need to find the end of the list which is becoming slower and slower as the list grows. Hint: google schlemiel the painter.
There is room for further improvements, for example having a constructor for node and passing head as reference rather than as pointer to a pointer.
It's been a week since i started learning about linked list and i only managed to learn about singly linked list. So today i implemented the linked list which i learned in c++ and while i tried to run it the code goes into an infinite loop of some random numbers. I tried debugging the code but i coudn't find whats so ever is wrong with the code. The code is below. Help is appreciated.Thanks
#include <iostream>
using namespace std;
struct node{
int data;
node * next;
};
class singly{
private:
node * head,*tail;
public:
singly(){
head=NULL;
tail=NULL;
}
void createNode(int value){
node * temp = new node;
temp->data=value;
temp->next=NULL;
if(head==NULL){
head=temp;
tail=temp;
temp=NULL;
}
else{
tail->next=temp;
tail=temp;
}
}
void display(){
node * temp = new node;
head=temp;
while(temp!=NULL){
cout << temp->data << "\t" << endl;
temp->next=temp;
}
}
void insert_end(int value){
node*newnode = new node;
node*temp = new node;
newnode->data=value;
newnode->next=NULL;
temp=head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next=newnode;
}
void delete_node(){
node*current = new node;
node*previous = new node;
current = head;
while(current->next!=NULL){
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
};
int main(){
singly lists;
lists.createNode(32);
lists.createNode(654);
lists.createNode(34);
lists.createNode(234);
cout<<"\n--------------------------------------------------\n";
cout<<"---------------Displaying All nodes---------------";
cout<<"\n--------------------------------------------------\n";
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Inserting At End-----------------";
cout<<"\n--------------------------------------------------\n";
lists.createNode(55);
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Deleing At End-------------------";
cout<<"\n--------------------------------------------------\n";
lists.delete_node();
lists.display();
}
The member function display does not make sense.
It overwtites the data member head with uninitialized newly created temp.
node * temp = new node;
head=temp;
so the function invokes undefined behavior.
The function can look like
void display()
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
cout << temp->data << "\t";
}
}
Or it is better to define it the following way
std::ostream & display( std::ostream &os = std::cout )
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
os << temp->data << "\t";
}
return os;
}
The data member insert_end is also wrong. It does not take into account that head and tail can be equalto nullptr and does not change them.
The function can be defined the following way
void insert_end(int value)
{
node *newnode = new node { value, nullptr };
if ( tail == nullptr )
{
head = tail = newnode;
}
else
{
tail = tail->next = newnode;
}
}
The member function delete_node firstly does not make sense for a singly-linked list and again is wrong and invokes undefined behavior. The function should remove the first node from the list.
Nevertheless if you want to remove the last node from the list then the function can look like
void delete_node()
{
if ( head != nullptr )
{
tail = nullptr;
node *current = head;
while ( current->next )
{
tail = current;
current = current->next;
}
if ( tail == nullptr )
{
head = tail;
}
else
{
tail->next = nullptr;
}
delete current;
}
}
For starters, display() is wrong. You want the update to be temp = temp->next; and it can also be initialized as node * temp = head hence not requiring the second line.
Your delete_node() can be re-written to:
if (head->next == NULL) // handles the case that it consists of 1 element
{
delete head;
head = NULL;
}
else
{
node *nextToEnd = head;
node *end = head->next;
while (end->next != NULL)
{
nextToEnd = end;
end = end->next;
}
delete end;
nextToEnd->next = NULL;
}
As stated in the comments, review the use of the new keyword
I am writing a simple function to insert at the end of a linked list on C++, but finally it only shows the first data. I can't figure what's wrong. This is the function:
node* Insert(node* head, int data)
{
if (head == NULL) {
head = new node();
head->data = data;
head->link = NULL;
return head;
}
else {
node* temp = head;
while (temp != NULL) {
temp = temp->link;
}
node* temp2 = new node();
temp2->data = data;
temp2->link = NULL;
(temp->link) = temp2;
return head;
}
}
Change the condition in while construct from:
while (temp!=NULL) {
temp=temp->link;
}
To
while (temp->link!=NULL) {
temp=temp->link;
}
In statement, temp->link = temp2, temp is a null pointer. You were dereferencing a NULL pointer.
To append a node at the back, temp pointer should point to the last node of the linked list. So, in the while loop, you need to just stop linked list traversal when you have reached the last node, i.e, the node whose link member points to nothing (has NULL). while (temp->link!=NULL) will stop at the last node as last node will have link member pointing to NULL.
You can simplify your logic by doing this:
void Insert(node **pnode, int data)
{
while (*pnode) {
pnode = &(*pnode)->link;
}
*pnode = new node(data, NULL);
}
assuming you have a node constructor that initializes data and link from arguments.
Instead of calling it as
head = Insert(head, 42);
you'd now do
Insert(&head, 42);
change while(temp!=NULL) to while(temp->link!=NULL)
node* Insert(node* head, int data)
{
if (head == NULL) {
head = new node();
}
else {
while (head->link != NULL) {
head = head->link;
}
head = head->link = new node();
}
head->data = data;
head->link = NULL;
return head;
}
I don't understand why the display() func show me only the first member of the list. I think I did a mess with pointers, but I can't understand where. I have compared this to other linked list source and it seem that the function is written in the-good-way.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
struct Node
{
int Data;
Node * next;
};
void initNode(struct Node *head,int n);
void AddNode(int n,Node* head);
void display(Node* head);
int main()
{
Node * head = new Node;
initNode(head,5);
display(head);
AddNode(10,head);
display(head);
AddNode(15,head);
display(head);
cin.get();
return 0;
}
void AddNode(int n,Node * head)
{
Node * node = new Node;
node->Data = n;
node->next = NULL;
Node * nextNode = head;
while(nextNode)
{
if(nextNode->next == NULL)
{
nextNode->next = node;
}
nextNode = nextNode->next;
}
}
void display(Node * head)
{
while(head)
{
cout << head->Data << " "<<endl;
head = head->next;
}
}
void initNode(struct Node *head,int n)
{
head->Data = n;
head->next = NULL;
}
Your AddNode method is over-complicated. Do something like this to add to the front:
Node *AddNode(int n, Node *head)
{
Node *newNode = new Node;
newNode->Data = n;
newNode->next = head;
return newNode;
}
Or to add to the end:
Node *AddNode(int n, Node *head)
{
Node *newNode = new Node;
newNode->Data = n;
newNode->next = NULL;
if(head == NULL) return newNode;
Node *current = head;
while(current->Next != NULL)
{
current = current->Next;
}
current->Next = newNode;
return head;
}
Doing AddNode this way you will not need initNode. Now you can just day:
Node *head = NULL;
head = AddNode(5, head);
head = AddNode(10, head);
head = AddNode(15, head);
display(head);
Also, you don't need to say struct Node in C++, it is only required in C.
Function AddNode has an infinite loop.
void AddNode(int n,Node * head)
{
Node * node = new Node;
node->Data = n;
node->next = NULL;
Node * nextNode = head;
while(nextNode)
{
if(nextNode->next == NULL)
{
nextNode->next = node;
}
nextNode = nextNode->next;
}
}
Let assume that you have only one element that is the head (after a call of initNode). And as the result head->next = NULL. So inside the body of the loop you make assignment
nextNode->next = node;
Now head->next is not equal to NULL. So after statement
nextNode = nextNode->next;
nextNode caontains the new element. As it is not equal to NULL then iteration of the loop will be repeated. Again for the new node its data member next is equal to NULL. And you add it to it itself.
Now you have no any element in the list that would have data member next equal to NULL. So you are unable to add new elements. The last element contains reference to itself.
You could write the function the following way
void AddNode(int n,Node * head)
{
Node * node = new Node;
node->Data = n;
node->next = NULL;
Node * nextNode = head;
while( nextNode -> next ) nextNode = nextNode->next;
nextNode->next = node;
}
But take into account that it is assumed that head is not equal to NULL. otherwise the function will be incorrect. I think that you should redesign you list.
In your AddNode function add a break in the if block.
void AddNode(int n,Node * head)
{
Node * node = new Node;
node->Data = n;
node->next = NULL;
Node * nextNode = head;
while(nextNode)
{
if(nextNode->next == NULL)
{
nextNode->next = node;
break;
}
nextNode = nextNode->next;
}
}
Now it should add properly.
Node * nextNode = head;
while(nextNode)
{
if(nextNode->next == NULL)
{
nextNode->next = node;
}
nextNode = nextNode->next;
}
The problem is this block of code. When you find the end of the list (if nextNode->next == NULL) you need to break out of the loop. Try it with an example to convince yourself.
Each time you add a node you traverse from head to the end of the list
You can change it as follows
void AddNode(int n){
Node *node=new Node;
node->data=n;
node->next=NULL; //head is global
if(head==NULL){
t=head=n;
}
else{
t->next=n; //t is global
t=t->next;
}
}
I am not able to figure out, why is my code to insert into a sorted doubly linked list failing on some test cases.Please let me know. I dont know of the test cases, they are system generated.
Node* SortedInsert(Node *head,int data)
{
// Complete this function
// Do not write the main method.
Node * temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
{
head = temp;
return head;
}
if (temp->data <= head->data)
{
temp->next = head;
head->prev = temp;
head = temp;
return head;
}
Node *curr = head;
while (curr->next!=NULL)
{
if (temp->data <= curr->data)
{
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
curr = curr->next;
}
curr->next = temp;
temp->prev = curr;
return head;
}
Thanks
Once you reach the last node, you should again compare its data with the new node and insert accordingly.
curr->next = temp;
temp->prev = curr;
return head;
}
If execution reaches this part, then at present curr is pointing to the last node. Now you should again compare its data like the following:
if (temp->data <= curr->data)
{ // insert before last node
curr->prev->next = temp;
temp->prev = curr->prev;
temp->next = curr;
curr->prev = temp;
return head;
}
// else insert at the end.
curr->next = temp;
temp->prev = curr;
return head;
}
Alternatively, you can write an if condition for node at the end of the list
Node* SortedInsert(Node *head,int data)
{
struct Node* p = head;
struct Node* q = NULL;
struct Node* r = new Node;
r->data=data;
r->prev=NULL;
r->next=NULL;
if(p==NULL){
p=r;
head=p;
}
else if(p!=NULL&&p->data>r->data){
p->prev=r;
r->next=p;
p->next=NULL;
head = r;
}
else{
p=head;
while(p!=NULL) {
if(p!=NULL&&p->data>r->data){ //If node is not at the end of list and smaller than some node
(p->prev)->next = r;
r->next = p;
r->prev = p->prev;
p->prev=r;
return head;
}
else if(p->next==NULL) //If node is at the end of list
{
p->next = r;
r->prev = p;
r->next = NULL;
return head;
}
else{}
p=p->next;
}
}
return head;
}
/*
* For reference:
*
* link to the problem :-
* https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
*
* DoublyLinkedListNode {
* int data;
* DoublyLinkedListNode* next;
* DoublyLinkedListNode* prev;
* };
*
* Function to insert a node in sorted DLL with given data & return head
*/
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode** prev = &head;
DoublyLinkedListNode* newNode = new DoublyLinkedListNode(data);
while ( *prev && ((*prev)->data <= data) ) {
prev = &(*prev)->next;
}
newNode->prev = *prev;
newNode->next = *prev;
*prev = newNode;
return head;
}
This solution works in all cases (insert in between, insert at the head, insert at the end etc). I was just trying to write a code without any conditions. While searching for some error, I ended up on this SO thread. Do correct me if this solution doesn't work for some test cases.
This is my code that passed all the test cases.
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
DoublyLinkedListNode *temp = head;
if (!temp) {
temp = new DoublyLinkedListNode(data);
head = temp;
return head;
}
while (temp->data < data && temp->next != nullptr) {
temp = temp->next;
}
if (temp->data > data) {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp;
if (temp == head) {
head = t;
temp->prev = t;
return head;
}
temp->prev->next = t;
t->next = temp;
temp->prev = t;
} else {
DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
t->next = temp->next;
temp->next = t;
temp->next->prev = temp;
if (t->next != nullptr) t->next->prev = t;
}
return head;
}
Here is some code in order to Insert a Node in Sorted Doubly Linked List
Node* SortedInsert(Node *head,int data)
{
struct Node *temp;
struct Node *newNode = new Node();
newNode->data=data;
newNode->next=NULL;
newNode->prev=NULL;
temp=head;
if(head==NULL)
{
head = newNode;
}
else
{
while(temp!=NULL)
{
if(temp->data<data&&temp->next!=NULL)
{
temp=temp->next;
}
else
{
if(temp->next==NULL&&temp->data<data)
{
temp->next = newNode;
newNode->prev = temp;
break;
}
else if(temp->prev==NULL&&temp->data>data)
{
newNode->next=temp;
temp->prev = newNode;
head=newNode;
break;
}
else
{
newNode->next = temp;
newNode->prev = temp->prev;
temp->prev->next =newNode;
temp->prev=newNode;
break;
}
}
}
}
return head;
}
Here is the complete C++ program for inserting a node at appropriate position in a sorted doubly linked list:
void InsertInSortedDoublyLinkedList()
{
Node *head;
head = CreateDoublyLinkList(3);
PrintDoublyLinkedList(head);
head = SortedInsert(head, 6);
printf("\n Linked list after insertion in sorted order\n");
PrintDoublyLinkedList(head);
}
void PrintDoublyLinkedList(Node *head)
{
Node *temp = head;
printf("NULL -> ");
while (temp)
{
printf("%d ->", temp->data);
temp = temp->next;
}
printf("NULL");
}
Node* CreateDoublyLinkList(int numberOfNodes)
{
Node *head = NULL, *temp, *temp1;
int startingValue = 3;
if (numberOfNodes == 0)
{
return head;
}
temp = (Node*)malloc(sizeof(Node));
temp->data = startingValue;
temp->next = NULL;
temp->prev = NULL;
head = temp;
startingValue += 2;
numberOfNodes--;
for (; numberOfNodes > 0; numberOfNodes--, startingValue += 2, temp = temp->next)
{
temp1 = (Node*)malloc(sizeof(Node));
temp1->data = startingValue;
temp1->next = NULL;
temp1->prev = temp;
temp->next = temp1;
}
return head;
}
Node* SortedInsert(Node *head,int data)
{
Node *temp = NULL, *temp1,*newNode;
int nodeInserted = 0;
if (head == NULL)
{
head = (Node*) malloc(sizeof(Node));
head->data = data;
head->next = NULL;
head->prev = NULL;
}
else
{
if (head->data > data)
{
//insertion need to take place before head itself.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
head->prev = newNode;
head = newNode;
}
else
{
temp1 = head;
temp = head ->next;
while (temp)
{
if (temp->data > data)
{
//we need to insert the node before temp
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = temp;
temp1->next = newNode;
temp->prev = newNode;
nodeInserted = 1;
break;
}
temp1 = temp;
temp = temp->next;
}
if (!nodeInserted)
{
//node insertion need to take place at tail.
newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = temp1;
newNode->next = NULL;
temp1->next = newNode;
}
}
}
return head;
}
struct Node
{
int data;
Node *next;
Node *prev;
}
Node* SortedInsert(Node *head,int data)
{
Node* p1,*p2;
int n=10;
p2=(Node*)malloc(sizeof(struct Node));
p2->next=NULL;
p2->prev=NULL;
p2->data=data;
p1=head;
if(p1==NULL)
return p2;
while(p1->next!=NULL)
{
if(p1->data<data)
p1=p1->next;
else
break;
}
/*Three cases arise when p1->next == NUll i.e. we are end of list
case 1: insert after the end
case 2: insert in between last and second last node
case 3: insert node at the beginning of the list
*/
if(p1->next==NULL)
{
if(p1->data<data)
{
p2->prev=p1;
p1->next=p2;
}
else if(p1->data > data && p1->prev!=NULL)
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
head=p2;
}
}
// Here we have only one case where new node is inserted between two nodes of the list
else
{
p2->next=p1;
p2->prev=p1->prev;
if(p1->prev!=NULL)
p1->prev->next=p2;
p1->prev=p2;
}
return head;
}