#include "stdafx.h"
#include<iostream>
using namespace std;
class Link
{
public:
int data;
Link *next;
Link *previous;
};
Link *head;
class List
{
private:
Link *first;
public:
List()
{
first=NULL;
}
void insert_before(int d)
{
Link *temp1=new Link;
temp1->data=d;
temp1->next=NULL;
temp1->previous=NULL;
if(first==NULL)
{
first=temp1;
}
else
{
Link *temp2=first;
temp1->next=temp2;
temp1->previous=NULL;
first=temp1;
temp2->previous=temp1;
}
}
void insert_at(int index,int d)
{
Link *temp1=first;
//for(int i=1;i<index-1;i++)
int i=1;
while(i<index && temp1->next!=NULL)
{
i++;
if(first==NULL)
{
cout<<"\nLink not found!!!\n";
break;
}
/* else if(temp1->next==NULL)
{
cout<<"\nInvalid Index!!!\n";
break;
}*/
else
{
//i++;
temp1=temp1->next;
Link *temp2=new Link();
temp2->data=d;
temp2->next=temp1->next;
temp1->next=temp2;
}
}
/*Link *temp2=new Link();
temp2->data=d;
temp2->next=temp1->next;
temp1->next=temp2;*/
}
void insert_end(int d)
{
if(first==NULL)
{
first=new Link();
first->data=d;
first->previous=NULL;
first->next=NULL;
}
else
{
Link *temp=first;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new Link();
temp->next->data=d;
temp->next->previous=temp;
temp->next->next=NULL;
}
}
void remove_before()
{
if(first==NULL)
{
cout<<"\nList is empty,,,Sorry your request can't be completed!!!"<<endl;
}
else
{
Link *temp1=first;
first=first->next;
first->previous=NULL;
delete temp1;
}
}
void remove_specific(int r)
{
Link *temp,*previous;
temp=first;
while(temp!=NULL)
{
if(first->data==r)
{
first=first->next;
delete temp;
cout<<"Value deleted!!!"<<endl;
}
else
{
break;
}
temp=first;
}
temp=first;
previous=temp;
while(temp!=NULL)
{
Link *next;
next=temp->next;
if(temp->data==r)
{
previous->next=temp->next;
delete temp;
cout<<"Value deleted!!!"<<endl;
temp=previous;
}
previous=temp;
temp=temp->next;
}
}
void remove_at(int index)
{
Link *temp1=first;
//for(int i=1;i<index-1;i++)
int i=1;
while(i<index-1)
{
i++;
if(temp1==NULL)
{
cout<<"\nLink not found!!!"<<endl;
}
else
{
temp1=temp1->next;
}
}
Link *temp2=temp1->next;
temp1->next=temp1->next->next;
delete temp2;
}
void remove_end()
{
if(first==NULL)
{
cout<<"\nNode not found"<<endl;
}
else
{
Link *temp1=first;
Link *temp2=temp1;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
delete temp1;
temp2->next=NULL;
}
}
void display()
{
Link *temp;
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
void display_reverse()
{
Link *temp=first;
if(first==NULL)
{
cout<<"List is Empty"<<endl;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->previous;
}
//cout<<temp->data<<"\t";
}
}
bool search(int search)
{
Link *temp=first;
while(temp->next!=NULL)
{
if(temp->data==search)
{
return true;
}
temp=temp->next;
}
}
/*int recur_display(Link *show)
{
Link *temp=NULL;
Link *first=NULL;
if (show->next == NULL)
{
return -1;
}
if(head!=NULL)
{
head->next=temp;
}
head=temp;
head->next = NULL;
head=first;
show=show->next;
recur_display(show);
cout <<show->data<<"\t";
}*/
};
int main()
{
List s1;
int choice;
do
{
cout<<"\n\t\t\t-----Main Menu-----\n\n";
cout<<"\t1)Insert End\t\t\t2)Insert At\n\t3)Insert Start\t\t\t4)Remove Remove Start\n\t5)Remove Specific Node\t\t6)Remove At\n\t7)Remove End\t\t\t8)Search Node\n\t9)Display\t\t\t10)Reverse Display\n\n\t\t\t\t11)Exit\n";
cout<<"\n\n\t\tPlease Select your Choice (1-10)\n";
cin>>choice;
if(choice==1)
{
int value;
cout<<"\nEnter an Integer:\n";
cin>>value;
s1.insert_end(value);
//s1.insert_before(value);
cout<<"\nLink created Successfully!!!\n";
}
else if(choice==2)
{
int index,value;
cout<<"\nEnter an Integer:";
cin>>value;
cout<<"\nNow enter its Index:";
cin>>index;
s1.insert_at(index,value);
cout<<"\nLink created Successfully!!!\n";
}
else if(choice==3)
{
int value;
cout<<"\nEnter an Integer:\n";
cin>>value;
//s1.insert_end(value);
s1.insert_before(value);
cout<<"\nLink created Successfully!!!\n";
}
else if(choice==4)
{
s1.remove_before();
cout<<"\nLink removed Successfully!!!\n";
}
else if(choice==5)
{
int value;
cout<<"\nEnter a specific value to delete:\n";
cin>>value;
s1.remove_specific(value);
}
else if(choice==6)
{
int index;
cout<<"\nEnter an Index:\n";
cin>>index;
s1.remove_at(index);
cout<<"\nLink removed Successfully!!!\n";
}
else if(choice==7)
{
s1.remove_end();
cout<<"\nLink removed Successfully!!!\n";
}
else if(choice==8)
{
int search;
cout<<"\nEnter an Integer to Search:\n";
cin>>search;
if(s1.search(search)==true)
{
cout<<"\n"<<search<<" is present in List!!!\n";
}
else
{
cout<<"\n"<<search<<" is not present in List!!!\n";
}
}
else if(choice==9)
{
cout<<"\nList in Actual order is...\n";
s1.display();
//s1.recur_display(head);
}
else if(choice==10)
{
cout<<"\nList in Reverse order is...\n";
s1.display_reverse();
}
else
{
exit(0);
}
}while(choice!=11);
return 0;
}
No comments:
Post a Comment