Sunday, December 6, 2020

CMSACOR05P: Data Structures Lab

 CMSACOR05P: Data Structures Lab


9. Create and perform different operations on Double-ended Queues using Linked List implementation.


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class node{

          public:

                   int key;

                   int data;

                   node* link1;

                   node(){

                             key=0;

                             data=0;

                             link1=NULL;

                   }

};

class de_queue{

          public:

                   node* head;

                   de_queue(){

                             head=NULL;

                   }

                   int check(int n){

                             node* ptr=head;

                             while(ptr!=NULL){

                                      if(ptr->key==n)

                                                return 1;

                                      ptr=ptr->link1;

                             }

                             return 0;

                   }

                   void queue(node* n){

                             if(head==NULL)

                                      head=n;

                             else{

                                      if(check(n->key))

                                                cout<<"THIS KEY IS ALREADY EXIST...TRY WITH ANOTHER KEY .."<<endl;

                                      else{

                                                node* ptr=head;

                                                while(ptr!=NULL){

                                                          if(ptr->link1==NULL){

                                                                   ptr->link1=n;

                                                                   break;

                                                          }

                                                          ptr=ptr->link1;

                                                }

                                      }

                             }                          

                   }

                   void stenqueue(node* n){

                             if(head==NULL)

                                      head=n;

                             else{

                                      if(check(n->key))

                                                cout<<"THIS KEY IS ALREADY EXIST...TRY WITH ANOTHER KEY .."<<endl;

                                      else{

                                                n->link1=head;

                                                head=n;

                                                cout<<"INSERT A DATA SUCCESSFULLY FROM FRONT...."<<endl;

                                      }

                             }

                   }

                   void lastenqueu(node* n){

                             if(head==NULL)

                                      head=n;

                             else{

                                      if(check(n->key))

                                                cout<<"THIS KEY IS ALREADY EXIST...TRY WITH ANOTHER KEY .."<<endl;

                                      else{

                                                node* ptr=head;

                                                while(ptr!=NULL){

                                                          if(ptr->link1==NULL){

                                                                   ptr->link1=n;

                                                                   break;

                                                          }

                                                          ptr=ptr->link1;

                                                }

                                                cout<<"INSERT DATA SUCCESSFULLY FROM REAR ...."<<endl;

                                      }

                             }                          

                   }

                   void stdqueue(){

                             if(head==NULL)

                                      cout<<"THE QUEUE IS EMPTY ...."<<endl;

                             else{

                                      head=head->link1;

                                      cout<<"DELETE AN ELEMENT SUCCESSFULLY FROM FRONT ...."<<endl;

                             }

                   }

                   void lastdqueue(){

                             if(head==NULL)

                                      cout<<"THE QUEUE IS EMPTY........."<<endl;

                             else{

                                      node* ptr=head;

                                      node* ptr1=NULL;

                                      while(ptr->link1!=NULL){

                                                ptr1=ptr;

                                                ptr=ptr->link1;

                                      }

                                      ptr1->link1=NULL;

                                      cout<<"DELETE AN ELEMENT SUCCESSFULLY FROM REAR..."<<endl;

                             }

                   } 

                   void display(){

                             if(head==NULL)

                                      cout<<"THE QUEUE IS EMPTY ...."<<endl;

                             else{

                                      node* ptr=head;

                                      while(ptr!=NULL){

                                                cout<<"("<<ptr->key<<","<<ptr->data<<")->";

                                                ptr=ptr->link1;

                                      }

                             }

                   }

};

int main(){

          de_queue ob;

          int opt=0;

          do{

                   cout<<"\nPRESS 0 TO EXIT ...."<<endl;

                   cout<<"PRESS 1 TO INSERT DAAT IN QUEUE ....."<<endl;

                   cout<<"PRESS 2 TO ENQUEUE AT FIRST ...."<<endl;

                   cout<<"PRESS 3 TO ENQUEUE AT LAST......"<<endl;

                   cout<<"PRESS 4 TO DEQUEUE AT FIRST......"<<endl;

                    cout<<"PRESS 5 TO DEQUEUE AT LAST......"<<endl;

                   cout<<"PRESS 6 TO DISPLAY......"<<endl;

                   cin>>opt;

                   node *n=new node();

                   switch(opt){

                             case 0:

                                      break;

                             case 1:

                                      cout<<"ENTER THE KEY OF THE DATA ->"<<endl;

                                      cin>>n->key;

                                      cout<<"ENTER THE DATA ->"<<endl;

                                      cin>>n->data;

                                      ob.queue(n);

                                      break;

                             case 2:

                                      cout<<"ENTER THE KEY OF THE DATA ->"<<endl;

                                      cin>>n->key;

                                      cout<<"ENTER THE DATA ->"<<endl;

                                      cin>>n->data;

                                      ob.stenqueue(n);

                                      break;

                             case 3:

                                      cout<<"ENTER THE KEY OF THE DATA ->"<<endl;

                                      cin>>n->key;

                                      cout<<"ENTER THE DATA ->"<<endl;

                                      cin>>n->data;

                                      ob.lastenqueu(n);

                                      break;

                             case 4:

                                      ob.stdqueue();

                                      break;

                             case 5:

                                      ob.lastdqueue();

                                      break;

                             case 6:

                                      ob.display();

                                      break;

                             default:

                                      cout<<"...............ERROR..............."<<endl;

                                      break;

                   }

                  

          }while(opt!=0);

          return 0;

}


