0% found this document useful (0 votes)
71 views3 pages

Double Lisk List Sir

This C++ program defines a Doubly Linked List data structure with methods to insert nodes at the front and end of the list, insert a node after a given node, delete a node, get the head node, and display the list. It includes a main function that tests the list functionality by adding several nodes, deleting one node, and displaying the list before and after deletion.

Uploaded by

Shaista Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Double Lisk List Sir

This C++ program defines a Doubly Linked List data structure with methods to insert nodes at the front and end of the list, insert a node after a given node, delete a node, get the head node, and display the list. It includes a main function that tests the list functionality by adding several nodes, deleting one node, and displaying the list before and after deletion.

Uploaded by

Shaista Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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