In C++, having trouble with this code. It will add properly but not delete the n
ID: 3838910 • Letter: I
Question
In C++, having trouble with this code. It will add properly but not delete the nodes properly it just exits out of the program and I can't see why. Any help is appreciated.
#include<bits/stdc++.h>
#include<string>
#include<fstream>
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*head=nullptr,*tail=nullptr;
/*
Class Declaration
*/
class double_llist
{
public:
void add_head(int value);
void add_tail(int value);
void delete_head();
void delete_tail();
void display_dlist();
void printHeadNodeValue();
void printTailNodeValue();
};
/*
* Main: Conatins Menu
*/
int main()
{
int choice, element;
double_llist dl;
int elem;
ifstream file;
file.open("textFile.txt");
int nod;
while(file>>nod){
dl.add_tail(nod);
}
file.close();
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Add a node at Head"<<endl;
cout<<"2.Add a node at tail"<<endl;
cout<<"3.Delete from head"<<endl;
cout<<"4.Delete from tail"<<endl;
cout<<"5.Print the node from back to forth"<<endl;
cout<<"6.Print the head node value"<<endl;
cout<<"7.Print the tail node value"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.add_head(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.add_tail(element);
cout<<endl;
break;
case 3:
if (head == NULL)
{
cout<<"List empty,nothing to delete"<<endl;
break;
}
dl.delete_head();
cout<<endl;
break;
case 4:
if (tail == NULL)
{
cout<<"List empty,nothing to delete"<<endl;
break;
}
dl.delete_tail();
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
case 6:
dl.printHeadNodeValue();
break;
case 7:
dl.printTailNodeValue();
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
void double_llist::add_head(int value)
{
struct node *temp;
temp = new(struct node);
temp->next = nullptr;
temp->info = value;
temp->prev = nullptr;
if (head == nullptr)
{
head = tail = temp;
}
else{
head->prev = temp;
temp->next = head;
head = temp;
}
}
void double_llist::add_tail(int value){
struct node *temp;
temp = new(struct node);
temp->prev = nullptr;
temp->info = value;
temp->next = nullptr;
if(tail == nullptr){
head = tail = temp;
}
else{
temp->prev = tail;
tail->next = temp;
tail = temp;
}
}
void double_llist::delete_head(){
if(head = nullptr)
cout<<"linked list is empty"<<endl;
else{
struct node *temp;
if(head->next == nullptr){
temp = head;
head = nullptr;
free(temp);
}
else{
temp = head;
head = head->next;
head->prev = nullptr;
free(temp);
}
}
}
void double_llist::delete_tail(){
if(tail = nullptr)
cout<<"linked list is empty"<<endl;
else{
struct node *temp;
if(tail->prev == nullptr){
temp = tail;
tail = nullptr;
free(temp);
}
else{
temp = tail;
tail = tail->prev;
tail->next = nullptr;
free(temp);
}
}
}
/*
* Display elements of Doubly Link List
*/
void double_llist::display_dlist()
{
struct node *q;
if (tail == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q = tail;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
cout<<q->info<<" <-> ";
q = q->prev;
}
cout<<"NULL"<<endl;
}
void double_llist::printHeadNodeValue(){
if(head == nullptr)
cout<<"There is no element to print"<<endl;
else
cout<<"Head node value is : "<<head->info<<endl;
}
void double_llist::printTailNodeValue(){
if(tail == nullptr)
cout<<"There is no element to print"<<endl;
else
cout<<"Tail node value is : "<<tail->info<<endl;
}
Explanation / Answer
In functions delete_head() and delete_tail() , you used a assignment operator
change if(head = nullptr) to if(head==nullptr)
Also define nullptr