#include<iostream>
using namespace std;
class node
{
public:
int info;
node *next, *prev;
}*start=NULL,*last=NULL;
void InsertAtBeg(int n)
{
node *temp=new node;
node *ptr=start;
temp->info=n;
temp->prev=NULL;
temp->next=start;
start=temp;
if(ptr==NULL)
{
last=temp;
}
else
{
temp->next->prev=temp;
}
}
void Display()
{
//cout<<"printing list in reverse order";
//node *ptr=last;
node *ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<"->";
//ptr=ptr->prev;
ptr=ptr->next;
}
cout<<"NULLn";
}
void InsertAtLoc(int n,int loc)
{
node *temp=new node;
node *ptr=start;
node *ptr1;
for(int i=0; i<loc-1; i++)
{
ptr1=ptr;
ptr=ptr->next;
}
temp->info=n;
temp->next=ptr1->next;
temp->prev=ptr1;
ptr1->next->prev=temp;
ptr1->next=temp;
}
void InsertAtLast(int n)
{
node *temp=new node;
node *ptr=last;
temp->info=n;
temp->next=NULL;
temp->prev=last;
last=temp;
if(ptr==NULL)
{
start=temp;
}
else
{
temp->prev->next=temp;
}
}
void DeleteFromFirst()
{
node *temp=start;
temp->next->prev=NULL;
start=temp->next;
}
void DeleteFromLast()
{
node *temp=last;
temp->prev->next=NULL;
last=temp->prev;
}
void DeleteFromLoc(int loc)
{
node *ptr=start;
node *ptr1;
for(int i=0; i<loc-1; i++)
{
ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=ptr->next;
ptr->next->prev=ptr1;
}
void Search(int n)
{
int loc=0;
node *ptr=start;
while(ptr!=NULL)
{
loc++;
if(ptr->info==n)
{
cout<<"item found at location "<<loc;
break;
}
else
{
ptr=ptr->next;
}
if(ptr==NULL)
{
cout<<"item doesn't exists in the list";
}
}
}
int main()
{
InsertAtBeg(10);
InsertAtBeg(20);
InsertAtBeg(30);
InsertAtBeg(40);
InsertAtBeg(50);
InsertAtLoc(15,5);
InsertAtLast(5);
DeleteFromFirst();
DeleteFromLast();
DeleteFromLoc(2);
Display();
Search(20);
return 0;
}
0 Comments