Sunday, December 6, 2020

5. Implement Circular Linked List using templates. Include functions for insertion, deletion and search of a number, reverse the list.

 CMSACOR05P: Data Structures Lab


#include<iostream>


using namespace std;


struct node


{


int data;


node *next;


};


class CLL


{


private:


node *head,*tail;


public:


CLL()


{


head=NULL;


tail=NULL;


}


void addNode(int n)


{


int k;


for(int i=1;i<=n;i++)


{


node *tmp=new node;


cout<<"\nData"<<i<<"= ";


cin>>k;


tmp->data=k;


tmp->next=NULL;


if(head==NULL)


{




head=tmp;


tail=tmp;


tmp->next=head;


}


else


{


tmp->next=head;


tail->next=tmp;


tail=tmp;


}


}


}


void display()


{


node *ptr=head;


while(ptr->next!=head)


{


cout<<" "<<ptr->data;


ptr=ptr->next;


}


cout<<" "<<ptr->data;


}




void search(int k)


{


node *current=head;


int c=0;


bool Flag=false;


while(current->next!=head)


{


if(current->data==k)


{


cout<<"\nSearch Successful !!!";


Flag=true;


break;


}


else


{


current=current->next;




}


c++;


}


if(Flag==false)


{


if(current->data==k)


{


cout<<"\nSearch Successful !!!";


Flag=true;


}


}


if(Flag==true)


{


c++;


cout<<"\n"<<k<<" is in Position "<<c;


}


else


{


cout<<"\n"<<k<<" is not found";


}


}




void Insert(int pos,int x)


{


node *tmp = new node;


tmp->data = x;


tmp->next = NULL;


node *ptr=NULL;


ptr=head;//points 1st element


if(pos==1)


{


head=tmp;//head updated


tmp->next=ptr;


tail->next=head;


}


else


{


for(int i=1;i<pos-1;i++)


{




ptr=ptr->next;


}


node *ptr1=NULL;


ptr1=ptr->next;


ptr->next=tmp;


tmp->next=ptr1;


}


}




void Deletion(int pos)


{


node *temp=head;


if(pos==1)


{


head=head->next;


delete temp;


tail->next=head;


}


else


{


for(int i=0;i<pos-2;i++)


{


temp=temp->next;


}


node *temp2=temp->next;


temp->next=temp->next->next;


delete temp2;


}


}


void reverse()


{


node *pre,*in,*next;


pre=head;//initially


in=NULL;//initially


next=NULL;//initially


while(pre->next!=head)


{


next=pre->next;


pre->next=in;//join




in=pre;//updated


pre=next;//updated


}


pre->next=in;//join


head->next=pre;//last node's next updated


head=pre;//haed updated


}


~CLL()


{


cout<<"\nThank u...";


}


};




int main()


{


cout<<"\n*****Presenting Circular Linked List*****";


CLL ob1;


cout<<"\nThe number of elements u want to insert in the Linked List: ";


int n;


cin>>n;


ob1.addNode(n);


cout<<"\nNow the linked list is: \n";


ob1.display();


while(1){


int ch;


cout<<"\n1.Search An Element\n2.Insertion\n3.Delition\n4.Reversing\n5.exit";


cout<<"\nPlease enter ur Choice: ";


cin>>ch;


switch (ch)


{


case 1:


int k;


cout<<"\nEnter the elment to search: ";


cin>>k;


ob1.search(k);


break;


case 2:


int posI,x;


cout<<"\nEnter the position to insert element: ";


cin>>posI;




cout<<"\nEnter the NEW DATA to insert: ";


cin>>x;


ob1.Insert(posI,x);


cout<<"\nNOW the UPDATED linked list AFTER insertion:\n";


ob1.display();


break;


case 3:


int posD;


cout<<"\nEnter the position u want to DELETE: ";


cin>>posD;


ob1.Deletion(posD);


cout<<"\nNOW the UPDATED linked list AFTER deletion:\n";


ob1.display();


break;


case 4:


ob1.reverse();


cout<<"\nThe reversed Linked List is:\n";


ob1.display();


break;


case 5:


cout<<"End"<<endl;


default:


cout << "Sorry! Invalid Choice ";




}


if(ch==5)break;


}




}

No comments:

Post a Comment