Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
II-I Semester (R16)
KALLAM HAR
Departme
Data s
Data struct
1
RANADHAREDDY INSTITUTE OF TEC
ent of Computer Science and Enginee
2nd
Year 1st Semester
structure through C++ Lab (2017-18)
R16
Lab Manual
cture through C++ Lab (2017-18)
CHNOLOGY
eering
8)
II-I Semester (R16) Data structure through C++ Lab (2017-18)
2
S.NO Title Date Page_NO
1 Write a C++ program for addition of two polynomial
expressions?
19-06-2017 3
2 Implementation of Multistack in a Single Array?
29-06-2017 5
3 Write a program to implement the operations on
Circular Queue?
03-07-2017 9
4 Write a program to implement Single linked list?
03-07-2017 13
5 Write a program to perform Binary Search Operation
using divide and conques concept?
10-07-2017 18
6 Implementation of Doubly linked list?
17-07-2017 20
7 Implementation of Binary Search trees.
24-07-2017 25
8 Implementation of Hash table.
31-07-2017 32
9 Implementation of Breadth First Search Techniques.
21-08-2017 34
10 Implementation of Breadth First Search Techniques.
28-08-2017 36
11 Write a program to Implement Prim’s Algorithm?
04-09-2017 38
12 Write a program to implement Kruskal’s Algorithm?
04-09-2017 39
13 Write a program to implement Dijkstra’s Algorithm?
11-09-2017 41
14 Write a program to implement Merge Sort?
11-09-2017 42
15 Write a program to implement Quick Sort?
18-09-2017 43
II-I Semester (R16) Data structure through C++ Lab (2017-18)
3
1. Write a C++ program for addition of two polynomial expressions?
#include<iostream.h>
#include<conio.h>
class poly
{
struct
{ int coef;
int expo;
} term[20];
int degree;
public:
void input()
{ int i;
cout<<"Enter number of terms";
cin>>degree;
cout<<"Enter coefficients and exponents for " << degree <<"
terms"<<endl;
for(i=0;i<degree;i++)
cin>>term[i].coef>>term[i].expo;
}
void add(poly A, poly B)
{
int i,j,k,l;
degree=A.degree+B.degree;
i=0;j=0;k=0;
while(i<A.degree && j<B.degree)
{
if( A.term[i].expo<B.term[j].expo)
{ term[k].coef=B.term[j].coef;
term[k].expo=B.term[j].expo;
j++;
k++;
}
if( A.term[i].expo>B.term[j].expo)
{ term[k].coef=A.term[i].coef;
term[k].expo=A.term[i].expo;
i++;
k++;
}
if( A.term[i].expo==B.term[j].expo)
{ term[k].coef=A.term[i].coef+B.term[j].coef;
term[k].expo=A.term[i].expo;
i++;
j++;
k++;
}
}
if(i<A.degree)
{ for(l=i; l<A.degree; l++)
{ term[k].coef=A.term[l].coef;
II-I Semester (R16) Data structure through C++ Lab (2017-18)
4
term[k].expo=A.term[l].expo;
k++;
}
}
if(j<B.degree)
{ for(l=j; l<B.degree; l++)
{ term[k].coef=B.term[l].coef;
term[k].expo=B.term[l].expo;
k++;
}
}
degree=k;
}
void display()
{ int i;
cout<<"polynomial is"<<endl;
for(i=0;i<degree;i++)
cout<<term[i].coef<<"x"<<term[i].expo<<" ";
}
};
main()
{
poly A,B,C;
cout<<"Enter Polynomial A:"<<endl;
A.input();
cout<<"Enter Polynomial B:"<<endl;
B.input();
C.add(A,B);
C.display();
getch();
}
Output:
Enter Polynomial A:
Enter number of terms 3
Enter coefficients and exponents for 3 terms
3 2 4 1 6 0
Enter Polynomial B:
Enter number of terms 3
Enter coefficients and exponents for 3 terms
5 2 5 1 8 0
Polynomial is
8x2 9x1 14x0
II-I Semester (R16) Data structure through C++ Lab (2017-18)
5
2. Implementation of Multistack in a Single Array?
#include <iostream.h>
#define max 10
class multistack
{
int top1, top2, stk_arr[max];
public:
multistack()
{
top1=-1;
top2=max;
}
void push()
{
int x,ch;
if(top1==top2-1)
{
cout<<"stack overflow "<<endl;
return;
}
cout<<"enter a no \n";
cin>>x;
cout<<"\n press 1 to push in stack1 or press 2 for stack2:";
cin>>ch;
if(ch==1)
stk_arr[++top1]=x;
else
stk_arr[--top2]=x;
cout<< x <<" element is successfully pushed \n";
return;
}
void pop()
{
int y,ch;
cout<<"\n press 1 to pop from stack1 or press 2 for stack2";
cin>>ch;
if(ch==1)
{
if(top1==-1)
{
cout<<"stack underflow\n";
return;
}
y=stk_arr[top1];
stk_arr[top1--]=0;
}
else
{
if(top2==max)
{
cout<<"stack underflow\n";
II-I Semester (R16) Data structure through C++ Lab (2017-18)
6
return;
}
y=stk_arr[top2];
stk_arr[top2++]=0;
}
cout<<y<< "\n element is successfully poped from stack \n";
return;
}
void display()
{
int i;
if (top1 == -1)
{
cout<<"stack 1 is empty \n";
}
else
{
cout<<"elements of Stack 1 are : \n";
for (i = 0; i <= top1; i++)
{
cout<<stk_arr[i]<<endl;
}
}
if (top2 == max)
{
cout<<"stack 2 is empty \n";
}
else
{
cout<<"elements of Stack 2 are : \n";
for (i = max-1; i >= top2; i--)
{
cout<<stk_arr[i]<<endl;
}
}
return ;
}
};
main()
{
multistack s;
int ch;
do
{
cout<<"\n 1:push\n 2:pop\n 3:display\n 4:exit\n choice:";
cin>>ch;
switch (ch)
{
case 1:s.push();
break;
case 2:s.pop();
II-I Semester (R16) Data structure through C++ Lab (2017-18)
7
break;
case 3:s.display();
break;
case 4:cout<<"exiting from program\n";
break;
default:cout<<"wrong choice\n";
break;
}
}while(ch!=4);
}
Output:
1:Push
2:Pop
3:Display
4:Exit
Choice: 1
Enter a no 10
Press 1 to push in stack 1 or press 2 for stack 2 : 1
10 element is successfully pushed
1:Push
2:Pop
3:Display
4:Exit
Choice: 1
Enter a no 20
Press 1 to push in stack 1 or press 2 for stack 2 : 1
20 element is successfully pushed
1:Push
2:Pop
3:Display
4:Exit
Choice: 1
Enter a no 100
Press 1 to push in stack 1 or press 2 for stack 2 : 2
100 element is successfully pushed
1:Push
2:Pop
3:Display
4:Exit
Choice: 3
Elements of stack 1 are:
II-I Semester (R16) Data structure through C++ Lab (2017-18)
8
20
10
Elements of stack 2 are:
200
100
1:Push
2:Pop
3:Display
4:Exit
Choice: 2
Press 1 to pop from stack 1 or press 2 for stack 2: 1
20
Element is successfully poped from stack
1:Push
2:Pop
3:Display
4:Exit
Choice: 2
Press 1 to pop from stack 1 or press 2 for stack 2: 2
200
Element is successfully poped from stack
1:Push
2:Pop
3:Display
4:Exit
Choice: 3
Elements of stack 1 are:
10
Elements of stack 2 are:
100
II-I Semester (R16) Data structure through C++ Lab (2017-18)
9
3. Write a program to implement the operations on Circular Queue?
#include <iostream.h>
#define max 5
class cqueue
{
int front,rear, cq[max];
public:
cqueue()
{
front= -1;
rear = -1;
}
void insert()
{
int x;
if(front==(rear+1)%max )
{
cout<<"Circular queue overflow "<<endl;
return;
}
cout<<"enter a no \n";
cin>>x;
rear=(rear+1)%max;
cq[rear]=x;
if(front==-1) front=0;
}
void del()
{
int y;
if(front==rear && rear==-1)
{
cout<<"circular queue underflow\n";
return;
}
y=cq[front];
cq[front]=0;
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}
void display()
{
int i;
if (front==rear && rear==-1)
{
II-I Semester (R16) Data structure through C++ Lab (2017-18)
10
cout<<"circular is empty \n";
return;
}
cout<<"elements of circular queue are : \n";
if(front<=rear)
for (i = front; i <= rear; i++)
cout<<cq[i]<<endl;
else
{for (i = front; i <= max-1; i++)
cout<<cq[i]<<endl;
for (i = 0; i <= rear; i++)
cout<<cq[i]<<endl;
}
}
};
main()
{
cqueue q;
int ch;
do
{
cout<<"\n 1:insert\n 2:del\n 3:display\n 4:exit\n choice:";
cin>>ch;
switch (ch)
{
case 1:q.insert();
break;
case 2:q.del();
break;
case 3:q.display();
break;
case 4:cout<<"exiting from program\n";
break;
default:cout<<"wrong choice\n";
break;
}
}while(ch!=4);
}
Output:
1:insert
2:del
3:display
4:exit
Choice :1
Enter a no 10
1:insert
2:del
3:display
II-I Semester (R16) Data structure through C++ Lab (2017-18)
11
4:exit
Choice : 1
Enter a no 20
1:insert
2:del
3:display
4:exit
Choice : 1
Enter a no 30
1:insert
2:del
3:display
4:exit
Choice : 1
Enter a no 40
1:insert
2:del
3:display
4:exit
Choice : 1
Enter a no 50
1:insert
2:del
3:display
4:exit
Choice : 3
Elements of circular queue are:
10
20
30
40
50
1:insert
2:del
3:display
4:exit
Choice : 1
Circular queue overflow
1:insert
2:del
3:display
4:exit
II-I Semester (R16) Data structure through C++ Lab (2017-18)
12
Choice : 2
1:insert
2:del
3:display
4:exit
Choice : 2
1:insert
2:del
3:display
4:exit
Choice : 2
1:insert
2:del
3:display
4:exit
Choice : 3
Elements of circular queue are: 40
50
1:insert
2:del
3:display
4:exit
Choice : 2
1:insert
2:del
3:display
4:exit
Choice :2
Circular queue underflow
1:insert
2:del
3:display
4:exit
Choice :4
II-I Semester (R16) Data structure through C++ Lab (2017-18)
13
4. Write a program to implement Single linked list?
#include<iostream.h>
class Node
{
public:
int data;
Node* next;
};
class List:public Node
{
Node *start;
public:
List()
{
start=NULL;
}
void create()
{int num;
Node *temp=new Node;
if(start==NULL)
{
cout<<"\nEnter an Element:";
cin>>num;
temp->data=num;
temp->next=NULL;
start=temp;
}
else
{
cout<<"single linked list has an Elements, can not create new
list";
}
}
void insert()
{
Node *prev,*last;
int count,pos,ch,num;
Node *temp=new Node;
cout<<"\nEnter an Element:";
cin>>num;
temp->data=num;
temp->next=NULL;
cout<<"\nINSERT AS\n1:startNODE\n2:LASTNODE\n3:At specific
Node";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
II-I Semester (R16) Data structure through C++ Lab (2017-18)
14
temp->next=start;
start=temp;
break;
case 2:
last=start;
while(last->next!=NULL)
last=last->next;
last->next=temp;
break;
case 3:
Node *p,*n;
cout<<"\nEnter the Position to Insert:";
cin>>pos;
n=start;
while(count!=pos)
{
p=n;
n=n->next;
count++;
}
if(count==pos)
{
p->next=temp;
temp->next=n;
}
else
cout<<"\nNot Able to Insert";
break;
}
}
void del()
{
Node *prev,*last=start;
int count=1,pos,ch;
cout<<"\nDELETE\n1:startNODE\n2:LASTNODE\n3:At specific Node";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(start!=NULL)
{
cout<<"\nDeleted Element is "<<start->data;
start=start->next;
}
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(last->next!=NULL)
{
II-I Semester (R16) Data structure through C++ Lab (2017-18)
15
prev=last;
last=last->next;
}
cout<<"\nDeleted Element is: "<<last->data;
prev->next=NULL;
break;
case 3:
Node *p,*n;
cout<<"\nEnter the Position to Insert:";
cin>>pos;
n=start;
while(count!=pos)
{
p=n;
n=n->next;
count++;
}
if(count==pos)
{
cout<<"\nDeleted Element is: "<<n->data;
p->next=n->next;
}
else
cout<<"\nNot Able to Delete";
break;
}
}
void display()
{
Node *last=start;
if(last==NULL)
{
cout<<"\nList is Empty";
}
while(last!=NULL)
{
cout<<last->data;
cout<<"-->";
last=last->next;
}
cout<<"NULL";
}
};
int main()
{
List l;
int ch;
while(1)
{
cout<<"\n**** MENU ****";
cout<<"\n1:CREATE FRIST NODE
II-I Semester (R16) Data structure through C++ Lab (2017-18)
16
\n2:INSERT\n3:DELETE\n4:DISPLAY\n5:EXIT\n";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.del();
break;
case 4:
l.display();
break;
case 5:
return 0;
}
}
return 0;
}
Output:
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 1
Enter an element 10
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 2
Enter an element 20
Insert as
1:startNODE
2:LASTNODE
3:At specific Node
Enter your choice 1
II-I Semester (R16) Data structure through C++ Lab (2017-18)
17
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 2
Enter an element 30
Insert as
1:startNODE
2:LASTNODE
3:At specific Node
Enter your choice 3
Enter the position to insert 3
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 4
20�10�30�NULL
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 2
Enter an element 40
Insert as
1:start node
2:last node
3:at specific node
Enter your choice 2
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 3
II-I Semester (R16) Data structure through C++ Lab (2017-18)
18
Delete
1:start node
2:last node
3:at specific node
Enter your choice 3
Enter the position to insert 2
Deleted element is 10
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 4
20�30�40�NULL
*** MENU ***
1:CREATE FRIST NODE
2:INSERT
3:DELETE
4:DISPLAY
5:EXIT
Enter your choice 5
5. Write a program to perform Binary Search Operation using divide and conques concept?
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],i,key,n,result;
int low,mid,high;
void binarysearch(int a[],int key,int low,int high);
clrscr();
cout<<"how many numbers you want to enter?";
cin>>n;
cout<<"\n enter "<<n<<"elements in ascending order\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n enter the elements to be searched";
II-I Semester (R16) Data structure through C++ Lab (2017-18)
19
cin>>key;
low=0;
high=n-1;
binarysearch(a,key,low,high);
getch();
}
void binarysearch(int a[],int key,int low,int high)
{
int mid;
if(low>high)
{ cout<<"Search element not found";
return;
}
else
{
mid=(low+high)/2;
if(key==a[mid])
{
cout<<"key elements is found at posion"<<mid+1;
return ;
}
if(key<a[mid])
{
high=mid-1;
binarysearch(a,key,low,high);
}
if(key>a[mid])
{
low=mid+1;
binarysearch(a,key,low,high);
}
}
}
Output:
How many numbers you want to enter? Enter 5 numbers in ascending order
10
20
30
40
50
Enter the element to be searched 10 key element is found position.
II-I Semester (R16) Data structure through C++ Lab (2017-18)
20
6. Implementation of Doubly linked list?
#include<iostream.h>
class Node
{
Public:
Node *prev;
int data;
Node *next;
};
class list:public Node
{
Node *start;
Node *last;
public:
List()
{
Start=NULL;
Last=NULL;
}
void create()
{
int num;
Node *temp=new Node;
if(start==NULL)
{
cout<<"\n Enter an element:";
cin>>num;
temp-->prev=NULL;
temp-->data-num;
temp-->next=NULL;
start=temp;
last=temp
}
else
{
cout<<"Double linked list has an element, cannot
create new list"
}
}
void insert()
{
int ch,num;
Node *temp=new Node;
cout<<"\n Enter an element";
cin>>num;
temp-->prev=NULL;
temp-->data-num;
temp-->next=NULL;
cout<<"\n INSERT AS \n 1:Start Node \n 2:" Last Node";
cout<<"\n Enter your choice:";
II-I Semester (R16) Data structure through C++ Lab (2017-18)
21
cin>>ch;
switch(ch)
{
case 1:
temp-->next=start;
start-->prev=temp;
start=temp;
break;
case 2:
last-->next=temp;
temp-->prev=last;
last=temp;
}
}
void del()
{
Node *p;
int ch;
cout<<"\n Delete \n 1:Start Node \n 2:Last Node";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
if(start!=NULL)
{
cout<<"\n deleted element is:"<<start--
>data;
start=start-->next;
start-->prev=NULL;
}
else
{
cout<<"\n not able to delete";
break;
}
case 2:
cout<<"\n deleted element is:"<<last-->data;
last=last-->prev;
last-->next=NULL;
break;
}
}
void display()
{
Node *p;
int ch;
cout<<"\n Display\n 1:last to right \n 2:right to left";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
II-I Semester (R16) Data structure through C++ Lab (2017-18)
22
{
case 1:
p=start;
if(p==NULL)
{
cout<<"\n list is empty";
return;
}
cout<<"\n elements from left to right are
\n";
while(p!=NULL)
{
cout<<p-->data;
cout<<"-->";
p=p-->next;
}
break;
case 2:
p=last;
if(p==NULL)
{
cout<<"\n list is empty";
return;
}
cout<<"\n elements from right to left are
\n";
while(p!=NULL)
{
cout<<p-->data;
cout<<"-->";
p=p-->prev;
}
break;
}
}
};
main()
{
list d;
int ch;
do
{
cout<<"\n ***DOUBLE LINKED LIST MENU*** \n 1:create first
node \n 2:Insert \n 3:Delete \n 4: Display \n 5:Exit \n";
cout<<"\n Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
d.create();
break;
case 2:
d.insert();
break;
II-I Semester (R16) Data structure through C++ Lab (2017-18)
23
case 3:
d.del();
break;
case 4:
d.display();
break;
case 5:
break;
}
}while(ch<=4);
}
Output:
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:1
Enter an element 10
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:1
Double Linked List has an element, cannot create new list
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:2
Enter an element 20
Insert as
1: Start Node
2: Last Node
Enter your choice:1
II-I Semester (R16) Data structure through C++ Lab (2017-18)
24
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:2
Enter an element 30
Insert as
1: Start Node
2: Last Node
Enter your choice:2
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:4
Display
1:left to right
2:right to left
Enter your choice:1
Elements from left to right are
20�10�30�
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:3
Delete
1:start node
2:last node
Enter your choice:2
Deleted element is 30
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
II-I Semester (R16) Data structure through C++ Lab (2017-18)
25
5:Exit
Enter your choice:4
Display
1:left to right
2:right to left
Enter your choice:2
Elements from right to left are
10 20
***DOUBLE LINKED LIST MENU***
1:create first node
2:Insert
3:Delete
4: Display
5:Exit
Enter your choice:5
7. Implementation of Binary Search trees.
/* Binary Search Tree */
#include<iostream.h>
#include<conio.h>
class node
{ public:
int data;
node *left;
node *right;
};
class BST:public node
{ public:
node *root;
BST()
{
root=NULL;
}
void create()
{int num;
node *temp=new node;
if(root==NULL)
II-I Semester (R16) Data structure through C++ Lab (2017-18)
26
{
cout<<"\nEnter an Element:";
cin>>num;
temp->left=NULL;
temp->data=num;
temp->right=NULL;
root=temp;
}
else
cout<<"BST has already a Root node";
}
void insert(node *t, int x)
{
node *temp=new node;
if(t->data == x)
{cout<<"duplicate data";
return;
}
if( x < t->data )
{
if( t->left == NULL )
{
temp->left=NULL;
temp->data=x;
temp->right=NULL;
t->left=temp;
return;
}
else
insert(t->left, x);
}
if( x > t->data )
{
if( t->right == NULL )
{
temp->left=NULL;
temp->data=x;
temp->right=NULL;
t->right=temp;
return;
}
else
insert(t->right, x);
}
}
void search(node *t,int x)
{
if(t==NULL)
II-I Semester (R16) Data structure through C++ Lab (2017-18)
27
{cout<<"element not found";
return;
}
if(t->data==x)
{
cout<<"Element found in the BST";
return;
}
if(x < t->data)
search(t->left,x);
if(x > t->data)
search(t->right,x);
}
void inorder(node *t)
{
if(t==NULL)
return;
if(t !=NULL)
{
inorder(t->left);
cout<<t->data<<" ";
inorder(t->right);
}
}
void preorder(node *t)
{
if(t !=NULL)
{
cout<<t->data<<" ";
preorder(t->left);
preorder(t->right);
}
}
void postorder(node *t)
{
if(t !=NULL)
{
postorder(t->left);
postorder(t->right);
cout<<t->data <<" ";
}
}
};
void main()
{
int ch,x;
BST b;
do
{
II-I Semester (R16) Data structure through C++ Lab (2017-18)
28
cout<<"\n**** BST MENU ****";
cout<<"\n1:CREATE ROOT NODE \n2:INSERT\n3:SEARCH\n4:INORDER
\n5:PREORDER\n6:POSTORDER\n7:EXIT\n";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
b.create();
break;
case 2:
cout<<"Enter a Number: ";
cin>>x;
b.insert(b.root,x);
break;
case 3:
cout<<"Enter a Number to search: ";
cin>>x;
b.search(b.root,x);
break;
case 4:
b.inorder(b.root);
break;
case 5:
b.preorder(b.root);
break;
case 6:
b.postorder(b.root);
break;
case 7:
break;
}
}while(ch<=6);
getch();
}
Output:
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:1
Enter an element 50
**** BST MENU ****
II-I Semester (R16) Data structure through C++ Lab (2017-18)
29
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 30
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 40
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 20
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 80
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
II-I Semester (R16) Data structure through C++ Lab (2017-18)
30
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 90
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:2
Enter an element 70
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:4
20 30 40 50 70 80 90
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:5
50 30 20 40 80 70 90
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
II-I Semester (R16) Data structure through C++ Lab (2017-18)
31
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:6
20 40 30 70 90 80 50
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:3
Enter an element 80
Element found in the BST
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:1
BST has already root node
**** BST MENU ****
1:CREATE ROOT NODE
2:INSERT
3:SEARCH
4:INORDER
5:PREORDER
6:POSTORDER
7:EXIT
Enter Your Choice:7
II-I Semester (R16) Data structure through C++ Lab (2017-18)
32
8. Implementation of Hash table.
#include <iostream.h>
#include <conio.h>
#define SIZE 7
class node
{public:
int data;
node *next;
} ;
class hashtable : public node
{
public:
node *htable[SIZE];
hashtable()
{
int i ;
node *t;
for(i = 0; i < SIZE; i++)
{ t=new node;
t->data = NULL;
t->next = NULL;
htable[i]=t;
}
}
void insert(int n)
{
int index ;
index=hash(n);
if( htable[index]->data != NULL )
{
resolve(htable[index],index, n);
}
else
{
htable[index]->data=n;
}
}
int hash(int n)
{
return(n%SIZE);
}
void resolve(node *t, int loc, int n)
{
node *tmp,*p;
II-I Semester (R16) Data structure through C++ Lab (2017-18)
33
p=t;
while(p->next != NULL)
p = p->next;
tmp = new node;
tmp->data = n;
tmp->next = NULL;
p->next=tmp;
}
void display()
{
int i = 0;
node *target;
for(i = 0; i < SIZE; i++)
{
if(htable[i]->data != NULL)
{
target = htable[i];
while(target)
{
cout<<"\nlocation: "<< i <<" data:" << target->data;
target = target->next;
}
}
}
}
};
int main(void)
{
int n,i;
hashtable h;
clrscr();
cout<<"\nEnter any 5 integer numbers\n";
for(i=0;i<=4;i++)
{ cin>>n;
h.insert(n);
}
h.display();
getch();
}
Output:
Enter any 5 integer numbers
10
II-I Semester (R16) Data structure through C++ Lab (2017-18)
34
20
21
36
49
Location:0 data :21
Location:0 data :49
Location :1 data :36
Location :3 data :10
Location :6 data :20
9. Implementation of Depth First Search Techniques.
#include <iostream.h>
#include <conio.h>
int stack[10],top=-1;
void push(int item)
{
top=top+1;
stack[top]=item;
}
int pop()
{
int item;
if (top != -1)
{ item=stack[top];
top=top-1;
return(item);
}
}
void DFT(int graph[][10],int n,int start)
{ int v[10]={0,0,0,0,0,0,0,0,0,0};
int i,j,x;
push(start);
v[start]=1;
cout<<"\n";
while(1)
{
start=pop();
cout<<start+1 <<" ";
x=start;
for(i=0;i<n;i++)
{
if(graph[x][i]==1 && v[i]!=1)
II-I Semester (R16) Data structure through C++ Lab (2017-18)
35
{
push(i);
v[i]=1;
}
}
if(top==-1)
break;
}
}
void main()
{
int graph[10][10];
int i,j,n,v1,v2;
char ch,type;
clrscr();
cout<<"Enter number of Verteces:";
cin>>n;
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
graph[i][j]=0 ;
}
do
{
cout<<"Enter the Edge of graph";
cin>>v1>>v2;
graph[v1-1][v2-1]=1;
graph[v2-1][v1-1]=1;
cout<<"Another edge y/n?";
cin>>ch;
}
while(ch=='y');
cout<<"The Depth First Traversal of graph is\n";
DFT(graph,n,0);
getch();
}
Output:
Enter numbers of vertices 5
Enter the edge of graph 0 1
Another edge y/n? y
Enter the edge of the graph 02
Another edge y/n? y
Enter the edge of graph 03
II-I Semester (R16) Data structure through C++ Lab (2017-18)
36
Another edge y/n?y
Enter the edge of the graph 14
Another edge y/n?y
Enter the edge of the graph 23
Another edge y/n?y
Enter the edge of the graph 24
Another edge y/n?n
Enter the edge of the graph 34
Another edge y/n?n
The depth first traversal of graph is 0 3 4 2 1
10. Implementation of Breadth First Search Techniques.
#include <iostream.h>
#include <conio.h>
int queue[10],front=-1,rear=-1;
void insert(int n)
{
if(rear<=5)
{
rear++;
queue[rear]=n;
}
}
int isempty()
{
if (front==rear )
return 1;
else
return 0;
}
int del()
{ int n;
if(!isempty())
{
front++;
n=queue[front];
return n;
}
}
void BFT(int graph[][10],int n,int start)
{ int v[6]={0,0,0,0,0,0};
int i,j;
insert(start);
II-I Semester (R16) Data structure through C++ Lab (2017-18)
37
v[start]=1;
while(!isempty())
{ start=del();
cout<<start+1<<" ";
for(i=0;i<n;i++)
{
if(graph[start][i]==1 && v[i]==0)
{
insert(i);
v[i]=1;
}
}
}
}
void main()
{
int graph[10][10];
int i,j,n,v1,v2;
char ch,type;
clrscr();
cout<<"Enter number of Vertices:";
cin>>n;
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
graph[i][j]=0 ;
}
do
{
cout<<"Enter the Edge of graph";
cin>>v1>>v2;
graph[v1-1][v2-1]=1;
graph[v2-1][v1-1]=1;
cout<<"Another edge y/n?";
cin>>ch;
} while(ch=='y');
cout<<"Breadth First Search of Graph is \n ";
BFT(graph,n,0);
getch();
}
Output:
Enter numbers of vertices 5
Enter the edge of graph 0 1
II-I Semester (R16) Data structure through C++ Lab (2017-18)
38
Another edge y/n? y
Enter the edge of the graph 02
Another edge y/n? y
Enter the edge of graph 03
Another edge y/n?y
Enter the edge of the graph 14
Another edge y/n?y
Enter the edge of the graph 23
Another edge y/n?y
Enter the edge of the graph 24
Another edge y/n?n
Enter the edge of the graph 34
Another edge y/n?n
Breadth First Search of Graph is 0 1 2 3 4
11. Write a program to Implement Prim’s Algorithm?
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,u,v,n,i,j,nedge=1;
int visited[10]={0},min,mincost=0,cost[10][10];
clrscr();
cout<<"\n Enter the number of Verteces:";
cin>>n;
cout<<"\n Enter the adjacency matrix:\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
cout<<"\n";
while(nedge<n)
{
min=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
II-I Semester (R16) Data structure through C++ Lab (2017-18)
39
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
cout<<"\n Edge "<<nedge << a << "-->" << b <<" cost:"<<min;
nedge++;
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
cout<<"\n Minimun cost="<<mincost;
getch();
}
Output:
Enter the no of vertices: 4
Enter the adjacency matrix :
0 6 4 3
6 0 2 5
4 2 0 7
3 5 7 0
Enter 11 � 4 cost:3
Enter 21 � 3 cost:4
Enter 33 � 2 cost:2
Minimum � cost =9
12. Write a program to implement Kruskal’s Algorithm?
#include<iostream.h>
#include<conio.h>
int i,j,k,a,b,u,v,n,nedge=1;
int min,mincost=0,cost[9][9];
void main()
{
clrscr();
cout<<"\nEnter the no. of vertices\n";
cin>>n;
cout<<"\nEnter the cost adjacency matrix\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
if(cost[i][j]==0)
cost[i][j]=999;
II-I Semester (R16) Data structure through C++ Lab (2017-18)
40
}
}
cout<<"\nThe edges of Minimum Cost Spanning Tree are\n\n";
while(nedge<n)
{ min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
cout<<"("<<a<<"-->"<<b<< ") ="<<min<<"\n";
mincost +=min;
nedge++;
cost[a][b]=cost[b][a]=999;
}
cout<<"\n\tMinimum cost = "<<mincost;
getch();
}
Output:
Enter the no of vertices: 4
Enter the cost adjacency matrix :
0 6 4 3
6 0 2 5
4 2 0 7
3 5 7 0
The edges of minimum cost spanning tree are
(2�3)=2
(1�4)=3
(1�3)=4
Minimum cost=9
II-I Semester (R16) Data structure through C++ Lab (2017-18)
41
13. Write a program to implement Dijkstra’s Algorithm?
#include<iostream.h>
#include<conio.h>
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,j,u,count,flag[10],min;
for(i=1;i<=n;i++)
{flag[i]=0;
dist[i]=cost[v][i];
}
count=2;
flag[1]=1;
while(count<n)
{
min=99;
for(j=1;j<=n;j++)
if(dist[j]<min && !flag[j])
{min=dist[j];
u=j;
}
flag[u]=1;
for(j=1;j<=n;j++)
if((dist[u]+cost[u][j]<dist[j]) && !flag[j])
dist[j]=dist[u]+cost[u][j];
count++;
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
cout<<"\n Enter the number of nodes:";
cin>>n;
cout<<"\n Enter the cost matrix:\n";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cin>>cost[i][j];
}
cout<<"\n Enter the source :";
cin>>v;
dij(n,v,cost,dist);
cout<<"\n Shortest path:\n";
for(i=1;i<=n;i++)
if(i!=v)
cout<<v <<"-> "<<i << "cost=" << dist[i] <<"\n";
getch();
}
II-I Semester (R16) Data structure through C++ Lab (2017-18)
42
Output:
Enter the no of vertices: 4
Enter the cost matrix:
0 6 4 3
6 0 2 5
4 2 0 7
3 5 7 0
Enter the source: 2
Shortest path:
2�1 cost =6
2�3 cost=2
2�4 cost=5
14. Write a program to implement Merge Sort?
#include<iostream.h>
#include<conio.h>
void merge(int * , int , int , int );
void mergesort(int *array, int left, int right)
{
int mid = (left+right)/2;
if(left<right)
{
mergesort(array,left,mid);
mergesort(array,mid+1,right);
merge(array,left,mid,right);
}
}
void merge(int *array, int left, int mid, int right)
{
int tempArray[50];
int n,i;
int pos=0, lpos = left, rpos = mid + 1;
n=right-left+1;
while(lpos <= mid && rpos <= right)
{
if(array[lpos] < array[rpos])
tempArray[pos++] = array[lpos++];
else
tempArray[pos++] = array[rpos++];
II-I Semester (R16) Data structure through C++ Lab (2017-18)
43
}
while(lpos <= mid) tempArray[pos++] = array[lpos++];
while(rpos <= right)tempArray[pos++] = array[rpos++];
/* Copy back the sorted array to the original array */
for(i = 0;i < pos; i++)
array[i+left] = tempArray[i];
return;
}
void main()
{
int n;
int array[20];
int i;
cout<<"Enter No of Elements";
cin>>n;
cout<<"Enter Numbers " ;
for(i = 0;i < n;i++)
cin>>array[i];
/* Calling this functions sorts the array */
mergesort(array,0,n-1);
cout<<"\n Sorted Array is \n";
for(i = 0;i < n;i++)
cout<<array[i]<<" ";
getch();
}
Output:
Enter no of elements 5
Enter numbers 5 3
25
18
4
45
Sorted array is
4 18 25 45 53
15. Write a program to implement Quick Sort?
#include<iostream.h>
#include<conio.h>
II-I Semester (R16) Data structure through C++ Lab (2017-18)
44
void quick(int a[],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i] <= pivot && i<j ) i++;
while(a[j]>pivot && i<=j ) j--;
if(i<=j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[first];
a[first]=temp;
quick(a,first,j-1);
quick(a,j+1,last); }
}
void main()
{
int a[50];
int n,i;
clrscr();
cout<<"\n enter n value";
cin>>n;
cout<<"\n Enter numbers ";
for(i=0;i<n;i++)
cin>>a[i];
quick(a,0,n-1);
cout<<"the sorted numbers are \n";
for(i=0;i<n;i++)
cout<<a[i] << " ";
getch();
}
Output:
Enter n value 5
Enter number 4 53 45 25 18
The sorted numbers are
4 18 25 45 53
Powered by TCPDF (www.tcpdf.org)