dsprograms(2nd cse)
DESCRIPTION
this file contains Data Structures Programs in C++TRANSCRIPT
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 1) /* Implementation of STACK ADT using arrays */#include<iostream.h>#include<conio.h>template <class t>class stack{
t s[20];int top,size;public:
stack (int);void push(t);t pop();void display();
};template <class t>stack <t> :: stack(int n){
top=-1;size=n;
}template <class t>void stack<t> :: push(t item){
if(top>=size-1)cout<<"stack is full";
elses[++top]=item;
}template <class t>t stack <t> :: pop(){
if(top==-1){
cout<<"\n stack is empty\n";return 0;
}else
return (s[top--]);}template <class t>void stack <t> :: display(){
if(top==-1)cout<<"\n stack is empty\n";
else{
cout<<"\n the stack elements are\n";for(int i=top;i>=0;i--)
cout<<s[i]<<endl;}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 1
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}void main(){
stack <int> s1(20);stack <char> s2(20);int ch,ch1;int iitem,piitem;char citem,pcitem;clrscr();do{
cout<<"\n\n 1.push\n 2.pop \n 3.exit\n";cout<<"\n Enter ur choice:";cin>>ch;switch(ch){
case 1:cout<<"\n 1.Integer \n 2.Character \n";cin>>ch1;if(ch1==1){
cout<<"\n Enter the element:";cin>>iitem;s1.push(iitem);s1.display();
}else{
cout<<"\n Enter the character :";cin>>citem;s2.push(citem);s2.display();
}break;
case 2:cout<<"\n 1.Integer \n 2.Character \n";cout<<"Enter ur choice :";cin>>ch1;if(ch1==1){
piitem=s1.pop();cout<<"\n The popped element is \n";cout<<piitem;s1.display();
}else{
pcitem=s2.pop();cout<<"\n The popped element is \n";cout<<pcitem;s2.display();
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 2
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
} break;}
}while(ch<=2);}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 3
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 2) /* Implementation of QUEUE ADT using arrays */#include <iostream.h>#include <conio.h>template <class t>class queue{
t q[10];int front,rear,size;public:
queue(int);void insert(t);t del();void display();
};
template <class t>queue <t>:: queue(int n){
rear=-1;front=-1;size=n;
}
template <class t>void queue <t> :: insert(t item){
if(rear==size-1)cout<<"\n Queue is full \n";
else{
q[++rear]=item;if(front==-1)
front=0;}
}
template <class t>t queue <t> :: del(){
if(front==-1){
cout<<"\n queue is empty \n";return 0;
}else{
if(front==rear){
int temp=q[front];front=-1;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 4
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
rear=-1;return temp;
}else
return (q[front++]);}
}
template <class t>void queue <t> :: display(){
if(front==-1)cout<<"\n queue is empty\n";
else{
cout<<"\n the queue elements are :\n";for( int i=front;i<=rear;i++)
cout<<q[i]<<endl;}
}
void main(){
queue <int> q1(20);queue <char> q2(20);int ch,ch1,iitem,ditem;char citem,dcitem;clrscr();do{
cout<<"\n 1.Insertion\n 2.Deletion \n 3.Exit\n";cout<<"\n Enter your choice:";cin>>ch;switch(ch){
case 1:cout<<"\n 1.Integer \n 2.Character \n";cout<<"\n Enter ur choice :";
cin>>ch1;if(ch1==1){
cout<<"\n Enter the element :";cin>>iitem;q1.insert(iitem);q1.display();
}else{
cout<<"\n Enter the element :";cin>>citem;q2.insert(citem);
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 5
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
q2.display();}break;case 2:
cout<<"\n 1. Integer 2. Character \n";cout<<"\n Enter your choice:";cin>>ch1;if(ch1==1){
ditem=q1.del();cout<<"\n The deleted integer item is :"<<ditem;q1.display();
}else{ dcitem=q2.del(); cout<<"\n The deleted character item is :"<<dcitem;
q2.display();}break;
}}while(ch<=2);
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 6
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 3) /* Implementation STACK ADT using singly linked list */#include <iostream.h>#include <conio.h>template <class t>class stack{
protected:struct node{
t data;node *link;
};node *top;
public:void push(t);void pop();void display();stack(){
top=NULL;}
};template <class t>void stack <t> :: push(t item){
node *first=new node[1];first->data=item;first->link=top;top=first;
}template <class t>void stack <t> :: pop(){
if(top==NULL)cout<<"\n stack is empty \n";
else{
cout<<"\n the popped elemtn is :"<<top->data;top=top->link;
}}template <class t>void stack <t> :: display(){
if(top==NULL)cout<<"stack is empty";
else{
node *list=top;cout<<"\n The elements in the stack are \n";
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 7
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
while(list->link!=NULL){
cout<<list->data<<endl;list=list->link;
}cout<<list->data;
} }void main(){
stack <int> s1;stack <char> s2;int ch,ch1,iitem;char citem;clrscr();do{
cout<<"\n 1.Push \n 2.Pop \n 3.Exit \n";cout<<"\n Enter your choice:";cin>>ch;switch(ch){
case 1:cout<<"\n 1.Integer 2.Character \n";cout<<"\n Enter your choice :";cin>>ch1;if(ch1==1){
cout<<"\n Enter Integer Element:";cin>>iitem;s1.push(iitem);s1.display();
}else{
cout<<"\n Enter Character Element: ";cin>>citem;s2.push(citem);s2.display();
}break;case 2:cout<<"\n 1.Integer 2.Character \n";cout<<"\n Enter your choice :";cin>>ch1;if(ch1==1){ s1.pop();
s1.display();}else{ s2.pop();
s2.display();}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 8
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
break;}
}while(ch<=2);}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 9
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 4) / * Implementation of Queues using singly LinkedList */#include<iostream.h>#include<conio.h>template <class T>class Listqueue{ struct node { T data; node *link; }*front,*rear; public: Listqueue(); void Enqueue(T val); T Dequeue(); void Print(); ~Listqueue();};
template <class T>Listqueue<T>::Listqueue(){ front=rear=NULL;}
template <class T>void Listqueue<T>::Enqueue(T val){ node *newnode,*ptr; newnode=new node; if(newnode==NULL) { cout<<"Queue is full!!!"; return; } else { newnode->data=val; newnode->link=NULL; if(rear==NULL) { front=rear=newnode; return; } rear->link=newnode; rear=rear->link; }}template <class T>T Listqueue<T>::Dequeue()
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 10
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
{if(front==NULL) { cout<<"Queue is empty!!!"; return(0); } else { node *temp; temp=front; T val=front->data; front=front->link; delete temp; cout<<"Deleted item is:"; return val; }}
template <class T>void Listqueue<T>::Print(){if(front==NULL) { cout<<"Queue is empty!!!"; return; } else { node *ptr; ptr=front; cout<<"Elements in the Queue are:\n" ; while(ptr!=NULL) { cout<<ptr->data<<" "; ptr=ptr->link; } }}
template <class T>Listqueue<T>::~Listqueue(){if(front==NULL) return; node *temp; while(front!=NULL) { temp=front; front=front->link; delete temp; }
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 11
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}
void main(){ clrscr(); Listqueue <int>obj; int ch,flag=1; while(flag==1) { cout<<"\nMENU"; cout<<"\n1.Insert"; cout<<"\n2.Delete"; cout<<"\n3.Print"; cout<<"\n4.Exit"; cout<<"\nEnter your choice:"; cin>>ch; switch(ch) { case 1: cout<<"\nEnter the element to be inserted:"; int val; cin>>val; obj.Enqueue(val); cout<<"Do you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 2: cout<<obj.Dequeue(); cout<<"\nDo you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 3: obj.Print(); cout<<"\nDo you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 4: flag=0; break; default: cout<<"Enter the right choice!!!"; } } getch();}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 12
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 5 a)/* Convertion of infix expression into postfix expression */#include <iostream.h>#include <conio.h>#include <ctype.h>#include <string.h>class intopost{
char stack[30],infix[30],suffix[30];int top;public:
void push(char);char pop();int prior(char);void postfix();intopost(){
top=-1;}
};
void intopost :: push(char ch){
stack[++top]=ch;}char intopost :: pop(){
return stack[top--];}
int intopost :: prior(char ch){
if(ch=='('||ch=='#')return 1;
if(ch=='+'||ch=='-')return 2;
if(ch=='*'||ch=='/')return 3;
if(ch=='^'||ch=='$')return 4;
}
void intopost :: postfix(){int j=0;push('#');cout<<"\n Enter valid infix expression :";cin>>infix;for(int i=0;infix[i]!='\0';i++){
if(isalnum(infix[i]))suffix[j++]=infix[i];
else if(infix[i]=='(')
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 13
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
push(infix[i]);else if(infix[i]==')'){
while(stack[top]!='(')suffix[j++]=pop();
pop();}else{
while(prior(stack[top])>=prior(infix[i]))suffix[j++]=pop();
push(infix[i]);}
}while(stack[top]!='#')
suffix[j++]=pop();suffix[j]='\0';cout<<"\n The corresponding postfix expression is";cout<<suffix;
}
void main(){
intopost p;clrscr();p.postfix();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 14
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 5b)/* Convertion of infix expression into prefix expression */
#include <iostream.h>#include <conio.h>#include <ctype.h>#include <string.h>class intoprefix{
char stack[30],infix[30],suffix[30];int top;public:
void push(char);char pop();int prior(char);void prefix();intoprefix(){
top=-1;}
};
void intoprefix :: push(char ch){stack[++top]=ch;
}char intoprefix :: pop(){
return stack[top--];}
int intoprefix :: prior(char ch){
if(ch==')'||ch=='#')return 1;
if(ch=='+'||ch=='-')return 2;
if(ch=='*'||ch=='/')return 3;
if(ch=='^'||ch=='$')return 4;
}
void intoprefix :: prefix(){
int j=0;push('#');cout<<"\n Enter the valid infix expression:";cin>>infix;strrev(infix);for(int i=0;infix[i]!='\0';i++){
if(isalnum(infix[i]))
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 15
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
suffix[j++]=infix[i];else if(infix[i]==')')
push(infix[i]);else if(infix[i]=='('){
while(stack[top]!=')')suffix[j++]=pop();
pop();
}else{
while((prior(stack[top])>prior(infix[i]))&&(stack[top]!=')'))suffix[j++]=pop();
push(infix[i]);
}}
while(stack[top]!='#')suffix[j++]=pop();suffix[j]='\0';strrev(suffix);cout<<"\n The corresponding prefix expression is:";cout<<suffix;}
void main(){
intoprefix p;clrscr();p.prefix();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 16
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 5c)/* evaluation of posffix expression */
#include <iostream.h>#include <conio.h>#include <ctype.h>#include <math.h>#define MAX 50class eval{
int s[MAX],top;char p[20];double res;public :
eval(){
top=-1;}void push(double);double pop();double result(char, double, double);void suffixeval();
};
void eval :: push(double e){
s[++top]=e;}
double eval :: pop(){
return s[top--];}double eval :: result(char p,double op1,double op2){
switch(p){
case '+': return (op2+op1);case '-': return (op2-op1);case '*': return (op2*op1);case '/': return (op2/op1);case '^': return pow(op2,op1);
}}
void eval :: suffixeval(){
cout<<"\n Enter a valid postfix expression :";cin>>p;for(int i=0;p[i]!='\0';i++){
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 17
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
if(isdigit(p[i]))push((double)(p[i]-'0'));
else{
double op1,op2;op1=pop();op2=pop();double val=result(p[i],op1,op2);push(val);
}}res=s[top];cout<<"\n After Evaluating result is: "<<res;
}void main(){
eval e;clrscr();e.suffixeval();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 18
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 6)/* implementation of deque using doubly linked list */#include <iostream.h>#include <conio.h>template <class t>class dll{
private :struct node{
t data;struct node *next;struct node *prev;
}*head;public :
dll(){head=NULL;
}void create();void print();void insert_front();void insert_rear();void del_front();void del_rear();
};template <class t>void dll <t> :: create(){
node *n1,*last,*temp;char ans='y';do{
n1=new node;cout<<"\nEnter the data:";cin>>n1->data;n1->next=NULL;n1->prev=NULL;if(head==NULL){
head=n1;last=n1;
}else{
last->next=n1;n1->prev=last;last=n1;
}cout<<"\n \nEnter more ?";ans=getche();
}while(ans=='y'||ans=='Y');
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 19
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
cout<<"\n The list is created\n";getch();
}template <class t>void dll <t> ::print(){
node *temp;temp=head;if(temp==NULL){
cout<<"\n The list is empty\n";getch();return;
}else{
cout<<"\n The list is :";while(temp!=NULL){
cout<<temp->data<<" ";temp=temp->next;
}}getch();
}template <class t>void dll <t> ::insert_rear(){
node *temp,*n1;int val,flag=0;cout<<"\n Enter the data of the new node to insert";cin>>val;temp=head;if(temp==NULL)
flag=1;else{
while(temp->next!=NULL)temp=temp->next;
}n1=new node;if(n1==NULL)
cout<<"\n Unable to allocate memory\n";n1->data=val;n1->next=NULL;n1->prev=NULL;if(flag==0){
temp->next=n1;n1->prev=temp;
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 20
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
elsehead=n1;
cout<<"\n Node inserted ";}template <class t>void dll <t> :: insert_front(){
node *temp,*n1;int data;cout<<"\n Enter the data of the new node to insert";cin>>data;n1=new node;if(n1==NULL)
cout<<"\n Unable to allocate memory\n";n1->data=data;n1->next=NULL;n1->prev=NULL;if(head){
n1->next=head;head->prev=n1;
}head=n1;cout<<"\n Node inserted ";
}template <class t>void dll <t> :: del_front(){
node *curr, *temp;curr=head;if(curr==NULL)
cout<<"\n Node not found";else{
if(head->next==NULL && head->prev==NULL)head=NULL;
else{
head=curr->next;head->prev=NULL;
}}cout<<"\n The item deleted";delete curr;
}template <class t>void dll <t> :: del_rear(){
node *curr,*temp;curr=head;while(curr->next!=NULL)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 21
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
curr=curr->next;temp=curr->prev;temp->next=NULL;delete curr;cout<<" \n The item is deleted\n";
}void main(){
char ans='y';int ch;dll <int> d;dll <char> c;do{
cout<<"\n" <<"MENU";cout<<"\n n1.Create";cout<<"\n n2.Display";cout<<"\n n3.Insertion by front";cout<<"\n n4.Insertion by rear";cout<<"\n n5.Deletion by front";cout<<"\n n6.Deletion by rear";cout<<"\n Enter your choice:";cin>>ch;switch(ch){
case 1:d.create(); break;
case 2:d.print(); break;
case 3:d.print();d.insert_front();d.print(); break;
case 4:d.insert_rear(); d.print();
break;case 5:d.print();
d.del_front(); d.print();
break;case 6:d.print();
d.del_rear(); d.print();
break;default: cout<<"\n Invalid choice";
}cout<<"\n Do you want to continue?";ans=getch();
}while(ans=='y'||ans=='Y');cout<<"\n \n Enter the characters in a Queue"<<endl;c.create();
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 22
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
c.print();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 23
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 7)/* Implementation of Binary search tree for the following operations
a) Inserting an element into a binary search treeb) Deleting an element from a binary search treec) Searching for a key element in a binary search tree*/
#include<conio.h>#include<iostream.h>#include<stdlib.h>
struct node{
int element;node *left,*right;
};
typedef struct node *nodeptr;template <class t>class BSTree{
public:void insert(t,nodeptr &);void del(t, nodeptr &);int delet(nodeptr &);void search(t,nodeptr &);void display(nodeptr &, int);
};
template <class t>
void BSTree <t> ::insert( t x,nodeptr &p){
if(p==NULL){
p=new node;p->left=NULL;p->right=NULL;p->element=x;
}else{
if(x<p->element)insert(x,p->left);
else if(x>p->element)insert(x,p->right);
elsecout<<"\n Eelement already exist!";
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 24
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}
template <class t>void BSTree <t> ::del(t x,nodeptr &p){
nodeptr d;if(p==NULL)
cout<<"\n Element not found";else if(x<p->element)
del(x,p->left);else if(x>p->element)
del(x,p->right);else if((p->left==NULL)&&(p->right==NULL)){
d=p;delete d;p=NULL;cout<<"\n Element deleted";
}else if(p->left==NULL){
d=p;delete d;p=p->right;cout<<"Element deleted";
}else if(p->right==NULL){
d=p;p=p->left;delete d;cout<<"\n Element deleted";
}else
p->element=delet(p->right);}
template <class t>int BSTree <t>:: delet(nodeptr &p){
int c;if(p->left==NULL){
c=p->element;p=p->right;return c;
}else
c=delet(p->left);return c;
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 25
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
template <class t>void BSTree<t> :: search(t x, nodeptr &p){
if(p==NULL)cout<<"\n Element not found";
else{
if(x<p->element)search(x,p->left);
else if(x>p->element)search(x,p->right);
elsecout<<"\n Element found";
}}
template <class t>void BSTree <t>:: display(nodeptr &x,int level){
int i;if(x){
display(x->right,level+1);cout<<"\n";for(i=0;i<level;i++)
cout<<" ";cout<<x->element;display(x->left,level+1);
}}void main(){
BSTree <int>t;clrscr();nodeptr root=NULL;int item;int ch;do{
cout<<"\n\n MENU\n";cout<<"\n 1.Create 2.Display 3.Search 4.Delete 5.Exit\n";cin>>ch;switch(ch){
case 1:while(1) {
cout<<"Enter value:Enter -1 to stop:"; cin>>item; if(item==-1)
break;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 26
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
t.insert(item,root); } break;
case 2: t.display(root,1); break;
case 3:cout<<"\n Enter element to be inserted:"; cin>>item;
t.search(item,root); break;
case 4:cout<<"\n Enter the element to be deleted:"; cin>>item;
t.del(item,root); break;
case 5: exit(0);default: cout<<"\n Invalid choice";
}getch();
}while (1);
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 27
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 8)//implementation of circular queue ADT using Array#include<iostream.h>#include<conio.h>#define MAX 5template <class T>class Cirqueue{ T *Cqueue; int front,rear; public: Cirqueue(); void Insertitem(T val); T deleteitem(); void print(); ~Cirqueue();};template <class T>Cirqueue<T>::Cirqueue(){ Cqueue=new T[MAX]; front=rear=-1;}template <class T>void Cirqueue<T>::Insertitem(T val){ if(rear==-1) { front=rear=0; Cqueue[rear]=val; } else { if(((rear+1)%MAX)!=front) { rear=(rear+1)%MAX; Cqueue[rear]=val; } else cout<<"Queue is FULL!!!"; }}template <class T>T Cirqueue<T>::deleteitem(){ if(front==-1) { cout<<"Queue is EMPTY!!!"; return(0); } else
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 28
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
{ T val=Cqueue[front]; if(front==rear) front=rear=-1; else front=(front+1)%MAX; return val; }}template <class T>void Cirqueue<T>::print(){ if(front==-1) { cout<<"Queue is EMPTY!!!"; return; } else { for(int i=front;i<=rear;i++) cout<<Cqueue[i]<<" "; } if(front>rear) { for(int m=front;m<MAX;m++) cout<<Cqueue[m]<<" "; for(int n=0;n<=rear;n++) cout<<Cqueue[n]<<" "; }}template <class T>Cirqueue<T>::~Cirqueue(){ delete[]Cqueue;}
int main(){ clrscr(); Cirqueue <int>obj; int ch,flag=1; while(flag==1) { cout<<"\nMENU"; cout<<"\n1.Insert"; cout<<"\n2.Delete"; cout<<"\n3.Print"; cout<<"\n4.Exit"; cout<<"\nEnter your choice:"; cin>>ch; switch(ch)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 29
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
{ case 1: cout<<"\nEnter the element to be inserted:"; int val; cin>>val; obj.Insertitem(val); cout<<"Do you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 2: cout<<"\nDeleted item is:"; cout<<obj.deleteitem(); cout<<"\nDo you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 3: cout<<"\nElements in the queue are:"; obj.print(); cout<<"\nDo you want to continue?(Yes=1 or No=0):"; cin>>flag; break; case 4: flag=0; break; default: cout<<"Enter the right choice!!!"; } } return 0; getch();}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 30
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 9)/*Using non-recursive functions to traversing the given binary tree in
a) Preorder b) Inorder c) Postorder */#include<iostream.h>#include<conio.h>#include<stdlib.h>int top=-1,top1=-1,ch;template<class t>class bst{struct node /* definig structure for node */{ public:t data;node *left,*right;}*root,*a[20],*b[20];public:bst(){root=NULL;}struct node *insert(node *r,t val){if(r==NULL) /* insertion for first element */{r=new node;r->data=val;r->left=NULL;r->right=NULL;}else{if(r->data<val)r->right=insert(r->right,val);elser->left=insert(r->left,val);}return(r);}struct node *deletion(node *r,t val){node *data,*d,*x,*p,*i;int f=0;x=r;p=NULL;while(x!=NULL){if(x->data==val) /* search for element to delete */{d=x;f=1;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 31
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
break;}else if(val<x->data){p=x;x=x->left;}else{p=x;x=x->right;}}if(f==0)cout<<"\n element not found";else /* deletion of element */{if(d->left!=NULL && d->right!=NULL){p=d;i=d->left;while(i->right!=NULL){p=i;i=i->right;}d->data=i->data;d=i;}if(d->left!=NULL && d->right==NULL){if(p==NULL)r=d->left;else if(d==p->left)p->left=d->left;elsep->right=d->right;}if(d->left==NULL && d->right!=NULL){if(p==NULL)r=NULL;else if(d==p->left)p->left==NULL;elsep->right=NULL;}cout<<"\n elements deleted ";return(r);}}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 32
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
void inorder_non_rec(node *r) /* inorder traversal */{node *l;l=r;do{while(l!=NULL){push(l);l=l->left;}while(top>-1){l=pop();cout<<" "<<l->data;if(l->right!=NULL){l=l->right;break;}elsel=NULL;}}while(l!=NULL);}void preorder_non_rec(node *r) /* for pre order traversal */{node *l;l=r;do{cout<<" "<<l->data;if(l->right!=NULL)push(l->right);l=l->left;if(l==NULL && top>-1)l=pop();}while(l!=NULL);}void postorder_non_rec(node *r) /* for post order traversal */{node *l;l=r;do{while(l!=NULL){push(l);if(l->right!=NULL){push(l->right);
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 33
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
b[++top1]=l->right;}l=l->left;}do{l=pop();if(l!=b[top1])cout<<" "<<l->data;else{top1=top1-1;break;}}while(top>-1);}while(l!=NULL && top>-1);}void push(node *r) /* function for pushing */{top=top+1;a[top]=r;}struct node *pop() /* function for popping */{return a[top--];}int function() /* function for calling operations */{t element;cout<<"\nmenu \n 1 insert 2 inorder 3 preorder 4 post order 5 delete 6 exit\n";do{cout<<"\n enter ur choice ";cin>>ch;switch(ch){case 1:cout<<"\n enter element to be inserted:";
cin>>element;root=insert(root,element);break;
case 2:cout<<"\n rec inorder traversal:";inorder_non_rec(root);break;
case 3:cout<<"\n rec preorder traversal :";preorder_non_rec(root);break;
case 4:cout<<"\n rec post order traversal:";postorder_non_rec(root);break;
case 5:cout<<"\n enter element to be deleted :";cin>>element;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 34
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
deletion(root,element);break;
case 6:return(0);}}while(ch<6);}};
void main(){bst<int>b1;bst<float>b2;bst<char>b3;clrscr();while(1){cout<<"\n data type \n 1 int 2 float 3 char 4 exit \n";cout<<"enter data type \n";cin>>ch;switch(ch){case 1:b1.function();
break;case 2:b2.function();
break;case 3:b3.function();
break;case 4:exit(1);}}getch();}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 35
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 10 a)// Program for the implementation of Breadth First Search of graph#include <iostream.h>#include <conio.h>#include <stdlib.h>#define max 10class que{
private :int arr[max];int rear,front;
public:void insert(int);int del();int isqempty();int isqful();que(){
front=rear=-1;}
};
void que::insert(int item){
if(isqful())cout<<"\n Queue is full";
else{
rear++;arr[rear]=item;if(front==-1)
front=0;}
}
int que::del(){
int item;if(isqempty()){
cout<<"\nqueue is empty";return NULL;
}else{
item=arr[front];if(front==rear)
front=rear=-1;else
front++;return item;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 36
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}}int que:: isqempty(){
if(front==-1)return 1;
elsereturn 0;
}
int que::isqful(){
if(rear==max-1)return 1;
elsereturn 0;
}que q;class graph{
private :int adj[max][max];int vst[max];int n;
public :graph(){
n=0;}void buildadjm();void bfs(int x);void menu();
};
void graph::buildadjm(){
int i,j;cout<<"\n Enter the graph in the matrix form:\n";for(i=1;i<=n;i++){
cout<<"\nEnter "<<i<<"row";for(j=1;j<=n;j++)
cin>>adj[i][j];}
}void graph::bfs(int x){
int j,k;if(vst[x]==0){
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 37
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
vst[x]=1;cout<<" "<<x;
}
for(;;){
for (j=1;j<=n ;j++ ){
if((adj[x][j]==1)&&(vst[j]==0)){
q.insert(j);vst[j]=1;
}if(q.isqempty())
return;else{
k=q.del();cout<<" "<<k;
}}
}}void graph::menu(){
int i;cout<<"\n\n Enter number of nodes:";cin>>n;buildadjm();for(i=1;i<=n;i++)
vst[i]=0;cout<<"\n The bfs traversal is as follows:\n";for(i=1;i<=n;i++)
if(vst[i]==0)bfs(i);
getch();}
void main(){
clrscr();graph g;g.menu();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 38
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 10 b)// Program for the implementation of Depth First Search of graph#include <iostream.h>#include <conio.h>#include <stdlib.h>#define max 10class graph{
private:int adj[max][max];int vst[max];int n;public :graph(){
n=0;}void buildadjm();void dfs(int x);void menu();
};
void graph::buildadjm(){
int i,j;cout<<"\n Enter the graph in the matrix form:\n";for(i=1;i<=n;i++){
cout<<"\nEnter "<<i<<"Row :";for(j=1;j<=n;j++)
cin>>adj[i][j];}
}void graph::dfs(int x){
int j;vst[x]=1;cout<<" "<<x;for(j=1;j<=n;j++){
if((adj[x][j]==1)&&(vst[j]==0))dfs(j);
}}void graph::menu(){
int i;cout<<"\n\n Enter number of nodes:";cin>>n;buildadjm();for(i=1;i<=n;i++)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 39
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
vst[i]=0;cout<<"\n The dfs traversal is as follows:\n";
for(i=1;i<=n;i++)if(vst[i]==0)
dfs(i);getch();
}void main(){
clrscr();graph g;g.menu();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 40
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 11 a)// Program for the implementation of Quick sort
#include<iostream.h>#include<stdlib.h>#include<conio.h>void quick(int,int);int a[100];void main(){
int i,n,lo,hi;clrscr();cout<<"enter no.of elements";cin>>n;cout<<"enter elements";for(i=0;i<n;i++)
cin>>a[i]; /* reading elements */lo=0; hi=n-1;quick(lo,hi);cout<<"sorted elements are:\n";for(i=0;i<n;i++)
cout<<a[i]<<"\t";getch();
}void quick(int l,int h) /* using quick sort */{
int i,j,pivot,pin,t;i=l+1;j=h;pivot=a[l];pin=l;if(l<h){
do{
while(a[i]<pivot&&i<=h)i++;
while(a[j]>pivot&&j>=l)j--;
if(i<j){ /* swapping */
t=a[j];a[j]=a[i];a[i]=t;
}}while(i<j);t=a[pin];a[pin]=a[j];a[j]=t;quick(l,j-1);quick(j+1,h);
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 41
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 42
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 11 b)// Program for the implementation of Merge sort
#include<iostream.h>#include<conio.h>void mergesort(int,int);void merge(int,int,int);int a[10],b[10];void main(){
int i,n,lo,hl;clrscr();cout<<"enetr no.of elemnets :";cin>>n;cout<<"enetr elements into array";for(i=0;i<n;i++)
cin>>a[i]; /*reading elements */lo=0;hl=n-1;mergesort(lo,hl);cout<<"sorted elements are:\n";for(i=0;i<n;i++)
cout<<"\t"<<a[i];getch();
}void mergesort(int l,int h){
int mid;if(l<h){
mid=(l+h)/2; /* breaking array into two parts */mergesort(l,mid);mergesort(mid+1,h);merge(l,mid,h);
}}void merge(int l,int mid,int h){
int i,j,k,hl;i=l;j=mid+1;k=l;while(i<=mid&&j<=h){
if(a[i]<a[j]){
b[k]=a[i];i++;
}else{
b[k]=a[j];
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 43
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
j++;}k++;
}if(i>mid) /* finding largest element from two parts */{
for(hl=j;hl<=h;hl++)b[k++]=a[hl];
}else if(j>h){
for(hl=i;hl<=mid;hl++)b[k++]=a[hl];
}for(k=l;k<=h;k++)
a[k]=b[k];}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 44
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 11 c)// Program for the implementation of Heap sort
#include<iostream.h>#include<conio.h>int x[10],i,n;void heap(int);void maxheap(int);void main(){
clrscr();cout<<"enter no of elements";cin>>n;cout<<"enetr the elements";for(i=1;i<=n;i++)
cin>>x[i]; /* reading elements */heap(n);cout<<"sorted elements are:";for(i=1;i<=n;i++)
cout<<x[i]<<"\t";}void heap(int n){
int t;while(n>1){
maxheap(n); /* calling max heap */t=x[1];x[1]=x[n];x[n]=t;n=n-1;
}}void maxheap(int n){
int i,t,j;for(i=2;i<=n;i++){
t=x[i];j=i;while(x[j/2]<t&&j>1){
x[j]=x[j/2];j=j/2;
}x[j]=t;
}}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 45
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 12 a) // Program to implement linear search in both recursive and non-recursive#include <iostream.h>#include <conio.h>#include <stdlib.h>
template <class t>class search{
t a[20],key;int n;void getdata();int linsearch(t);
public:void menu();
};template <class t>void search<t>::getdata(){
cout<<"\n Enter the size of array:";cin>>n;cout<<"\n Enter array Elements\n";for(int i=1;i<=n;i++)
cin>>a[i];}
template <class t>int search<t>::linsearch(t key){
for(int i=1;i<=n;i++){
if(a[i]==key)return i;
}return -1;
}
template <class t>void search<t> :: menu(){
int pos,ch;getdata();cout<<"\n Enter the key value:";cin>>key;pos=linsearch(key);if(pos!=-1)
cout<<"\n The key"<< key <<" is found at postion:"<<pos;else
cout<<"\n Serach is unsuccessful";getch();
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 46
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
}
void main(){
clrscr();search <int> s1;search <char> s2;do{
char ch;cout<<"\n MENU \n";cout<<"\n 1.Integer 2.Character 3.Exit";cout<<"\n Enter your choice:";cin>>ch;switch(ch){
case '1':s1.menu(); break;
case '2':s2.menu();
break;case '3':
exit(0);default: cout<<"\n Invalid choice";
}getch();
}while (1);
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 47
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 12 b)// Program to implement Binary search in both recursive and non-recursive#include <iostream.h>#include <conio.h>#include <stdlib.h>template <class t>class search{
t a[20],key;int n;void getdata();void showdata();void sort();int binsearch(t);public:
void menu();};
template <class t>void search <t> ::getdata(){
cout<<"\n Enter size of array:";cin>>n;cout<<"\n Enter array elements\n";for(int i=0;i<n;i++)
cin>>a[i];}
template <class t>void search <t> ::showdata(){
for(int i=0;i<n;i++)cout<<" "<<a[i];
}
template <class t>void search <t> ::sort(){
for (int i=0;i<n ;i++ ){
for(int j=i+1;j<n;j++){
if(a[i]>a[j]){ t temp=a[i]; a[i]=a[j]; a[j]=temp;}
}}
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 48
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
template <class t>int search <t> ::binsearch(t key){
int low,high,mid;low=0;high=n-1;while(low<=high){
mid=(low+high)/2;if(a[mid]==key)
return mid;else
if(key>a[mid])low=mid+1;
elsehigh=mid-1;
}return -1;
}template <class t>void search <t> ::menu(){
int pos,ch;getdata();cout<<"\n Before sorting";showdata();sort();cout<<"\n After sorting";showdata();cout<<"\n Enter key value:";cin>>key;pos=binsearch(key);if(pos!=-1)
cout<<"The key"<<key<<" is found at position "<< pos;else
cout<<"\n Search is unsuccessful";}void main(){
clrscr();search <int> s1;search <char> s2;do{
char ch;cout<<"\n MENU\n";cout<<" 1.Integer 2.Character 3.Exit";cout<<"\n Enter your choice:";cin>>ch;switch(ch){
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 49
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
case '1':s1.menu();break;
case '2':s2.menu();break;
case '3':exit(0);default: cout<<"\n Invalid choice!";getch();
} }while(1);}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 50
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 13)// Program to implement AVL tree to for the following operations
a) Insertion into AVL tree b) Deletion from an AVL tree
#include <iostream.h>#include <conio.h>#define FALSE 0#define TRUE 0struct node{
int data;node *left;node *right;int height;
};
typedef node *nodeptr;template <class t>class avl{
public :void insert(t,nodeptr &);void delet(t,nodeptr &);void show(nodeptr &,int);int avlheight(nodeptr);int deletmax(nodeptr &);nodeptr sinrotleft(nodeptr &);nodeptr sinrotright(nodeptr &);nodeptr dourotleft(nodeptr &);nodeptr dourotright(nodeptr &);int max(t,t);
};template <class t>void avl <t> :: insert (t x, nodeptr &p){
if(p== NULL){
p=new node;p->data=x;p->left=NULL;p->right=NULL;p->height=0;
}else{
if(x<p->data){
insert(x,p->left);if(avlheight(p->left)-avlheight(p->right) ==2){
if(x<p->left->data)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 51
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
p=sinrotleft(p);else p=dourotleft(p);
}}else{if(x>p->data)
{insert(x,p->right);if((avlheight(p->right)-avlheight(p->left) == 2){
if(x<p->right->data) p=sinrotleft(p);else p=dourotleft(p);
}
}else{
cout<<"\n Element already exist";return;
}
}int m,n,d;m=avlheight(p->left);n=avlheight(p->right);d=max(m,n);p->height=d+1;
}
template <class t>void avl <t> :: show(nodeptr &x,t level){
int i;if(x){
show(x->right,level+1);cout<<"\n";for(i=0;i<level;i++)
cout<<" ";cout<<" "<<x->data;show(x->left,level+1);
}}template <class t>nodeptr avl <t> :: dourotleft(nodeptr &p1)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 52
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
{p1->left=sinrotright(p1->left);return sinrotleft(p1);
}template <class t>nodeptr avl <t> :: dourotright(nodeptr &p1){
p1->right=sinrotright(p1->right);return sinrotright(p1);
}template <class t>nodeptr avl <t> :: sinrotright(nodeptr &p1){
nodeptr p2;p2=p1->right;p1->right=p2->left;p2->left=p1;p1->height=max(avlheight(p1->left),avlheight(p1->right));p2->height=max(p1->height,avlheight(p2->right)+1);return p2;
}template <class t>nodeptr avl <t> :: sinrotleft(nodeptr &p1){
nodeptr p2;p2=p1->left;p1->left=p2->right;p2->right=p1;p1->height=max(avlheight(p1->left),avlheight(p1->right));p2->height=max(avlheight(p2->left),p1->height+1);return p2;
}template <class t>void avl <t> :: delet(t x,nodeptr &p){
nodeptr d;if(p==NULL)
cout<<"\n Element not found!";else
if(x<p->data){
delet(x,p->left); if((avlheight(p->right)-avlheight(p->left))==2) {
int u,pu,gu;gu=p->data;pu=p->right->data;if(p->right->right==NULL)
u=p->right->left->data;else
u=p->right->right->data;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 53
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
if(gu<pu && pu<u)p=sinrotright(p);
elsep=dourotright(p);
}}else if(x>p->data) {
delet(x,p->right);if((avlheight(p->left)-avlheight(p->right))==2){
int u,pu,gu;gu=p->data;pu=p->left->data;if(p->right->right==NULL)
u=p->left->right->data;else
u=p->left->left->data;if(gu>pu && pu>u)
p=sinrotright(p);else
p=dourotright(p);}
}else
if((p->left==NULL) && (p->right==NULL)) {
d=p;delete d;p=NULL;cout<<"\n Element deleted!";return;
}else if(p->left==NULL) {
d=p;delete d;p=p->right;cout<<"\n Element deleted";return;
}else if(p->right==NULL) {
d=p;delete d;p=p->left;cout<<"\n Element deleted";return;
}else{
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 54
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
p->data=deletmax(p->left);int m,n,d1;m=avlheight(p->left);n=avlheight(p->right);d1=max(m,n);p->height=d1+1;
} }template <class t>int avl <t> :: avlheight(nodeptr p){
int t;if(p==NULL)
return 0;else{
t=p->height;return t;
}}
template <class t>int avl <t> :: deletmax(nodeptr &p){
int c;if(p->right==NULL){
c=p->data;p=p->left;return c;
}else{
c=deletmax(p->right);return c;
}}
template <class t>int avl <t> :: max(t v1,t v2){
return ((v1>v2)?v1:v2);}void main(){
clrscr();nodeptr root=NULL;int element;avl <int> a1;while(1)
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 55
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
{cout<<"\nEnter elements to insert(Enter -1 to exit) :";cin>>element;if(element==-1)
break;a1.insert(element,root);
}cout<<"\n The tree after insertion:";a1.show(root,1);cout<<"\n Enter the element to be deleted:";cin>>element;cout<<"\n the tree after deletion :\n";a1.show(root,1);cout<<"\n";getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 56
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
Program 14)// Program to implement Kruskal’s algorithm to generate a minimum cost spanning tree#include <iostream.h>#include <conio.h>#define INFINITY 999template <class t>class kruskal{
private:typedef struct Graph{int v1;int v2;t cost;}GR;GR G[20];
public :int tot_edges,tot_nodes;void create();void spanning_tree();void get_input();int minimum(int);
};
int Find(int v2,int parent[]){
while(parent[v2]!=v2){
v2=parent[v2];}return v2;
}
void Union(int i,int j,int parent[]){
if(i<j)parent[j]=i;
elseparent[i]=j;
}template <class t>void kruskal <t>::get_input(){
cout<<"\n Enter Total number of nodes: ";cin>>tot_nodes;cout<<"\n Enter Total number of edges: ";cin>>tot_edges;
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 57
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
template <class t>void kruskal <t>::create(){
for(int k=0;k<tot_edges;k++){
cout<<"\n Enter Edge in (v1, v2) from";cin>>G[k].v1>>G[k].v2;cout<<"\n Enter Corresponding cost ";cin>>G[k].cost;
}}template <class t>int kruskal <t>::minimum(int n){
int i,small,pos;small=INFINITY;pos=-1;for(i=0;i<n;i++){
if(G[i].cost<small){
small=G[i].cost;pos=i;
}}return pos;
}template <class t>void kruskal <t>::spanning_tree(){
int count,k,v1,v2,i,j,tree[10][10],pos,parent[10];t sum;count=0;k=0;sum=0;for(i=0;i<tot_nodes;i++)
parent[i]=i;while(count!=tot_nodes-1){
pos=minimum(tot_edges);if(pos==-1)
break;v1=G[pos].v1;v2=G[pos].v2;i=Find(v1,parent);j=Find(v2,parent);if(i!=j){
tree[k][0]=v1;tree[k][1]=v2;k++;
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 58
Y.S.R ENGINEERING COLLEGE OF YOGIVEMANA UNIVERSITY::PRODDATUR
count++;sum+=G[pos].cost;Union(i,j,parent);
}G[pos].cost=INFINITY;
}if(count==tot_nodes-1){
cout<<"\n Spanning tree is..."<<endl;cout<<"\n _____________"<<endl;for(i=0;i<tot_nodes-1;i++){
cout<<"["<<tree[i][0];cout<<" - ";cout<<tree[i][1]<<"]"<<endl;
}cout<<"\n ___________"<<endl;cout<<"Cost of spanning tree is = "<<sum<<endl;
}else{
cout<<"There is no spanning Tree"<<endl;}
}
void main(){
kruskal <int> obj;clrscr();cout<<"\n \t Graph creation ";obj.get_input();obj.create();obj.spanning_tree();getch();
}
COMPUTER SCIENCE AND ENGINEERING(2012-13) Page 59