6.Perform Stack operations using Linked List implementation

#include <iostream>

using namespace std;


struct node

{

int data;

node *next;

};

class stack

{

private:

node *top;

public:

stack()

{

top= NULL;

}

void push()


{

int a;

cout<<"\nEnter the element to PUSH: ";

cin>>a;

node *tmp=new node();

tmp->data=a;

tmp->next=top;

top=tmp;

}

void pop()

{

if(top==NULL)

{

cout<<"\nStack is Empty.";

}

else

{

node *temp=NULL;

temp=top;

cout<<"\nPoped element is "<<temp->data;

top=top->next;

delete temp;

}

}

void display()

{

cout<<"\nThe Stack is:\n";

node *h=top;

cout<<"\n";

if(h==NULL)

cout<<"\nStack is Empty.";

while(h!=NULL)

{

cout<<" "<<h->data;

h=h->next;

}

}


~stack()

{

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

}


};

int main()

{

stack ob;

int ch;


do

{

    cout<<"\n ____STACK OPERATION USING LISNKED LIST____";

cout<<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT";

cout<<"\nEnter ur Choice: ";

cin>>ch;

switch(ch)

{

case 1:

ob.push();

break;


case 2:

ob.pop();

break;


case 3:

ob.display();

break;


case 4:

cout<<"\nEXIT";

break;


default:

cout<<"\nSorry! Invalid Choice";

}


}

while(ch!=4);

return 0;

}

7.


3. Implement Linked List using templates. Include functions for insertion, deletion and search of a number, reverse the list and concatenate two linked lists (include a function and also overload operator +).

 CMSACOR05P: Data Structures Lab


3. Implement Linked List using templates. Include functions for insertion, deletion and search of a number, reverse the list and concatenate two linked lists (include a function and also overload operator +).



#include <iostream>


using namespace std;


struct node


{


int data;


node *next;


};


class linked_list


{


private:


node *head,*tail;


public:


linked_list()


{


head = NULL;


tail = NULL;


}


void add_node(int n)


{


int x;


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


{


cout<<"\nDATA"<<i<<"=";


cin>>x;


node *tmp = new node;


tmp->data = x;


tmp->next = NULL;


if(head == NULL)


{


head = tmp;


tail = tmp;


}


else


{


tail->next = tmp;


tail = tail->next;


}


}


}


node* gethead()


{


return head;


}


void Search(node *head_ref,int key)


{


node *current=head_ref;


int c;


bool Flag=false;


while(current!=NULL)


{


if(current->data==key)


{




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


Flag=true;


break;


}


else


{


current=current->next;


}


c++;


}


if(Flag==true)


{


c++;


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


}


else


{


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


}


}


void Insert(int pos,int x,node *head_ref)


{


node *tmp = new node;


tmp->data = x;


tmp->next = NULL;


node *ptr=NULL;


ptr=head_ref;//points 1st element


if(pos==1)


{


head=tmp;//head updated


tmp->next=ptr;


}


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 *head_ref)


{


if(pos==1)


{


node *temp1=head_ref;


head=head_ref->next;


delete temp1;


}


else


{


node *temp=head_ref;


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


{


if(temp->next!=NULL)


{


temp=temp->next;


}


}


node *temp2=temp->next;


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


delete temp2;


}


}


void reverse(node *head_ref,int n)//n= number of elements in linked list


{


int i;


node *pre,*in,*next;


pre=head_ref;//initially


in=NULL;//initially


next=NULL;//initially




while(pre!=NULL)


{


next=pre->next;


pre->next=in;//join


in=pre;//updated


pre=next;//updated


}


head=in;//at last iteration in points the last node so...


}


linked_list operator+(linked_list ob2)


{


linked_list ob3;


node *link=gethead();


while(link->next!=NULL)


{


link=link->next;


}


link->next=ob2.gethead();


ob3.head=head;


return ob3;


}


void display(node *head_ref)


{


if(head_ref == NULL)


{


cout << "NULL" << endl;


}


else


{


cout << head_ref->data <<" ";


display(head_ref->next);


}


}


~linked_list()


{


cout<<"\nThank U...";


}


};


int main()


