Professional Documents
Culture Documents
DATA STRUCTURES
PRACTICAL FILE
Submitted by : Submitted to :
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];
}
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];
}
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;
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;
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();
}
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<<" ";
}
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;
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;
}
Snode<X>*temp=top;
while(temp!=0)
{
cout<<temp->info<<setw(3);
temp=temp->next;
}
int main()
{
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' :";
cin>>a;
}
}
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;
}
}
else
break;
cout<<endl<<"do u wish to continue. if yes press 'y' : ";
cin>>a;
}
return 0;
}
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();
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;
}