You are on page 1of 63

ARYABHATTA COL

Department of Computer Science


University of Delhi

Examination Roll no - 21059570053


Semester - 3rd
B.Sc. H Computer Science

DATA STRUCTURES
PRACTICAL FILE
Submitted by : Submitted to :

Shruti verma Dr. Sonal Linda

QUES 1. Given a list of N elements, which follows no particular arrangement, you are
required to search an element x in the list. The list is stored using array data structure. If the
search is successful, the output should be the index at which the element occurs, otherwise
1
returns -1 to indicate that the element is not present in the list. Assume that the elements of
the list are all distinct. Write a program to perform the desired task.
CODE
#include<iostream>
using namespace std;
int recurLinearSearch(int array[],int element,int size);
int main()
{
cout<<"Enter Size Of Array :";
int size;
cin>>size;
int array[size], element,i;

for(int j=0;j<size;j++){
cout<<"Enter element at index "<<j<<" :";
cin>>array[j];
}

cout<<"Enter element to Search in Array :";


cin>>element;
int result;
result=recurLinearSearch(array,element,size--);
if(result>0){
cout<<"element found in Array at index “<<result ;
}
else{
cout<<"element not found in Array ";
}
return 0;
}
int recurLinearSearch(int array[],int element,int size)
{
size=size-1;
if(size <0)
{
return -1;
}
else if(array[size]==element)
{
return size;
}
else{
return recurLinearSearch(array,element,size);
}
}

