#include
using namespace std;
class Node
{
public:
int data;
Node *prev, *next;
Node()
{
data=NULL;
prev=NULL;
next=NULL;
}
};
class dll
{
private:
Node *header;
public:
dll(){
header=NULL;
}
void createList();
void displayList();
void insertEnd();
void deleteFirstNode();
void deleteLastNode();
void deleteNthNode();
};
void dll::deleteFirstNode()
{
if(header)
{
Node *temp;
temp=header;
header=temp->next;
header->prev=NULL;
delete temp;
}
else
{cout<<"\nThe list is empty!\n";}
return;
}
void dll::deleteLastNode()
{
if(header)
{
Node *temp, *last;
temp=header;
while(temp)
{
last=temp;
temp=temp->next;
}
last->prev->next=NULL;
delete last;
delete temp;
}
else
{cout<<"\nThe list is empty!\n";}
return;
}
void dll::deleteNthNode()
{
if(header)
{
Node *temp, *last;
temp=header;
int position=0, counter=0;
cout<<"Enter the node position that you want to delete: \t";
cin>>position;
if(position>=0)
{
while(counter!=position && temp)
{
last=temp;
temp=temp->next;
counter++;
}
if(temp)
{
cout<<"\nThe node that you want to delete has value:\t\n"<data;
last->prev->next=temp;
temp->prev=last->prev;
delete last;
}
else{cout<<"\nThe list does not contain that many elements\n";}
}
}
return;
}
void dll::insertEnd()
{
if(header==NULL)
{
createList();
}
else
{
Node *newNode, *temp, *last;
newNode=new Node;
cout<<"Enter the value for the node:\t";
cin>>newNode->data;
temp=header;
while(temp)
{
last=temp;
temp=temp->next;
}
newNode->next=NULL; //newNode is the new last node
newNode->prev=last;
newNode->prev->next=newNode;
}
return;
}
void dll::createList()
{
int in_val;
Node *temp;
temp=new Node;
cout<<"Enter the value for the node:\t";
cin>>temp->data;
if(header == NULL)
{
//This is the first node in the double linked list
temp->next=NULL;
temp->prev=NULL;
}
else
{
//If this is a new node at the front of an already populated linkedlist
temp->next=header;
temp->next->prev=temp;
header=temp;
}
header=temp;
return;
}
void dll::displayList()
{
Node *temp, *last;
temp=header;
cout<<"Display the list from first to last using the NEXT pointer.\n";
while(temp)
{
cout<data<<"\t"<<endl;
last=temp;
temp=temp->next;
}
/*if(last)
{
cout<<"Display the list from last to last using the PREV pointer.\n";
while(last)
{
cout<data<<"\t"<<endl;
last=last->prev;
}
}*/
}
void main()
{
cout<<"******This is a Doubly Linked List Demo******"<<endl;
int choice=0;
dll objDLL;
do{
cout<<"Enter the opration you want to perform: ";
cout<<"\n1). Add a node at front of the list";
cout<<"\n2). Display a node";
cout<<"\n3). Add a node at end of the list";
cout<<"\n4). Delete the node at head of the list";
cout<<"\n5). Delete the node at tail of the list";
cout<<"\n6). Delete the node at nth position of the list";
cout<<"\n7). Exit\t";
cin>>choice;
switch(choice)
{
case 1: objDLL.createList(); break;
case 2: objDLL.displayList(); break;
case 3: objDLL.insertEnd(); break;
case 4: objDLL.deleteFirstNode();break;
case 5: objDLL.deleteLastNode();break;
case 6: objDLL.deleteNthNode();break;
case 7: exit(0);
default: cout<<"\n Invalid operation/input detected. Please try again later.";
}
}while(choice!=7);
}