Sunday, December 6, 2020

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;


}




}

Q2

CMSACOR05P: Data Structures Lab


 2. WAP using templates to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection sort.

Q1

 CMSACOR05P: Data Structures Lab

1. Write a program to search an element from a list. Give user the option to perform Linear or Binary search. Use Template functions.

Monday, November 30, 2020

9. WAP to print a triangle of stars as follows (take number of lines from user):

 9. WAP to print a triangle of stars as follows (take number of lines from user): 

                          *
                       ***
                     *****
                   *******
                 *********


5. Write a function that checks whether a given string is Palindrome or not. Use this function to find whether the string entered by user is Palindrome or not.

 5. Write a function that checks whether a given string is Palindrome or not. Use this function to find whether the string entered by user is Palindrome or not.



6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less than 100.

 6. Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less than 100.