QUES2 . Given a list of N elements, which is sorted in ascending order, you are required to
search an element x in the list. The list is stored using array data structure. If the search is
successful, the output should be the index at which the element occurs, otherwise returns -1
to indicate that the element is not present in the list. Assume that the elements of the list
are all distinct. Write a program to perform the desired task.
CODE
#include<iostream>
using namespace std;
int binrec(int array[],int x,int size,int h, int l);
int main()
{
int h,l=0,x,mid;
cout<<"Enter Size Of Array :";
int size=5;
cin>>size;
int array[size];
h=size-1;

for(int j=0;j<size;j++){
cout<<"Enter element at index "<<j<<" :";
cin>>array[j];
}

cout<<"Enter element to Search in Array :";


cin>>x;
int result;
result=binrec(array,x,size,h,l);
if (result<0)
cout<<"element not found ";
else
cout<<"element found at index"<<result;
return 0;
}
int binrec(int array[],int x,int size,int h, int l)
{
if (l<=h)
{
int mid=(l+h)/2;
if (array[mid]==x)
return mid;
if (array[mid]>x)
{
return (binrec(array, x, size,l,mid-1));
}
return (binrec( array, x,size,mid +1, h));
}
else
return -1;

QUES 3. Write a program to implement singly linked list which supports the following
operations: (i) Insert an element x at the beginning of the singly linked list (ii) Insert an
element x at position in the singly linked list (iii)Remove an element from the beginning of
the singly linked list (iv) Remove an element from position in the singly linked list. (v) Search
for an element x in the singly linked list and return its pointer (vi) Concatenate two singly
linked lists
CODE
#include <iostream>
using namespace std;
template<class X>
class snode {
public:
X info;
snode<X> *next;
snode(){
info = 0;
next = 0;
}
snode (X x, snode*n = 0){
info = x;
next = n;

}
};
template<class X>

class slist{
private:
snode <X>*head;
snode <X>*tail;
public:
slist(){
head = 0;
tail=0;
}
~slist();
void addtohead(X x);
void addtotail(X x);
X deletefromhead();
X deletefromtail();
bool search(X x);
bool isempty();
void display();
void Delete();
void insert(X x);
int size();
void reverse();
};
template<class X>
void slist <X>:: reverse(){
snode<X> *temp2;

snode<X> *temp1 =0;


snode<X> *temp =head;
while(temp!=0){
temp2=temp1;
temp1=temp;
temp=temp->next;

temp1->next=temp2;
}
head=temp1;
}
template<class X>
int slist <X>::size(){
int l=0;
snode<X> *temp =head;
while(temp!=0){
l++;
temp=temp->next;
}
return l;
}
template<class X>
void slist <X>:: insert(X x){
int pos;
cout<<"enter position at which you wish to insert the node ::";
cin>>pos;
int c=1;
if (pos==1){
addtohead(x);
return;
}
if(size()<pos){
addtotail(x);
return;
}
snode <X>*temp =head;
while(c < pos-1){
temp=temp->next;
c++;
}

if(temp->next == 0){
addtotail(x);
return;
}
snode <X>*p = new snode<X>(x);
p->next=temp->next;
temp->next=p;
}
template<class X>
void slist<X> :: Delete(){
int pos;
cout<<"enter position at which you wish to delete the node ::";
cin>>pos;
int c=1;
if (pos==1){
deletefromhead();
return;
}
if(size()<pos){
deletefromtail();
return;
}
snode<X> *temp =head;
snode <X>*temp1=0;;
while(c < pos){
temp1=temp;
temp=temp->next;
c++;
}
if((temp->next == 0)||(size()<pos)){
deletefromtail();
return;
}
temp1->next=temp->next;
//temp=0;
delete temp;

template<class X>
void slist<X> :: addtohead(X x){
snode <X>*P = new snode <X>(x);
if(head == 0){
head = tail = P;
}
else {
P -> next = head;
head = P;
}
}
template< class X>
void slist<X> :: addtotail(X x){
snode <X>*P = new snode <X>(x);
if(tail== 0){
head= tail = P;
}
else {
tail -> next = P ;
tail = P;
}
}
template<class X>
X slist<X> :: deletefromhead(){
int x = head -> info;
if(head == tail){
delete head;
head = tail = 0;
}
else {
snode <X>*P = head ;
head = head -> next;
delete P;
}
return x;
}
template<class X>
X slist <X>:: deletefromtail(){
X x = tail -> info;
if(head == tail){
delete tail ;
head = tail = 0;
}
else {
snode <X>*P = head ;
while( P -> next!= tail){
P = P -> next;
}
P -> next = 0;
delete tail;
tail = P;
}
return x;
}
template<class X>
void slist <X>:: display(){
snode <X>*temp = head;
while( temp != 0){
cout << temp -> info<<" ";
temp = temp -> next;
}
}
template<class X>
slist <X>::~slist(){
cout<<"destructor executed!";
snode <X>*temp=head;
while(temp!=0){
head =head->next;
delete temp;
temp=head;
}
}
template<class X>
bool slist <X>::search(X x){
snode <X>*temp=head;
while(temp!=0){
if(temp->info==x){
break;
}
temp=temp->next;
}
if (temp==0)
return 0;
else
return 1;
}
template<class X>
bool slist <X>::isempty(){
if(head==0)
return 0;
else
return 1;

int main (){


slist <char>*s1 = new slist<char>();
char x;
char a='y';
while(a=='y'){
cout<<"MENU DRIVEN PROGRAM FOR SINGLE LINKED LIST..."<<endl;
cout<<"1.addtohead "<<endl;
cout<<"2.addtotail"<<endl;
cout<<"3.deletefromhead"<<endl;
cout<<"4.deletefromtail"<<endl;
cout<<"5.search"<<endl;
cout<<"6.isempty"<<endl;
cout<<"7.display"<<endl;
cout<<"8.Delete"<<endl;
cout<<"9.insert"<<endl;
cout<<"10.to release memory/delete LL"<<endl;
cout<<"11.reverse ll"<<endl;
int ch;
cout<<"enter choice ::";
cin>>ch;
if (ch==1){
cout<<"enter the element :";

cin>>x;
s1 -> addtohead(x);
}
else if(ch==2){
cout<<"enter the element :";

cin>>x;
s1 -> addtotail(x);
}
else if(ch==3){
if(s1->isempty()==1)
cout<<"deleted element is ::"<<s1 -> deletefromhead();
else
cout<<"linked list is empty ";
}
else if(ch==4){
if(s1->isempty()==1)
cout<<"deleted element is ::"<<s1 -> deletefromtail();
else
cout<<"linked list is empty ";
}
else if(ch==5){
if(s1->isempty()==1){
cout<<"enter the element to be searched :";

cin>>x;
bool p=s1 -> search(x);
if(p==0)
cout<<"element not found ";
else
cout<<"element found ";
}
else
cout<<"linked list is empty ";
}
else if (ch==6){
bool p=s1->isempty();
if (p==0)
cout<<"LL is empty ";
else
cout<<"LL is not empty ";
}
else if(ch==7){
if(s1->isempty()==1){
s1 -> display();
cout<<endl<<" sixe is ::"<<s1->size();}
else
cout<<"linked list is empty ";
}
else if(ch==8){
if(s1->isempty()==1)
s1 -> Delete();
else
cout<<"linked list is empty ";
}
else if(ch==9){
cout<<"enter the element :";

cin>>x;
s1 -> insert(x);
}
else if(ch==10){
s1 -> ~slist();}
else if(ch==11){
s1 -> reverse();}
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' :";
cin>>a;
}

return 0;
}

QUES 4. Write a program to implement doubly linked list which supports the following
operations: (i) Insert an element x at the beginning of the doubly linked list (ii) Insert an
element x at position in the doubly linked list (iii)Insert an element x at the end of the
doubly linked list (iv) Remove an element from the beginning of the doubly linked list (v)
Remove an element from position in the doubly linked list. (vi) Remove an element from the
end of the doubly linked list (vii) Search for an element x in the doubly linked list and return
its pointer (viii) Concatenate two doubly linked lists

CODE
#include<iostream>
#include<iomanip>
using namespace std;
template<class X>

class Dnode{
public:
X info;
Dnode <X> *next;
Dnode <X>*prev ;
Dnode(){
info=0;
next=0;
prev=0;
}
Dnode(X x,Dnode *n=0,Dnode *p=0)
{
info=x;
next=n;
prev=p;
}
};
template<class X>
class Dlist{
private:
Dnode <X>*head;
Dnode <X>*tail;
public:
Dlist()
{
head=tail=0;
}
~Dlist();

void addtohead(X x);


void addtotail(X x);
X deletefromhead();
X deletefromtail();
bool search(X x);
int isempty();
void display();
void Delete();
void insert(X x);
int size();
};
template<class X>
int Dlist <X>::size(){
int l=0;
Dnode<X> *temp =head;
while(temp!=0){
l++;
temp=temp->next;
}
return l;
}
template<class X>
void Dlist <X> :: insert(X x){
int pos;
cout<<"enter position at which you wish to insert the node ::";
cin>>pos;
int c=1;
if (pos==1){
addtohead(x);
return;
}
if(size()<pos){
addtotail(x);
return;
}
Dnode <X>*temp =head;
while(c < pos-1){
temp=temp->next;
c++;
}
if(temp->next == 0){
addtotail(x);
return;
}
Dnode <X>*p = new Dnode <X>(x);
p->next=temp->next;
temp->next->prev=p;
temp->next=p;
p->prev=temp;
}
template<class X>
void Dlist <X> :: Delete(){
int pos;
cout<<"enter position at which you wish to delete the node ::";
cin>>pos;
int c=1;
if (pos==1){
deletefromhead();
return;
}
if(size()<pos){
deletefromtail();
return;
}
Dnode <X>*temp =head;
Dnode <X>*temp1=0;;
while(c < pos){
temp1=temp;
temp=temp->next;
c++;
}
if(temp->next == 0){
deletefromtail();
return;
}
//temp->prev=0;
temp1->next=temp->next;
temp->next->prev=temp1;
//temp->next=0;
delete temp;

}
template<class X>
void Dlist <X>::addtohead(X x)
{
Dnode <X>*p=new Dnode <X>(x);
if(head==0)
{
head=tail=p;
}
else
{
p->next=head;
head->prev=p;
head=p;
}
}
template<class X>
void Dlist <X>::addtotail(X x)
{
Dnode <X>*p=new Dnode <X>(x);
if(head==0)
{
head=tail=p;
}
else{
tail->next=p;
p->prev=tail;
tail=p;
}
}
template<class X>
X Dlist <X>::deletefromhead()
{
int x=head->info;
if(head==tail)
{
delete head;
head=tail=0;
}
else
{
Dnode <X>*p=head;
head=head->next;
head->prev=0;
delete p;
}

return x;
}
template<class X>
X Dlist <X>::deletefromtail()
{
int x=tail->info;
if(head==tail)
{
delete tail;
head=tail=0;
}
else
{
Dnode <X> *p=head;
tail=tail->prev;
tail->next=0;
delete p;
}

return x;
}
template<class X>
void Dlist <X>::display()
{
Dnode <X>*temp=head;
while(temp!=0)
{
cout<<temp->info<<setw(3);
temp=temp->next;
}
cout<<endl;
}
template<class X>
bool Dlist <X>::search(X x)
{
Dnode <X>*p=head;
while(p!=0)
{
if(p->info==x)
break;
p=p->next;
}
if(p==0)
return 0;
else
return 1;
}
template<class X>
int Dlist <X>::isempty()
{
Dnode <X>*p=head;
if(p==0)
return 0;
return 1;
}
template<class X>
Dlist <X>::~Dlist()
{
Dnode <X>*temp=head;
while(temp!=0)
{
head=head->next;
delete temp;
temp=head;
}
}

int main()
{
Dlist <char>*D1=new Dlist<char>();
int choice;
char x;
char a='y';
while(a=='y'){
cout<<"Enter the choice :"<<endl;
cout<<"1.add to head "<<endl<<"2. add to tail"<<endl<<"3. delete from head"<<endl<<"4. delete from tail"<<endl<<"5.
search the element"<<endl<<"6.check if list is empty"<<endl<<"7.display the list"<<endl<<"8.delete an
element"<<endl<<"9. insert an element"<<endl<<"10.delete the whole list"<<endl<<"Enter the choice: ";
cin>>choice;
if(choice==1)
{cout<<"Enter the element: ";
cin>>x;
D1->addtohead(x);
}
else if(choice==2)
{cout<<"Enter the element: ";
cin>>x;
D1->addtotail(x);
}
else if(choice==3)

{
if(D1->isempty()==1)
cout<<"deleted element is ::"<<D1 -> deletefromhead();
else
cout<<"linked list is empty ";
}
else if(choice==4)
{
if(D1->isempty()==1)
cout<<"deleted element is ::"<<D1 -> deletefromtail();
else
cout<<"linked list is empty ";
}

else if(choice==5)
{if(D1->isempty()==1){
cout<<"Enter the element to search: ";
cin>>x;
bool result;
result=D1->search(x);
if(result==true)
cout<<"Element is found"<<endl;
else
cout<<"Element is not found"<<endl;
}
else
cout<<"linked list is empty ";
}

else if(choice==6)
{
int r;
r=D1->isempty();
if(r==0)
cout<<"The list is empty"<<endl;
else
cout<<"The list is not empty"<<endl ;
}
else if(choice==7)
{{if(D1->isempty()==1){
D1->display();
cout<<endl<<"size is ::"<< D1->size();}
else
cout<<"linked list is empty ";
}

}
else if(choice==8){
if(D1->isempty()==1)
D1 -> Delete();
else
cout<<"linked list is empty ";
}
else if(choice==9)
{cout<<"Enter the element: ";
cin>>x;
D1->insert(x);
}
else if(choice==10)
{
D1->~Dlist();
}
else {
cout<<"Entered choice is wrong! Enter the correct choice";
}
cout<<endl<<"If you want to countiue? (Enter 'y' or 'n')";
cin>>a;
}
return 0;
}
QUES 5. Write a program to implement circularly linked list which supports the following
operations: (i) Insert an element x at the front of the circularly linked list (ii) Insert an
element x after an element y in the circularly linked list (iii)Insert an element x at the back of
the circularly linked list (iv) Remove an element from the back of the circularly linked list (v)
Remove an element from the front of the circularly linked list (vi) remove the element x
from the circularly linked list (vii)Search for an element x in the circularly linked list and
return its pointer (viii) Concatenate two circularly linked lists

CODE
#include <iostream>
using namespace std;
template<class X>
class cnode {
public:
X info;
cnode<X> *next;
cnode(){
info = 0;
next = 0;
}
cnode (X x, cnode*n = 0){
info = x;
next = n;

}
};
template<class X>
class clist{
private:
cnode <X>*tail;
public:
clist(){
tail=0;
}
~clist();
void addtohead(X x);
void addtotail(X x);
X deletefromhead();
X deletefromtail();
bool search(X x);
bool isempty();
void display();
void Delete();
void insert(X x);
int size();
};
template<class X>
int clist <X>::size(){
int l=0;
cnode<X> *temp =tail->next;
do{
l++;
temp=temp->next;
}while(temp!=tail->next);
return l;

}
template<class X>
void clist <X>:: insert(X x){
int pos;
cout<<"enter position at which you wish to insert the node ::";
cin>>pos;
int c=1;
if (pos==1){
addtohead(x);
return;
}
if(size()<pos){
addtotail(x);
return;
}
cnode <X>*temp =tail->next;
while(c < pos-1){
temp=temp->next;
c++;
}
if(temp->next ==tail->next){
addtotail(x);
return;
}
cnode <X>*p = new cnode<X>(x);
p->next=temp->next;
temp->next=p;
}
template<class X>
void clist<X> :: Delete(){
int pos;
cout<<"enter position at which you wish to delete the node ::";
cin>>pos;
int c=1;
if (pos==1){
deletefromhead();
return;
}
if(size()<pos){
deletefromtail();
return;
}
cnode<X> *temp =tail->next;
cnode <X>*temp1=0;;
while(c < pos){
temp1=temp;
temp=temp->next;
c++;
}
if(temp->next == tail->next){
deletefromtail();
return;
}
temp1->next=temp->next;
//temp=0;
delete temp;

template<class X>
void clist<X> :: addtohead(X x){
cnode <X>*P = new cnode <X>(x);
if(tail== 0){
tail = P;
tail->next= P;
}
else {
P -> next =tail->next;
tail->next= P;
}
}
template< class X>
void clist<X> :: addtotail(X x){
cnode <X>*P = new cnode <X>(x);
if(tail== 0){
tail = P;
tail->next= P;
}
else {
P -> next =tail->next;
tail->next= P;
tail=P;
}
}
template<class X>
X clist<X> :: deletefromhead(){
X x = tail->next -> info;
if(tail->next==tail){
delete tail;
tail = 0;
}
else {
cnode <X>*P =tail->next ;
tail->next=P -> next;
delete P;
P=0;
}
return x;
}
template<class X>
X clist <X>:: deletefromtail(){
X x = tail-> info;
if(tail->next==tail){
delete tail;
tail = 0;
}
else {
cnode <X>*P = tail->next ;
while( P -> next!= tail){
P = P -> next;
}
P -> next = tail->next;
delete tail;
tail = P;
}
return x;
}
template<class X>
void clist <X>:: display(){
cnode <X>*temp = tail->next;
do{
cout << temp -> info<<" ";

temp = temp -> next;


}while( temp!=tail->next);

}
template<class X>
clist <X>::~clist(){
cout<<"destructor executed!";
cnode <X>*temp=tail->next;
do{
tail->next =tail->next->next;
delete temp;
temp=tail->next;
}while(temp!=tail->next);
}
template<class X>
bool clist <X>::search(X x){
cnode <X>*temp=tail->next;
do{
if(temp->info==x){
break;
}
temp=temp->next;
}while(temp!=tail->next);
if (temp==0)
return 0;
else
return 1;
}
template<class X>
bool clist <X>::isempty(){
if(tail==0)
return 0;
else
return 1;

int main (){


clist <char>*c1 = new clist<char>();
char x;
char a='y';
while(a=='y'){
cout<<"MENU DRIVEN PROGRAM FOR CIRCULAR LINKED LIST..."<<endl;
cout<<"1.addtohead "<<endl;
cout<<"2.addtotail"<<endl;
cout<<"3.deletefromhead"<<endl;
cout<<"4.deletefromtail"<<endl;
cout<<"5.search"<<endl;
cout<<"6.isempty"<<endl;
cout<<"7.display"<<endl;
cout<<"8.Delete"<<endl;
cout<<"9.insert"<<endl;
cout<<"10.to release memory/delete LL"<<endl;
int ch;
cout<<"enter choice ::";
cin>>ch;
if (ch==1){
cout<<"enter the element :";

cin>>x;
c1 -> addtohead(x);
}
else if(ch==2){
cout<<"enter the element :";

cin>>x;
c1 -> addtotail(x);
}
else if(ch==3){
if(c1->isempty()==1)
cout<<"deleted element is ::"<<c1 -> deletefromhead();
else
cout<<"linked list is empty ";
}
else if(ch==4){
if(c1->isempty()==1)
cout<<"deleted element is ::"<<c1 -> deletefromtail();
else
cout<<"linked list is empty ";
}
else if(ch==5){
if(c1->isempty()==1){
cout<<"enter the element to be searched :";

cin>>x;
bool p=c1 -> search(x);
if(p==0)
cout<<"element not found ";
else
cout<<"element found ";
}
else
cout<<"linked list is empty ";
}
else if (ch==6){
bool p=c1->isempty();
if (p==0)
cout<<"LL is empty ";
else
cout<<"LL is not empty ";
}
else if(ch==7){
if(c1->isempty()==1){
c1 -> display();
cout<<endl<<"size is ::"<<c1->size();}
else
cout<<"linked list is empty ";
}
else if(ch==8){
if(c1->isempty()==1)
c1 -> Delete();
else
cout<<"linked list is empty ";
}
else if(ch==9){
cout<<"enter the element :";
cin>>x;
c1 -> insert(x);
}
else if(ch==10){
c1 -> ~clist();}
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' :";
cin>>a;
}

return 0;
}

QUES 6. Implement a stack using ARRAY representation


CODE
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

QUES 7. Implement a stack using Linked representation


CODE FOR STACK USING SLL.
#include<iostream>
#include<iomanip>
using namespace std;
template <class X>
class Snode{
public:
X info;
Snode<X>*next;
Snode()
{
info=0;
next=0;
}
Snode(X x,Snode<X>*n=0)
{
info=x;
next=n;
}
};

template <class X>


class StackLL{
private:
Snode<X>*top;
Snode<X>*tail;
public:
StackLL()
{
tail=top=0;
}
~StackLL();
void push(X x);
X pop();
int isempty();
void display();
};

template <class X>


int StackLL<X>::isempty()
{
if(top==0)
return 1;
else
return 0;
}

template <class X>


void StackLL<X>::push(X x)
{
Snode<X>*p=new Snode<X>(x);
if(top==0)
tail=top=p;
else{
p->next=top;
top=p;
}
}

template <class X>


X StackLL<X>::pop()
{
X x=top->info;
if(top==tail)
{
delete top;
tail=top=0;
}
else
{
Snode<X>*p=top;
top=top->next;
delete p;
}
return x;
}

template <class X>


void StackLL<X>::display()
{

Snode<X>*temp=top;
while(temp!=0)
{
cout<<temp->info<<setw(3);
temp=temp->next;
}

int main()
{

StackLL<char>* S1=new StackLL<char>();


int choice;
char a='y';
while(a=='y')
{
cout<<"MENU DRIVEN PROGRAM FOR STACK USING SINGLE LINKED LIST"<<endl;
cout<<"1.Push"<<endl<<"2.Pop"<<endl<<"3.Is the Stack is empty?"<<endl<<"4.Display the stack"<<endl<<"Enter the
choice: ";
cin>>choice;
if(choice==1)
{
int x;
cout<<"Enter the element to be inserted: ";
cin>>x;
S1->push(x);
}
else if(choice==2)
{
if(S1->isempty()==1)
cout<<"The stack is empty. No element can be pop.";
else
cout<<"The pop out element is: "<<S1->pop();
}
else if(choice==3)
{
int res=S1->isempty();
if(res==1)
cout<<"Stack is empty";
else
cout<<"Stack is not empty";
}
else if(choice==4)
{
if(S1->isempty()==1)
cout<<"The stack is empty."<<endl;
else{
cout<<"Stack: "<<endl;
S1->display();
}
}

else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' :";
cin>>a;
}
}

CODE FOR STACK USING DLL.


#include<iostream>
using namespace std;
template <class X>
class Dnode
{
public:
X info;
Dnode <X>*next;
Dnode <X>*prev;
Dnode()
{
next=prev=0;
}
Dnode(X x,Dnode <X>*n=0,Dnode <X>*p=0)
{
info=x;
next=n;
prev=p;
}
};
template <class X>
class stackDl
{
Dnode <X>* top;
Dnode <X>*tail;
public:
stackDl()
{
top=0;
}
void push(X x);
X pop();
int isempty();
void display();
};
template <class X>
void stackDl <X>::push(X x)
{
Dnode <X>*p = new Dnode<X>(x);
if(top==0)
{
top=tail=p;
}
else
{
p->next=top;
top->prev=p;
top=p;
}
}
template <class X>
X stackDl <X>::pop()
{
X x=top->info;
if(top==tail)
{
delete top;
top=tail=0;
}
else
{
Dnode <X> *p=top;
top=top->next;
top->prev=0;
delete p;
}
return x;
}
template <class X>
int stackDl <X>::isempty()
{

if(top==0)
return 1;
else
return 0;
}
template <class X>
void stackDl <X>::display()
{
Dnode <X> *temp=top;
while(temp!=0)
{
cout<<endl<<temp->info<<" ";
temp=temp->next;
}

int main()
{
stackDl <char>*s1=new stackDl<char>();
char a='y';
while(a=='y')
{
cout<<"Menu driven program for stack using double linked list!!"<<endl;
cout<<"1.push"<<endl;
cout<<"2.pop"<<endl;
cout<<"3.isempty"<<endl;
cout<<"4.display"<<endl;
int ch;
cout<<"Enter your choice:"<<endl;
cin>>ch;
if(ch==1)
{
cout<<"Enter the element which do u want to push in the stack:";
char x;
cin>>x;
s1->push(x);
}

else if(ch==2)
{
if(s1->isempty()==1)
{
cout<<"The satck is empty!!";
cout<<endl;
}
else
{
cout<<"The poped element is"<<s1->pop();
}
}
else if(ch==3)
{
if(s1->isempty()==1)
cout<<"The stack is empty!!"<<endl;
else
cout<<"The stack is not empty!!"<<endl;
}
else if(ch==4)
{
if(s1->isempty())
{
cout<<"The stack is empty!!"<<endl;
}
else
{
cout<<"The stack is\n";
s1->display();
}
}
else
break;
cout<<endl<<"DO YOU WANT TO CONTINUE IF YES THEN PRESS 'y':"<<endl;
cin>>a;
cout<<endl;
}
return 0;
}
QUES 8. Implement Queue using Circular Array representation
#include <iostream>
using namespace std;
template <class T>
class cqueue {
private:
int front, rear;
T a[20] ;
public :
cqueue(){
front = rear = -1;
}
void enqueue(T x);
T dequeue();
int isempty();
int isfull();
int clear();
void display();
};
template <class T>
void cqueue <T> :: enqueue(T x){
if(!isfull())
{
if(rear == -1 || rear == 19)
{
rear=0;
if(front == -1)
front = 0;
}
else
{
rear++;
}
a[rear] = x;
}
else
{
cout<<"Queue is full";
}
}
template <class T>
T cqueue <T> :: dequeue()
{
if(!isempty()){
T x = a[front];
if(front==rear)
{
front = rear = -1;
}
else if(front == 19)
{
front = 0;
}
else
{
front++;
}
return x;
}
else
{
cout<<" Queue is empty";
return -1;
}
}
template <class T>
int cqueue <T> :: isempty(){
if (front == -1 && rear == -1)
{
return 1;
}
else {
return 0;
}
}
template <class T >
int cqueue <T> :: isfull(){
if( (front==0 && rear==19) || (front == rear+1)){
return 1;
}
else{
return 0;
}
}
template <class T >
void cqueue <T> :: display(){
if( front<=rear){
for( int i=front; i<=rear; i++){
cout<<a[i]<<endl;
}
}
else{
for( int i=front; i<=19; i++)
cout<< a[i];
for( int i=0; i<=rear; i++)
cout<< a[i];
}
}
template <class T >
int cqueue <T> :: clear()
{
front= rear = -1;
}
int main(){
cqueue <int>c1;
char a = 'y';
while(a=='y'){
cout << "menu driven program for queue "<<endl;
cout<< "1. enqueue "<<endl;
cout<< "2. dequeue "<<endl;
cout<< "3. isempty "<<endl;
cout<< "4. isfull "<<endl;
cout<< "5. clear "<<endl;
cout<< "6. display "<<endl;
int ch,x;
cout<<"enter choice : ";
cin >>ch;
if(ch==1){
cout<<"enter the element : ";
cin >>x;
c1.enqueue(x);
}
else if(ch==2){
c1.dequeue() ;
}
else if(ch==3){
int i = c1.isempty() ;
if( i== 1){
cout<<"queue is empty";
}
else{
cout<<"queue is not empty";
}

}
else if(ch==4){
int i = c1.isfull() ;
if( i== 1){
cout<<"queue is full";
}
else{
cout<<"queue is not full";
}

}
else if (ch==5){
c1.clear() ;

}
else if (ch==6){
c1.display () ;

}
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' : ";
cin>>a;
}
return 0;
}
QUES 9. Implement Queue using Circular LINKED LIST representation

#include <iostream>
using namespace std;
class cnode{
public:
int info;
cnode*next;
cnode(){
info=0;
next=0;
}
cnode(int x, cnode*n=0){
info = x;
next = n;
}
};
class cqueue
{
cnode*rear;
public:
cqueue(){
rear = 0;
}
void enqueue(int x);
int dequeue();
bool isempty();
};

bool cqueue::isempty(){
if(rear==0)
return 0;
else
return 1;
}

void cqueue ::enqueue(int x){


cnode*P = new cnode(x);
if(rear==0){
rear=P;
rear->next = P;
}
else
{
P->next = rear->next;
rear->next = P;
rear = P;
}
}
int cqueue ::dequeue(){
int x= rear->next->info;
if(rear->next==rear){
delete rear;
rear = 0;
}
else
{
cnode*P = rear->next;
rear->next = P->next;
delete P;
P=0;
}
return x;
}
int main(){
cqueue c1;
char a = 'y';
while(a=='y'){
cout << "menu driven program for queue "<<endl;
cout<< "1. enqueue "<<endl;
cout<< "2. dequeue "<<endl;
int ch,x;
cout<<"enter choice : ";
cin >>ch;
if(ch==1){
cout<<"enter the element : ";
cin >>x;
c1.enqueue(x);
}
else if(ch==2){
if(c1.isempty()==1)
cout<<"deleted element is ::"<<c1.dequeue();
else
cout<<"Queue is empty ";

}
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' : ";
cin>>a;
}
return 0;
}

QUES 10. Implement Double-ended Queues using Linked list representation


CODE
#include<iostream>
#include<iomanip>
using namespace std;
template<class X>

class DQnode{
public:
X info;
DQnode <X> *next;
DQnode <X>*prev ;
DQnode(){
info=0;
next=0;
prev=0;
}
DQnode(X x,DQnode *n=0,DQnode *p=0)
{
info=x;
next=n;
prev=p;
}
};
template<class X>
class DQueueLL{
private:
DQnode <X>*front;
DQnode <X>*rear;
int n;
public:
DQueueLL()
{
front=rear=0;
n=0;
}
~DQueueLL();

void insertfront(X x);


void insertback(X x);
X removefront();
X removeback();
int isempty();
void display();
int size();
X Front();
X Back();
};
template<class X>
int DQueueLL <X>::size(){
return n;
}
template<class X>
X DQueueLL <X>::Front(){
return front->info;
}
template<class X>
X DQueueLL <X>::Back(){
return rear->info;
}
template<class X>
void DQueueLL <X>::insertfront(X x)
{
DQnode <X>*p=new DQnode <X>(x);
if(front==0)
{
front=rear=p;
}
else
{
p->next=front;
front->prev=p;
front=p;
}
n++;
}
template<class X>
void DQueueLL <X>::insertback(X x)
{
DQnode <X>*p=new DQnode <X>(x);
if(front==0)
{
front=rear=p;
}
else{
rear->next=p;
p->prev=rear;
rear=p;
}
n++;
}
template<class X>
X DQueueLL <X>::removefront()
{
int x=front->info;
if(front==rear)
{
delete front;
front=rear=0;
}
else
{
DQnode <X>*p=front;
front=front->next;
front->prev=0;
delete p;
}
n--;
return x;
}
template<class X>
X DQueueLL <X>::removeback()
{
X x=rear->info;
if(front==rear)
{
delete rear;
front=rear=0;
}
else
{
DQnode <X> *p=rear;
rear=rear->prev;
rear->next=0;

delete p;
rear=p;
}
n--;
return x;
}
template<class X>
void DQueueLL <X>::display()
{
DQnode <X>*temp=front;
while(temp!=0)
{
cout<<temp->info<<setw(3);
temp=temp->next;
}
cout<<endl;
}
template<class X>
int DQueueLL <X>::isempty()
{
DQnode <X>*p=front;
if(p==0)
return 0;
return 1;
}
template<class X>
DQueueLL <X>::~DQueueLL()
{
DQnode <X>*temp=front;
while(temp!=0)
{
front=front->next;
delete temp;
temp=front;
}
n=0;
}

int main()
{
DQueueLL <char>*D1=new DQueueLL<char>();
int choice;
char x;
char a='y';
while(a=='y'){
cout<<"Enter the choice :"<<endl;
cout<<"1.add to front "<<endl<<"2. add to rear"<<endl<<"3. delete from front"<<endl<<"4. delete from
rear"<<endl<<"5.check if DQ is empty"<<endl<<"6.display the DQ"<<endl<<"7.Print front element"<<endl<<"8. Print last
element"<<endl<<"9.delete the whole DQ"<<endl<<"10.display size"<<endl<<"Enter the choice: ";
cin>>choice;
if(choice==1)
{cout<<"Enter the element: ";
cin>>x;
D1->insertfront(x);
}
else if(choice==2)
{cout<<"Enter the element: ";
cin>>x;
D1->insertback(x);
}
else if(choice==3)

{
if(D1->isempty()==1)
cout<<"deleted element is ::"<<D1 -> removefront();
else
cout<<"DQ is empty ";
}
else if(choice==4)
{
if(D1->isempty()==1)
cout<<"deleted element is ::"<<D1 -> removeback();
else
cout<<"DQ is empty ";
}

else if(choice==5)
{
int r;
r=D1->isempty();
if(r==0)
cout<<"The DQ is empty"<<endl;
else
cout<<"The DQ is not empty"<<endl ;
}
else if(choice==6)
{{if(D1->isempty()==1)
D1->display();
else
cout<<"DQ is empty ";
}

}
else if(choice==7){
if(D1->isempty()==1)
cout<<"front element is : "<<D1 -> Front();
else
cout<<"DQ is empty ";
}
else if(choice==8){
if(D1->isempty()==1)
cout<<"last element is : "<<D1 -> Back();
else
cout<<"DQ is empty ";
}
else if(choice==9)
{
D1->~DQueueLL();
}
else if(choice==10)
{
cout<<"size of DQ is ::"<<D1->size();
}
else {
cout<<"Entered choice is wrong! Enter the correct choice";
}
cout<<endl<<"If you want to countiue? (Enter 'y' or 'n')";
cin>>a;
}
return 0;
}

QUES 11. Write a program to implement Binary Search Tree which supports the following
operations: (i) Insert an element x (ii) Delete an element x (iii) Search for an element x in the
BST and change its value to y and then place the node with value y at its appropriate
position in the BST (iv) Display the elements of the BST in preorder, inorder, and postorder
traversal (v) Display the elements of the BST in level-by-level traversal (vi) Display the height
of the BST
CODE
#include <iostream>
using namespace std;
template <class T>
class cqueue
{
private:
int front ,rear;
T a[20];
public:
cqueue()
{
front=rear=-1;
}
void enqueue(T x);
T dequeue();
int isempty();
int isfull();
};
template <class T>
int cqueue<T>::isfull()
{
if ((front==0&&rear==19)||(front==rear+1))
return 1;
else
return 0;
}
template <class T>
void cqueue<T>::enqueue(T x)
{
if(!isfull())
{
if(rear==-1||rear==19)
{
rear=0;
if (front==-1)
{
front=0;
}
}
else
{
rear++;
}
a[rear]=x;
}
else
{
cout<<"Queue is full"<<endl;
}

}
template <class T>
T cqueue<T>::dequeue()
{
if (!isempty())
{
T x = a[front];
if (front==rear)
{
front=rear=-1;
}
else if(front==19)
{
front=0;
}
else
front++;
return x;
}
}
template <class T>
int cqueue<T>::isempty()
{
if(front==-1&&rear==-1)
return 1;
else
return 0;
}
template <class T>
class BSTnode
{
public:
int info;
BSTnode *left;
BSTnode *right;
BSTnode()
{
info=0;
left=0;
right=0;
}
BSTnode(int n,BSTnode *a=0,BSTnode *b=0)
{
info=n;
left=a;
right=b;
}
};
template <class T>
class BSTree
{
BSTnode <T> *root;
public:
BSTree ()
{
root=0;
}
int isempty();
void insert(T e1);
int search(T e2);
void inorder();
void inorder(BSTnode<T> *);
void preorder();
void preorder(BSTnode<T> *);
void postorder(BSTnode<T> *);
void postorder();
void breadthfirst();
void breadthfirst(BSTnode<T> *);
void deletemerging(BSTnode <T> * );
void deletebycopying(BSTnode <T> *);
int height(BSTnode <T> *);
int height();
void largest();
void smallest();
};
template <class T>
void BSTree<T> ::largest(){
BSTnode<T> * p=root;
while(p->right!=0){
p=p->right;
}
cout<<"largest element is "<<p->info;
}
template <class T>
void BSTree<T> ::smallest(){
BSTnode<T> * p=root;
while(p->left!=0){
p=p->left;
}
cout<<"smallest element is "<<p->info;
}
template <class T>
void BSTree<T> :: insert(T e1)
{
BSTnode<T> * temp=new BSTnode<T>(e1);
BSTnode<T> * p=root, *prev=0;
while(p!=0)
{
prev=p;
if(p->info<e1)
p=p->right;
else
p=p->left;
}
if(root==0)
root=temp;
else if(prev->info<e1)
prev->right=temp;
else
prev->left=temp;
}
template <class T>
void BSTree<T> :: inorder(BSTnode<T> *p)
{
if(p!=0)
{
inorder(p->left);
cout<<p->info<<" ";
inorder(p->right);
}
}
template <class T>
void BSTree<T> :: inorder()
{
inorder(root);
}
template <class T>
void BSTree<T> :: preorder(BSTnode<T> *p)
{
if (p!=0)
{
cout<<p->info<<" ";
preorder(p->left);
preorder(p->right);
}
}
template <class T>
void BSTree<T> :: preorder()
{

preorder(root);
}
template <class T>
void BSTree<T> :: postorder(BSTnode<T> *p)
{
if (p!=0)
{
postorder(p->left);
postorder(p->right);
cout<<p->info<<" ";
}
}
template <class T>
void BSTree<T> :: postorder()
{
postorder(root);
}
template <class T>
int BSTree<T> :: height(BSTnode <T>*root){
if(root ==0)
return 0;
else{
int hl=height(root->left );
int hr=height(root->right );
if (hl>=hr)
return hl+1;
else
return hr+1;
}
}
template <class T>
int BSTree<T> :: height()
{
int o=height(root);
return o;
}
template <class T>
void BSTree<T> ::breadthfirst(BSTnode<T> * root){
if (root== NULL)
return;
cqueue <BSTnode<T> *> q;
BSTnode<T> *P;
q.enqueue(root);
while(q.isempty()==0){
P=q.dequeue();
cout<<P->info<<" ";
if(P->left!=0)
q.enqueue(P->left);
if(P->right!=0)
q.enqueue(P->right);
}
}
template <class T>
void BSTree<T> ::breadthfirst(){
breadthfirst(root);
}
int main()
{
BSTree<int> T1;
char ch='y';
while(ch=='y'|| ch=='Y')
{
cout<<"1. Add element to BST"<<endl;
cout<<"2. Print Inorder"<<endl;
cout<<"3. Print Preorder"<<endl;
cout<<"4. Print Postorder"<<endl;
cout<<"5. calculate height"<<endl;
cout<<"6. Print BFT"<<endl;
cout<<"7. Print largest elemnt"<<endl;
cout<<"8. Print smallest elemnt"<<endl;
cout<<"Enter your choice :: ";
int c;
cin>>c;
if (c==1)
{
cout<<"Enter element you want to add :: ";
int t;
cin>>t;
T1.insert(t);
}
else if (c==2)
{
cout<<"Inorder of BST :: "<<endl;
T1.inorder();
cout<<endl;
}
else if (c==3)
{
cout<<"Preorder of BST :: "<<endl;
T1.preorder();
cout<<endl;
}
else if (c==4)
{
cout<<"Postorder of BST :: "<<endl;
T1.postorder();
cout<<endl;
}
else if (c==5)
{
cout<<"height of bst :: ";
int p=T1.height();
cout<<p;
cout<<endl;
}
else if (c==6)
{
cout<<"BFT of bst :: ";
T1.breadthfirst();
cout<<endl;
}
else if (c==7)
{
T1.largest();
cout<<endl;
}
else if (c==8)
{
T1.smallest();
cout<<endl;
}
else
cout<<"Wrong choice entered !!"<<endl;
cout<<"Do you wish to continue(y or Y)";
cin>>ch;
}
return 0;
}

You might also like