Datastructure Lab
Datastructure Lab
ADT.
a)Stack ADT
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}};
main(){
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}return (0);}
OUTPUT:
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
b) Queue ADT.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}};
main(){
int ch;
queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}return (0);}
OUTPUT
2. Write a program to convert the given infix expression to postfix expression using
stack.
#include<iostream.h>
#include<string.h>
#define size 50
char stack[size];
int tos=0,ele;
void push(int);
char pop();
char infix[30],output[30];
int prec(char);
int main()
{
int i=0,j=0,length;
char temp;
cout<<"\nEnter an infix expression: ";
cin>>infix;
length=strlen(infix);
for(i=0;i<length;i++)
{
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' &&
infix[i]!='(' )
{
output[j++]=infix[i];
}
else
{
if(tos==0)
{
push(infix[i]);
}
else
{
if(infix[i]!=')' && infix[i]!='(')
{
if( prec(infix[i]) <= prec(stack[tos-1]))
{
temp=pop();
output[j++]=temp;
push(infix[i]);
}
else
{
push(infix[i]);
}
}
else
{
if(infix[i]=='(')
{
push(infix[i]);
}
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{output[j++]=temp;
temp=pop();}
}
}
}
}
}
while(tos!=0)
{
output[j++]=pop();
}
cout<<"The Postfix expression is: "<<output;
}
void push(int ele)
{
stack[tos]=ele;
tos++;
}
char pop()
{
tos--;
return(stack[tos]);
}
int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}
OUTPUT : :
Exit code:
3.Write a program to find following using Recursion a) Factorial of +ve Integer b) nth
term of the Fibonacci Sequence c) GCD of two +ve integers
a) Factorial of +ve Integer
#include<iostream>
using namespace std;
int factorial(int n);
int main(){
int n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;}
int factorial(int n){
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Output
#include <iostream>
using namespace std;
/*
* Funtion to calculate Nth Fibonacci number
* fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
*/
int fibonacci(int term){
if(term < 2)
return term;
return fibonacci(term -1) + fibonacci(term - 2);
}
int main(){
int N, i;
cout << "Enter number of terms in Fibonacci series\n";
cin >> N;
/*
* Nth term = (N-1)th therm + (N-2)th term;
*/
for(i = 0; i < N; i++){
cout << fibonacci(i) << " ";
}
return 0;
}
Output
#include <iostream>
using namespace std;
int hcf(int n1, int n2);
int main(){
int n1, n2;
cout << "Enter two positive integers: ";
cin >> n1 >> n2;
cout << "H.C.F of " << n1 << " & " << n2 << " is: " << hcf(n1, n2);
return 0;}
int hcf(int n1, int n2){
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
Output:
a) A C++ program to implement the Stack ADT using singly linked list.
Program:
#include<iostream.h>
using namespace std;
template<class t>
class node
{
public:
t info;
node *link;
};
template <class t>
class StackADT
{
virtual void Push()=0;
virtual void Pop()=0;
virtual void Top()=0;
virtual void Display()=0;
};
template<class t>
class StackLinkImp:public StackADT<t>
{
public:
t item;
node<t> *temp, *top;
StackLinkImp()
{
top=NULL;
}
void Push()
{
temp=new node<t>;
cout<<"Enter the itemto be insrted on to the stack:";
cin>>item;
if(top==NULL)
temp->link=NULL;
else
temp->link=top;
temp->info=item;
top=temp;
cout<<"Insertion Completed Successfully";
}
void Pop()
{
if(top==NULL)
cout<<"Stack is empty";
else
{
item=top->info;
top=top->link;
cout<<"The deleted element is:"<<item;
}
}
void Top()
{
if(top==NULL)
cout<<"Stack is empty";
else
cout<<"The top element is:"<<top->info;
}
void Display()
{
if(top==NULL)
cout<<"Stack is empty";
else
{
temp=top;
cout<<"The elements in the stack are:";
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->link;
}
}
}
};
int main()
{
int ch;
StackLinkImp<int> s1;
do
{
cout<<"\n****Menu****";
cout<<"\n1. Push\n2. Pop\n3. Top\n4. Display\n5. Exit";
cout<<"\nEnter ur choice:";
cin>>ch;
switch(ch)
{
case 1: s1.Push();
break;
case 2: s1.Pop();
break;
case 3: s1.Top();
break;
case 4: s1.Display();
break;
case 5: exit(1);
}
}while(1);
}
b) Queue ADT
Program:
#include<iostream>
using namespace std;
template<class t>
class node
{
public:
t info;
node *link;
};
template<class t>
class QueueADT
{
virtual void Insert()=0;
virtual void Delete()=0;
virtual void Display()=0;
};
template <class t>
class QueueLinkImp: public QueueADT<t>
{
public:
t item;
node<t> *temp,*front,*rear;
QueueLinkImp()
{
front=rear=NULL;
}
void Insert()
{
temp=new node<t>;
cout<<"Enter an element to be inserted in to queue:";
cin>>item;
temp->info=item;
temp->link=NULL;
if(front==NULL)
{
front=rear=temp;
cout<<item<<" inserted Successfully";
return;
}
rear->link=temp;
rear=rear->link;
cout<<item<<" inserted Successfully";
}
void Delete()
{
if(front==NULL)
cout<<"Queue is empty";
else
{
item=front->info;
front=front->link;
cout<<"Deleted Element is: "<<item;
}
}
void Display()
{
if(front==NULL)
cout<<"Queue is empty";
else
{
cout<<"The elements in the queue are:";
for(temp=front;temp!=NULL;temp=temp->link)
cout<<temp->info<<" ";
}
}
};
void main()
{
int ch;
QueueLinkImp<int> q1;
do
{
cout<<"\n****MENU****";
cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit";
cout<<"\nEnter ur choice:";
cin>>ch;
switch(ch)
{
case 1: q1.Insert();
break;
case 2: q1.Delete();
break;
case 3: q1.Display();
break;
case 4: exit(1);
}
}while(1);}
5.Write a program for sorting the given list numbers in ascending order using the
following technique: Bubble sort and Selection sort
*Bubble Sort*
#include<iostream>
int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";
for(i=0;i<n;++i)
cin>>a[i];
for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
Output
*Selection Sort*
#include<iostream>
#include<conio.h>
int main()
{
int a[100],i,n;
cout<<"Enter The number of Element:\n";
cin>>n;
cout<<"\nEnter Elements:\n";
for(i=0;i<n;i++)
{
cout<<"\nEnter:";
cin>>a[i];
}
s_sort(a,n);
cout<<"\nAfter Sorting\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
return 0;
}
OutPut:
6. Write a program for sorting the given list numbers in ascending order using the
following technique: Merge sort and Heap sort
*Merge Sort*
#include <iostream>
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
return 0;
}
Program Explanation
1. Take input of data.
2. Call MergeSort() function.
3. Recursively split the array into two equal parts.
4. Split them until we get at most one element in both half.
5. Combine the result by invoking Merge().
6. It combines the individually sorted data from low to mid and mid+1 to high.
7. Return to main and display the result.
8. Exit.
HEAP SORT:
void main()
{
int a[10], i, size;
cout << "Enter size of list"; // less than 10, because max size of array is 10
cin >> size;
cout << "Enter" << size << "elements";
for( i=0; i < size; i++)
{
cin >> a[i];
}
heapsort(a, size);
getch();
}
#include <iostream>
using namespace std;
// Node class
class Node {
int key;
Node* left;
Node* right;
public:
Node() { key=-1; left=NULL; right=NULL; };
void setKey(int aKey) { key = aKey; };
void setLeft(Node* aLeft) { left = aLeft; };
void setRight(Node* aRight) { right = aRight; };
int Key() { return key; };
Node* Left() { return left; };
Node* Right() { return right; };
};
// Tree class
class Tree {
Node* root;
public:
Tree();
~Tree();
Node* Root() { return root; };
void addNode(int key);
void inOrder(Node* n);
void preOrder(Node* n);
void postOrder(Node* n);
private:
void addNode(int key, Node* leaf);
void freeNode(Node* leaf);
};
// Constructor
Tree::Tree() {
root = NULL;
}
// Destructor
Tree::~Tree() {
freeNode(root);
}
// Add a node
void Tree::addNode(int key) {
// No elements. Add the root
if ( root == NULL ) {
cout << "add root node ... " << key << endl;
Node* n = new Node();
n->setKey(key);
root = n;
}
else {
cout << "add other node ... " << key << endl;
addNode(key, root);
}
}
delete tree;
return 0;
}
.
OUTPUT:-
add root node ... 30
add other node ... 10
add other node ... 20
add other node ... 40
add other node ... 50
In order traversal
10 20 30 40 50
Pre order traversal
30 10 20 40 50
Post order traversal
20 10 50 40 30
8. Write a program to the implementation graph traversals – BFS and DFS.
*BFS*
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
EDGES
12
23
15
14
47
78
89
26
57
enter initial vertex1
Visited vertices
12 4 5 3 6 7 8 9
*DFS*
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
EDGES
12
23
26
15
14
47
57
78
89
enter initial vertex1
ORDER OF VISITED VERTICES1 2 3 6 4 7 8 9 5
9. Write a program to find the minimum spanning tree for a weighted graph using
Prim’s Algorithm
/*
* C++ Program to find MST(Minimum Spanning Tree) using
* Prim's Algorithm
*/
#include <iostream>
#include <conio.h>
using namespace std;
struct node
{
int fr, to, cost;
}p[6];
int c = 0, temp1 = 0, temp = 0;
void prims(int *a, int b[][7], int i, int j)
{
a[i] = 1;
while (c < 6)
{
int min = 999;
for (int i = 0; i < 7; i++)
{
if (a[i] == 1)
{
for (int j = 0; j < 7; )
{
if (b[i][j] >= min || b[i][j] == 0)
{
j++;
}
else if (b[i][j] < min)
{
min = b[i][j];
temp = i;
temp1 = j;
}
}
}
}
a[temp1] = 1;
p[c].fr = temp;
p[c].to = temp1;
p[c].cost = min;
c++;
b[temp][temp1] = b[temp1][temp]=1000;
}
for (int k = 0; k < 6; k++)
{
cout<<"source node:"<<p[k].fr<<endl;
cout<<"destination node:"<<p[k].to<<endl;
cout<<"weight of node"<<p[k].cost<<endl;
}
}
int main()
{
int a[7];
for (int i = 0; i < 7; i++)
{
a[i] = 0;
}
int b[7][7];
for (int i = 0; i < 7; i++)
{
cout<<"enter values for "<<(i+1)<<" row"<<endl;
for (int j = 0; j < 7; j++)
{
cin>>b[i][j];
}
}
prims(a, b, 0, 0);
getch();
}
Output:
enter values of adjacency matrix for a 7 node graph:
source node:0
destination node:1
weight of node3
source node:1
destination node:2
weight of node2
source node:2
destination node:3
weight of node1
source node:2
destination node:5
weight of node2
source node:5
destination node:6
weight of node1
source node:6
destination node:4
weight of node1
10. Write a program for sorting the given list numbers in ascending order using the
following technique: Insertion sort and Quick sort
*insertion sort*
/* C++ Program - Insertion Sort */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
OUTPUT:
*quick sort*
#include<iostream.h>
#include<conio.h>
int a[10],l,u,i,j;
void quick(int *,int,int);
void main()
{
clrscr();
cout <<"enter 10 elements";
for(i=0;i<10;i++)
cin >> a[i];
l=0;
u=9;
quick(a,l,u);
cout <<"sorted elements";
for(i=0;i<10;i++)
cout << a[i] << " ";
getch();
}
OUTPUT
enter 10 elements5 2 3 16 25 1 20 7 8 61 14
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 25 16 20 7 8 61
1 2 3 5 8 16 20 7 25 61
1 2 3 5 7 8 20 16 25 61
1 2 3 5 7 8 16 20 25 61
1 2 3 5 7 8 16 20 25 61