EX.
NO: 1
DATE: 15-06-24 Array Implementation Using Stack
Aim:
To write C++ program for Array implementation using
Stack
Algorithm:
Step 1: Start.
Step 2: Declare variables.
Step 3: Define push() to add element into stack.
Step 4: Define pop() to delete element from stack.
Step 5: Define display() to list out elements.
Step 6:Stop
Coding:
#include <iostream.h>
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
cout<<"Elements Added Successfully"<<endl;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top]
<<endl; top--;
cout<<"Elements deleted successfully"<<endl;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"ARRAY IMPLEMENTATION OF STACK"<<endl;
do {
cout<<"1) PUSH"<<endl;
cout<<"2) POP"<<endl;
cout<<"3) DISPLAY"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter Your choice: "<<endl;
cin>>ch;
switch(ch)
{ case 1: {
cout<<"Enter Value to be added:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
Output:
ARRAY IMPLEMENTATION OF STACK
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your choice:
1
Enter Value to be added:
10
Elements Added Successfully
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your choice:
1
Enter Value to be added:
20
Elements Added Successfully
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your choice:
1
Enter Value to be added:
30
Elements Added Successfully
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your
choice: 3
Stack elements are:30 20 10
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your choice:
2
The popped element is 30
Elements deleted successfully
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your
choice: 3
Stack elements are:20 10
1) PUSH
2) POP
3) DISPLAY
4) Exit
Enter Your
choice: Exit
Result:
Thus the program has been executed successfully.
EX.NO: 2
DATE: 24-06-24 Array implementation using Queue
Aim:
To write C++ program for Array implementation using
Queue.
Algorithm:
Step 1: Start.
Step 2: Declare variables.
Step 3: Define enqueue() to add element into queue.
Step 4: Define dequeue() to delete element from queue.
Step 5: Define display() to list out elements.
Step 6: Stop.
Coding:
#include <iostream.h>
#include<conio.h>
int queue[100], n = 100, front = - 1, rear = - 1;
void enqueue() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void dequeue() {
if (front == - 1 || front > rear)
{ cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl;
front++;;
}
}
void display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
do {
cout<<"1) Enqueue"<<endl;
cout<<"2) Dequeue"<<endl;
cout<<"3) Display"<<endl;
cout<<"4) Exit"<<endl;
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
Output:
1) Enqueue
2) Dequeue
3) Display
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit
Result:
Thus the program has been executed successfully.
EX.NO: 3
DATE: 01-07-24 Linked List Implementation of Stack
Aim:-
To write C++ program for linked list implementation of
Stack
Algorithm:-
Step 1: Start.
Step 2: Declare variables.
Step 3: Define push() to add element into stack.
Step 4: Define pop() to delete element from stack.
Step 5: Define display() to list out elements.
Step 6: Stop.
Coding:
#include <iostream.h>
#include<conio.h>
class Node {
public:
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() {
top = NULL;
}
void push( ) {
Node* newNode = new
Node(); Cout<<“Enter
element:” cin>>x;
newNode->data = x;
newNode->next = top;
top = newNode;
}
void pop() {
if (top == NULL) {
cout << "Stack is empty!" << endl;
return;
}
Node* temp = top;
top = top->next;
delete temp;
}
void display() {
if (top == NULL) {
cout << "Stack is empty" << endl;
return;
}
Node* temp = top;
cout << "Stack elements are: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
Void main()
{ Stack stk;
cout << "Linked List Implementation";
do
{
Cout<<“1.PUSH”<<endl;
Cout<<“2.POP”<<endl;
Cout<<“3.DISPLAY”<<endl;
Cout<<“Enter your Choice:”;
Cin>>ch;
Switch(ch)
{
Case 1:
stk.push();
break;
Case 2:
stk.pop();
break;
Case 3:
stk.display();
break;
case
4:
exit(0);
default:
Cout<<“Invalid choice”;
}
}
While(ch!=4);
getch();
}
Output:
Linked List Implementation
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your Choice: 1
Enter element: 10
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your Choice: 1
Enter element: 20
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter
your
Choice: 3
Stack elements are: 20 10
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your Choice: 2
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your Choice: 3
Stack elements are: 10
1. PUSH
2. POP
3. DISPLAY
4. EXIT
Enter your Choice: 4
EX.NO: 5
Convert Infix to Postfix Expression
DATE:
Aim:
To implement infix to postfix conversion of an expression
using stack.
Algorithm:
Step 1: Start.
Step 2: Push the elements to the end.
Step 3: Scan from left to right and repeat step 4 to 7 for each
element until the stack is empty .
Step 4: If an operand is encountered add it.
Step 5: If right parenthesis is encountered then
1) Repeatedly pop from the stack and add to r each
operator on the top of stack until the left parenthesis is
encountered.
2) Remove the left parenthesis .
Step 6: If an operator is encountered then pop from stack and
add to each operator on the top of the stack which has the
same precedence as higher precedence then operations.
Step 7: Stop.
Coding:
#include<iostream>
#include<stack>
using namespace std;
int precedence(char op)
{ if(op == '^') return 3;
else if(op == '*' || op == '/') return 2;
else if(op == '+' || op == '-') return 1;
return -1;
}
string infixToPostfix(string infix)
{ stack<char> s;
string postfix = "";
for(int i = 0; i < infix.length(); i++) {
char c = infix[i];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
postfix += c;
} else if(c == '(') {
s.push(c);
} else if(c == ')') {
while(!s.empty() && s.top() != '(') {
postfix += s.top();
s.pop();
}
if(!s.empty()) s.pop();
} else {
while(!s.empty() && precedence(s.top()) >= precedence(c)) {
postfix += s.top();
s.pop();
}
s.push(c);
}
}
while(!s.empty()) {
postfix += s.top();
s.pop();
}
return postfix;
}
int main() {
string infix;
cout << "Enter infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
Output:
Enter infix expression: (A+B)*C
Postfix expression: AB+C*
Result:
Thus the program has been executed successfully.
EX.NO: 4
DATE: 09-07-24 Linked list Implementation
of Queue
Aim:-
To write C++ program for linked list implementation using
Queue
Algorithm:-
Step 1: Start.
Step 2: Declare variables.
Step 3: Define enqueue() to add element into queue.
Step 4: Define dequeue() to delete element from queue.
Step 5: Define display() to list out elements.
Step 6: Stop.
Coding:
#include<iostream>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int data;
Node *next ;
};
class Queue
{
Node *front, *rear;
public:
Queue()
{
front = rear = NULL;
}
void enqueue()
{
int val;
Node *pnew;
pnew = new
Node;
Cout<<“Enter an element:”
Cin>>val;
pnew->data = val;
pnew->next = NULL;
if(front == NULL)
front = rear = pnew;
else
{
rear->next = pnew;
rear = pnew;
}
}
void dequeue()
{
Node *temp;
if(front == NULL)
cout<<"Queue is Empty";
else
{
temp= front;
front = front->next;
delete temp;
cout<<“Deleted Successfully “;
}
}
void display()
{
Node *temp;
temp= front;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp = temp->next;
}
cout<<endl;
}
};
void main()
{
clrscr();
Queue
q; Int ch;
do
{
Cout<<“Linked List Implementation of Queue”;
cout<<“1.Enqueue”;
cout<<“2.Dequeue”;
cout<<“3.Display”;
cout<<“4.Exit”;
cout<<“Enter your Choice”;
cin>>ch;
Switch(ch)
{
case 1:
q.enqueue();
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
Output:
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 1
Enter an element: 10
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 1
Enter an element: 20
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 3
10 20
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 2
Deleted Successfully
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 3
20
Linked List Implementation of Queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your Choice: 4
Result:
Thus the program has been executed successfully.
EX.NO:7A
DATE:
Linear Search
Aim:-
To write c++ program to implement linear search
Algorithm:-
Step 1: Start
Step 2: Declare necessary variables.
Step 3: Get size of an array from user.
Step 4: Get array elements from user.
Step 5: Get key element to be searched from user
Step 6: Compare each element with key element.
Step 7: If found, display the index position of key.
Step 8: If not found, display element not found.
Step 9: Stop
Coding:
#include<iostream>
using namespace std ;
int search ( int A [ ] , int n , int key )
{
int i ;
for ( i = 0 ; i < n ; i++ )
{
if ( key == A [ i ] )
{
return i ;
}
}
return - 1 ;
}
int main()
{
int A [ 30 ] ;
int key , n , i , x ;
cout << " Enter size of array : " ;
cin >> n ;
cout << "Enter Array " << endl ;
for ( i = 0 ; i < n ; i++ )
{
cin >> A [ i ] ;
}
cout << " Enter an element to search : " ;
cin >> key ;
x = search( A , n , key ) ;
if ( x != -1 )
{
cout << " Element is at index : " << x << endl;
}
else
{
cout << " Element not found ! " << endl ;
}
return 0 ;
}
Output:
Enter size Of array: 5
Enter Array:
10
20
30
40
50
Enter an element to search: 30
Result:
Thus the program has been executed successfully.
EX.NO:7B
DATE:
Binary Search
Aim:-
To write c++ program to implement Binary search
Algorithm:-
Step 1: Start
Step 2: Declare necessary variables.
Step 3: Get size of an array from user.
Step 4: Get sorted array elements from user.
Step 5: Get key element to be searched from user.
Step 6: Calculate middle element of an array.
Step 7: Compare key element with middle element.
Step 8:If key element is equal to middle element, display “
Element is found” with its index position.
Step 9: If key element is less than middle element, discard
searching left hand side elements.
Step 10: If key element is greater then middle element,
discard searching right hand elements.
Step 11: If not found, display element not found.
Step 12: Stop
Coding:
#include <iostream.h>
#include <conio.h>
void main (){
int arr[100], st, mid, end, i, num, tgt;
cout << " Enter the size of the array: " << endl;
cin >> num;
cout << " Enter the values in sorted array: " << endl;
for (i = 0; i < num; i++){
cout << " arr [" << i << "] = ";
cin >> arr[i];
}
st = 0;
end = num - 1;
cout << "Enter an element to be searched: " << endl;
cin >> tgt;
while ( st <= end)
{
mid = ( st + end ) / 2;
if (arr[mid] == tgt){
cout << " Element is found at index " << (mid );
exit (0);
}
else if ( tgt > arr[mid])
{ st = mid+1;
}
else if ( tgt < arr[mid])
{
end = mid - 1;
}
}
cout << " Element is not found. " << endl;
getch();
}
Output:
Enter the size of the array:
10
Enter the values in sorted array:
Arr[0]= 12
Arr [1] = 24
Arr [2] = 36
Arr [3] = 48
Arr [4] = 50
Arr [5] = 54
Arr [6] = 58
Arr [7] = 60
Arr [8] = 72
Arr [9] = 84
Enter an element to be searched:
50
Element is found at index 5
Result:
Thus the program has been executed
EX.NO:8A
DATE: Breadth First Search of Graph
Aim:-
To write a C++ program for implementation for Breadth first search
of Graph
Algorithm:-
Step1:Start
Step 2: Get the number of vertices (n) and edges (m) of a
graph from user.
Step 3: Get the edges and store them in the adjacency matrix.
Step 4: Choose an initial vertex for traversal.
Step 5: Set the initial vertex as visited.
Step 6:Find all adjacent vertices of the current vertex that
have not been visited
Step 7: Display the visited vertices.
Step 8: Stop.
Coding:
#include<iostream.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
Void main()
{
int m;
cout<<"Enter no of vertices:";
cin>> n;
cout<<"Enter no of edges:";
cin>> m;
cout<<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin>>i>>j;
cost[i][j]=1;
}
cout<<"Enter initial vertex to traverse from:";
cin>>v;
cout<<"Visitied vertices:";
cout<<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1; j<=n; j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v <<" ";
k++;
visit[v]=0;
visited[v]=1;
}
Getch();
}
Output:
Enter no of vertices:4
Enter no of edges:4
EDGES
12
13
24
34
Enter initial vertex to traverse from:1
Visited vertices:1 2 3 4
12
Result:
Thus the program has been executed successfully.
EX.NO:8B
DATE:
Depth First Search of Graph
Aim:-
To write a C++ program for implementation for Depth first
search of Graph
Algorithm:-
Step1:Start
Step 2: Get the number of vertices (n) and edges (m) of a
graph from user.
Step 3: Get the edges and store them in the adjacency matrix.
Step 4:Choose an initial vertex for traversal.
Step 5: Set the initial vertex as visited and push it onto stack.
Step 6:Pop a vertex and set it as visited.
Step 7: Display the visited vertices.
Step 8: Stop.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
Void main()
{
int m;
cout <<"Enter no of vertices:";
cin >> n;
cout <<"Enter no of edges:";
cin >> m;
cout <<"\nEDGES \n";
for(k=1; k<=m; k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"Enter initial vertex to traverse from:";
cin >>v;
cout <<"DFS ORDER OF VISITED VERTICES:";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n; j>=1; j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}
Output:
Enter no of vertices:4
Enter no of edges:4
EDGES
12
13
24
34
Enter initial vertex to traverse from:1
DFS ORDER OF VISITED VERTICES:1 2 4
3
Result:
Thus the program has been executed successfully.
EX.NO:9
DATE: Single source shortest path of a Graph
Aim:
To implement the graph of shortest path.
Algorithm:
Step 1: Start the execution.
Step 2: Find the shortest path of negative nodes.
Step 3: Find the graph maximum path as nodes, vertex
and find the length.
Step 4: Check the maximum value of infinity path.
Step 5: Calculate the mid distance and next node.
Step 6: Visit the distance and each of nodes.
Step 7: Stop the execution
Coding:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
{
int cost[max][max],distance[max],pred[max];
#define max 5
void dijkstra(int G[max][max],int n,int startnode);
int main()
{
int G[max][max]={{0,1,0,3,10},{1,0,5,0,0},{0,5,0,2,1},{3,0,2,0,6},
{10,0,1,6, 0}};
int n=5;
int u=0;
clrscr();
dijkstra(G,n,u);
return 0;
}
void dijkstra(int G[max][max],int n,int startnode)
int visited[max],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0) cost[i]
[j]=INFINITY;
else cost[i][j]=G[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode; visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY; for(i=0;i<n;i+
+) if(distance[i]<mindistance&&!
visited[i])
{
mindistance=distance[i];nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++) if(!
visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++) if(i!
=startnode)
{
cout<<"\nDistance of node"<<i<<"="<<distance[i]; cout<<"\
nPath="<<i;
j=i;
do
{
j=pred[j];
cout<<"<-"<<j;
}
while(j!=startnode);
}
getch();
}
Output:
Distance of node 1 = 1
Path = 1 <- 0
Distance of node 2 = 5
Path = 2 <-3 <- 0
Distance of node 3 = 3
Path = 3 <-0
Distance of node 4 = 6
Path = 4 <-2 <- 3 <- 0
Result:
Thus the program has been executed successfully.