#include<iostream>
using namespace std;
class linkedlist
{
private:
struct node
{
int element;
node* next;
}*head;
public:
linkedlist(); //constructor
void insertatbig(int element);
void insertatend(int element); //function to insert node
bool isempty(); //function to check list is empty
int remove(int position); //function to delete element
void display(); //function to display content
void searchelement(int element);
void searchatpos(int pos) ;
void searchanddelete(int element);
void reverse();
void selfadjustinglist(int key);
void selforganizinglist(int key);
void swapnode(int i,int j);
void swapelement(int i,int j);
void splitlist();
void sortnode();
void sortelement();
void copy(node* head);
void nthfromlast();
void middle();
void loop();
// void merge(node*head,node* l2.head);
// void comparetwolist(node* l1.head,node*l2.head);
~linkedlist(); //distructor
};
linkedlist::linkedlist()
{ head=NULL;
}
void linkedlist::insertatbig(int element)
{node* t=new node;
t->element=element;
t->next=head;
head=t;
}
void linkedlist::insertatend(int element)
{
if(isempty()){cout<<"ram";
head=new node;
head->element=element;cout<<"shyam";
head->next=NULL; return;}
else{
node* curr;
curr=head;
while(curr->next!=NULL)
{curr=curr->next;
}
node* t=new node;
t->element=element;
t->next=NULL;curr->next=t;
return;
}}
bool linkedlist::isempty()
{
if(head==NULL){
return true;}
else{
return false;}
}
int linkedlist::remove(int position)
{
if(isempty())
{
cout<<"\n list is empty no deletion possible\n";
return 0;
}
int data;
node*curr=head;
int count=0;
if(position==1)
{
data=curr->element;
head=head->next;
delete(curr);
return data;
}
else{
while(count<position-2){
curr=curr->next;
count++;}
node* t=curr->next;
if(curr->next->next!=NULL){
curr->next=curr->next->next;
cout<<"\nramayan\n";
}
else if(curr->next->next==NULL)
curr->next=NULL;
data=t->element;
cout<<"\n"<<data;
delete(t);
return(data);
}}
void linkedlist::display()
{if(isempty())
{
cout<<"\n list is empty no display possible\n";
return;
}
node*curr=head;
while(curr!=NULL)
{
cout<<curr->element<<" -> ";
curr=curr->next;
}
}
void linkedlist::searchelement(int element)
{if(isempty())
{
cout<<"\n list is empty no seraching possible\n";
return;
}
node* curr;
int pos=1;
curr=head;
while(curr!=NULL)
{
if(curr->element==element){
cout<<"element"<<element<<"found at"<<pos<<"!!!!!";
return;}
curr=curr->next;
pos++;
}
cout<<"\nsorry element is not present in linked list!!!";
}
void linkedlist::searchatpos(int pos)
{
if(isempty())
{
cout<<"\n list is empty no searching possible\n";
return;
}
if(pos<=0){
cout<<"\n you entered wrong position it should be greater then 0\n"; return;}
int count=1;
node* curr=head;
while(curr!=NULL)
{if(count==pos){
cout<<"data at"<<pos<<"is"<<curr->element;return;}
curr=curr->next;
count++;
}
cout<<"\n you enter wrong position list is smaller then your position\n";
}
void linkedlist::searchanddelete(int element )
{if(isempty())
{cout<<"\n list is empty";return;}
if(head->element==element)
{node* curr=head;
head=head->next;
delete(curr);
}
node *curr,*ncurr;
curr=head;
ncurr=head->next;
while(ncurr!=NULL)
{
if(ncurr->element==element)
{
curr->next=ncurr->next;
delete(ncurr);
cout<<"\n "<<element<<"deleted successfully";return;
}
curr=curr->next;
ncurr=ncurr->next;
}
cout<<"\n element not present \n";
}
void linkedlist::reverse()
{
if(isempty()){cout<<"\n list is empty";return;}
node* curr;
curr=head->next;
node* rhead;
head->next=NULL;
while(curr!=NULL)
{rhead=curr;
curr=curr->next;
rhead->next=head;
head=rhead;
}
display();
}
void linkedlist::selfadjustinglist(int key)
{if(isempty()){cout<<"\n list is empty";return;}
node* curr;
curr=head->next;
node* pcurr=head;
if(head->element==key)
return;
while(curr!=NULL)
{
if(curr->element==key)
{
if(curr->next!=NULL)
pcurr->next=curr->next;
else pcurr->next=NULL;
curr->next=head;
head=curr;
}pcurr=curr;
curr=curr->next;
}
display();
}
void linkedlist::selforganizinglist(int key)
{if(isempty()){cout<<"\n list is empty";return;}
}
linkedlist::~linkedlist()
{
node* curr=head;
while(head!=NULL)
{
curr=head;
head=head->next;
delete(curr);
}
}
int main()
{char c;
int a;
linkedlist l;
cout<<"*************!!!!WELCOME TO LINKED LIST OPERATION!!!!*************\n\t-------------------------------------------------------------------\n";
do
{
cout<<"\t\t \t hello user what u want to do \n ------------------------------------------------------------ \n ";
cout<<"\n\t\tpress 1 to insert at beginning\n\t\tpress 2 to insert at end\n\t\tpress 3 to search an element\n\t\tpress 4 to find elemnt at a position\n\t\tpress 5 to delete an elemnt\n\t\tpress 6 to display the list\n \t\tpress 7 to search and delete element\n\t\tpress 8 to reverse linked list\n";
cout<<"\t\tpress 9 to check self adjusting list\n";
cin>>a;
switch(a)
{case 1:cout<<"\n enter the list element to be added at begining \t";
int b;
cin>>b;
l.insertatbig(b);
break;
case 2:cout<<"\n enter the list element to be added at end\t";
int c;
cin>>c;
l.insertatend(c);break;
case 3:cout<<"\n enter the element you want to search in the list\t";
int d;
cin>>d;
l.searchelement(d);
break;
case 4:cout<<"\n enter the position whose elemet you want to find\t";
int e;
cin>>e;
l.searchatpos(e);
break;
case 5:cout<<"\n enter the position of element you want to delete from list\t";
int f;
cin>>f;
l.remove(f);
break;
case 6:cout<<"\n-----------------------------------------------------------------------\n\t";
l.display();
cout<<"\n----------------------------------------------------------------------";
break;
case 7:
cout<<"\n enter the element you want to search and delete:";
int g;
cin>>g;
l.searchanddelete(g); break;
case 8:cout<<"\n-----------------------------------------------------------------------\n\t";
l.reverse();
cout<<"\n----------------------------------------------------------------------";break;
case 9:
cout<<"\n enter the element want to search\n";
int h;
cin>>h;
cout<<"\n-----------------------------------------------------------------------\n\t";
l.selfadjustinglist(h);
cout<<"\n----------------------------------------------------------------------";break;
default:
cout<<"\n you have not selected appropriate choice see menu again/n";
}
cout<<"\n do you want to perform more operation if yes press y";
cin>>c;}
while(c=='y');
return 0;
}
No comments:
Post a Comment