Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <iostream>

using namespace std;


struct node
{
int data;
node *next;
node *prev;
};
class Dlink_list
{
private:
node *head,*tail;
public:
Dlink_list()
{
head = NULL;
tail = NULL;
}
void insert_front(int value)
{
node *n = new node();
if(head == NULL)
{
head = n;
tail = n;
}
else
{
n->data = value;
n->next = head;
n->prev = NULL;
head->prev = n;
head = n;
}
}
void add(int value)
{
node *n = new node();
n->data = value;
n->prev = NULL;
n->next = NULL;
if(head == NULL)
{
head = n;
tail = n;
}
else
{
tail->next = n;
n->prev = tail;
tail = n;
}
}
void insert_after(node *a,int value)
{
node *n = new node();
if(head == NULL)
{
head = n;
tail = n;
}
else
{
n->data = value;
n->next = a->next;
n->prev = a;
a->next = n;
}
}
void del(node *a)
{
if(head == NULL)
{
cout<<"List is Empty"<<endl;
}
else
{
node *b, *c; // for temporary links
b = a->prev; //Pointing to Previous node
b->next = a->next; //changing address of next of previous node
c = a->next; //Getting location of node next to node to be deleted node
c->prev = b; //Changing Previous part Node next to which is being
deleted
}
}
node *get_head()
{
return head;
}
void display()
{
if(head == NULL)
{
cout<<"List is Empty"<<endl;
}
else
{
node *n = new node();
n = head;
while(n != NULL)
{
cout<<n<<endl;
cout<<n->prev<<" ";
cout<<n->data<<" ";
cout<<n->next<<endl;
cout<<endl;
n = n->next;
}
}
}
};
int main()
{
Dlink_list list;
list.add(14);
list.add(15);
list.add(16);
list.add(17);
list.add(18);
cout<<"Values are inserted in Doubly linked list \n ";
cout<<endl;
cout<<"\nvalues in Doubly linked list\n\n";
list.display();
cout<<endl;
cout<<"Deleting Node"<<endl;
list.del(list.get_head()->next);
cout<<"\n after deleting a node list values are\n\n";
list.display();
/*
cout<<endl;
cout<<"\nafter inserting a node in front of list\n";
list.insert_front(11);
cout<<endl;
list.insert_after(list.get_head()->next->next->next,22);
cout<<endl;
list.display();
*/
return 0;
}

You might also like