{


cout<<"\n******PRESENTING LINKEDLIST OPERATIONS******\n";


cout<<"\nLet's create linked lists:";


linked_list a;


linked_list b;


linked_list c;


node *head;


int n1;


cout<<"\nEnter the Number of elements u want to insert in 1st linkedlist: ";


cin>>n1;


a.add_node(n1);


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


a.display(a.gethead());


while(1){


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


int ch;


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


cin>>ch;


switch (ch)


{


case 1:


int y;


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


cin>>y;


a.Search(a.gethead(),y);


break;


case 2:


int posI,x;


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


cin>>posI;


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


cin>>x;


a.Insert(posI,x,a.gethead());


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


a.display(a.gethead());


break;




case 3:


int pos;


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


cin>>pos;


a.Deletion(pos,a.gethead());


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


a.display(a.gethead());


break;


case 4:


a.reverse(a.gethead(),n1);


cout<<"\nThe REVERSED linked list is:\n";


a.display(a.gethead());


break;


case 5:


int n2;


cout<<"\nnumber of elements u want to insert in 2nd linkedlist: ";


cin>>n2;


b.add_node(n2);


c=a+b;


cout<<"\nPresenting the Marged linked list\n";


c.display(c.gethead());


break;


case 6: cout<<"END";


default:


cout << "Sorry! Invalid Choice ";


break;


}


if(ch==6)


break;


}


return 0;


}


Posted by

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

CMSACOR05P: Data Structures Lab



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



 #include <iostream>


using namespace std;




struct node


{


int data;


node *prv;


node *next;


};




class DLL


{


private:


node *head,*tail;


public:


DLL()


{


head = NULL;


tail = NULL;


}




void add_node(int n)


{




int x;


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


{


cout<<"\nDATA"<<i<<"=";


cin>>x;


node *tmp = new node;


tmp->data = x;


tmp->prv = NULL;


tmp->next = NULL;




if(head == NULL)


{


head = tmp;


tail = tmp;


}


else


{


tail->next = tmp;




tmp->prv=tail;


tail = tail->next;


}


}


}


node* gethead()


{


return head;


}


void Search(node *head_ref,int key)


{


node *current=head_ref;


int c=0;


bool Flag=false;


while(current!=NULL)


{


if(current->data==key)


{


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


Flag=true;


break;


}


else


{


current=current->next;


}


c++;


}


if(Flag==true)


{


c++;


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


}


else


{


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


}


}


void Insert(int pos,int x,int n,node *head_ref)




{


if(pos<=n+1)


{


node *tmp = new node;


tmp->data = x;


tmp->prv =NULL;


tmp->next = NULL;


node *ptr=NULL;


ptr=head_ref;//points 1st element


if(pos==1)


{


head=tmp;//head updated


tmp->next=ptr;


tmp->prv=tmp;


}


else


{


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


{


ptr=ptr->next;


}


node *ptrN=NULL;


ptrN= ptr->next;


ptr->next= tmp;


tmp->prv= ptr;


tmp->next= ptrN;


}


}


else


{


cout<<"\nSorry! Insertion is not possible.";


}


}


void Deletion(int pos,int n,node *head_ref)


{


if(pos<=n)


{


node *ptrN;


node *ptr=head_ref;




if(pos==1)


{


head=head_ref->next;


head->prv=NULL;


delete ptr;


}


else


{


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


{


if(ptr->next!=NULL)


{


ptr=ptr->next;


}


}


ptrN=ptr->next;


ptr->next=ptr->next->next;


delete ptrN;


if(ptr->next!=NULL)


{


ptr->next->prv=ptr;


}


}


}


else


{


cout<<"\nSorry! Deletion not possible.";


}




}


void reverse(node *head_ref)


{


int i;


node *pre,*in,*next;


pre=head_ref; //initially


in=NULL; //initially


next=NULL; //initially


while(pre!=NULL)


{




next=pre->next;


pre->next=in;//join


pre->prv=next;


in=pre;//updated


pre=next;//updated


}


head=in;//at last iteration in points the last node so...


}




void display(node *head_ref)


{


if(head_ref == NULL)


{


cout << "NULL" << endl;


}


else


{


cout << head_ref->data <<" ";


display(head_ref->next);


}


}


~DLL()


{


cout<<"\nThank U";


}


};


int main()


{


cout<<"\n*****Presenting Double Linked List Operations*****\n";


int n1;


cout<<"\nEnter the number of elments to insert in the linked list: ";


cin>>n1;


DLL a;


a.add_node(n1);


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


a.display(a.gethead());


while(1){


cout<<"\n1.Searching\n2.Insertion\n3.Deletion\n4.Reverse\n5.EXIT \nEnter ur choice: ";


int ch;


cin>>ch;




switch (ch)


{


case 1:


int key;


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


cin>>key;


a.Search(a.gethead(),key);


break;




case 2:


int p,x;


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


cin>>p;


cout<<"\nEnter the data: ";


cin>>x;


a.Insert(p,x,n1,a.gethead());


if(p<=n1+1)


{


cout<<"\nThe updated Linked List:\n";


a.display(a.gethead());


}


break;




case 3:


int pos;


cout<<"\nEnter the position to delete: ";


cin>>pos;


a.Deletion(pos,n1,a.gethead());


if(pos<=n1)


{


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


a.display(a.gethead());


}


break;




case 4:


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


a.reverse(a.gethead());


a.display(a.gethead());




break;


case 5:


cout<<"end"<<endl;


default:


cout << "Sorry! Invalid Choice ";






}


if(ch==5)break;


}


return 0;


}



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;


}




}