Double Linked List
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
struct node *prev;
struct node *next;
};
void insertBegin();
void insertEnd();
void insertLoc();
void deleteAtBeg();
void deleteEnd();
void deleteSpecific();
void display();
int findLength();
struct node *head = NULL;
int main()
{
int choice;
while (1)
{
cout << "Enter your choice to perform opreation on Double Linked List\n";
cout << "\n1.Insert Element at begining\n2. insert at end\n3.insert at pos\n4.delete at beg\n5.delete at end\n6.delete at position\n7.display\n6.find length\n8.exit\ns";
cout << "Enter your choice\n";
cin >> choice;
switch (choice)
{
case 1:
insertBegin();
break;
case 2:
insertEnd();
break;
case 3:
insertLoc();
break;
case 4:
deleteAtBeg();
break;
case 5:
deleteEnd();
break;
case 6:
deleteSpecific();
break;
case 7:
display();
break;
case 8:
cout << "Length of Double linked list is : \n\n", findLength();
break;
case 9:
exit(0);
default:
cout << "Please Enter a valid choice\n";
}
}
}
void insertBegin()
{
int data;
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
cout << "Please Enter Data which you want to Insert \n";
cin >> data;
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
temp->next = head;
head->prev = temp;
head = temp;
}
cout << " inserted into double linked list \n and Linked list is shown as: ";
cout << data << endl;
display();
}
void insertEnd()
{
int data, len = 1;
struct node *temp, *p;
temp = (struct node *)malloc(sizeof(struct node));
cout << "Enter data which you want to insert \n";
cin >> data;
temp->data = data;
temp->prev = NULL;
temp->next = NULL;
p = head;
if (p == NULL)
{
head = temp;
}
else
{
while (len < findLength())
{
p = p->next;
len++;
}
p->next = temp;
temp->prev = p;
}
cout << "inserted into double linked list \n and Linked list is shown as\n"
<< data;
display();
}
void insertLoc()
{
int pos, len = 1, data;
struct node *temp, *p;
temp = head;
cout << "Enter Location where do you want to insert node \n";
cin >> pos;
if (pos > findLength())
{
cout << "Invalid location \n";
}
else if (pos == 1)
{
insertBegin();
}
else
{
while (len < pos - 1)
{
temp = temp->next;
len++;
}
p = (struct node *)malloc(sizeof(struct node));
cout << "Enter data which you want to insert int doubly linked list\n";
cin >> data;
p->data = data;
p->next = temp->next;
temp->next->prev = p;
temp->next = p;
p->prev = temp;
cout << "inserted into double linked list \n and Linked list is shown as\n"
<< data;
display();
}
cout << "\n";
}
void deleteAtBeg()
{
struct node *temp;
if (head == NULL)
{
cout << "List is Empty";
}
else
{
temp = head;
if (temp->prev == temp->next)
{
head = NULL;
free(temp);
}
else
{
head = head->next;
head->prev = NULL;
free(temp);
}
cout << "\nDeletion successful";
}
}
void deleteEnd()
{
struct node *temp;
if (head == NULL)
cout << "List is Empty" << endl;
else
{
temp = head;
if (temp->prev == temp->next)
{
head = NULL;
free(temp);
}
else
{
while (temp->next != NULL)
temp = temp->next;
temp->prev->next = NULL;
free(temp);
}
cout << "\nDeletion successful";
}
}
void deleteSpecific()
{
int pos;
cout << "enter the position: " << endl;
cin >> pos;
if (head == NULL)
{
cout << "List is empty!" << endl;
}
else
{
struct node *temp = head;
if (pos == 1)
{
deleteAtBeg();
}
else
{
int loc = 1;
while (loc < (pos) && temp != NULL)
{
loc++;
temp = temp->next;
}
if (temp == NULL)
{
cout << "Desired position does not exist!!\n";
}
struct node *p = temp->prev;
p->next = temp->next;
if(temp->next);
temp->next->prev = p;
free(temp);
}
}
cout << "succesful " << endl;
}
void display()
{
struct node *temp;
temp = head;
if (temp == NULL)
{
cout << "List is Empty \n";
}
while (temp != NULL)
{
cout << "<-- " << temp->data << "-->";
temp = temp->next;
}
cout << "\n\n";
}
int findLength()
{
int count = 0;
struct node *temp;
temp = head;
if (temp == NULL)
{
cout << "List is Empty \n";
}
while (temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}