Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Format of Program

Aim:
===== Aim Here ====
Algorithm (if any):
======================
===== Algorithm Here ====
======================
Code:
======================
===== Program Here =====
======================
Output:
===== Screenshot =====

/* C++ program to implement basic stack operations without switch case */


#include<iostream>
using namespace std;
#define MAX 5
class Stack
{
public:
int top;
int a[MAX]; // Maximum size of Stack

Stack()
{
top = -1;
}
int push(int x);
int pop();
int peek();
int isEmpty();
};

int Stack::push(int x)
{
if (top >= (MAX-1))
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
cout << x << "Pushed/Added into stack\n";
return true;
}
}

int Stack::pop()
{
if (top<0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--];
return x;
}
}

int Stack::peek()
{
if (top < 0)
{
cout << "Stack is Empty";
return 0;
}
else
{
int x = a[top];
return x;
}
}

int Stack::isEmpty()
{
return (top < 0);
}

// Driver program to test above functions


int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack \n";
cout<< "Top Item is "<<s.a[s.top];
cout<<"\n"<<s.peek();
return 0;
}
/* C++ program to implement basic stack operations */
#include<iostream>
using namespace std;
#define MAX 10

class Stack
{
public:
int top;
int a[MAX]; // Maximum size of Stack

Stack()
{
top = -1;
}
void push();
void pop();
void display();
int peek();
int isEmpty();
};

void Stack::push()
{
int pushed_item;
if(top == (MAX-1))
cout<<"Stack Overflow\n";
else
{
cout<<"Enter the item to be pushed in stack : ";
cin>>pushed_item;
top=top+1;
a[top] = pushed_item;
}
}

void Stack::pop()
{
if(top == -1)
cout<<"Stack Underflow\n";
else
{
cout<<"Popped element is : "<<a[top]<<"\n";
top=top-1;
}
}

void Stack::display()
{
int i;
if(top == -1)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements :\n";
for(i = top; i >=0; i--)
cout<<"\n",a[i];
}
}

int Stack::peek()
{
if (top < 0)
{
cout << "Stack is Empty";
return 0;
}
else
{
int x = a[top];
return x;
}
}

int Stack::isEmpty()
{
return (top < 0);
}

// Driver program to test above functions


int main()
{
class Stack s;
int choice;

do{
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
break;
default:
cout<<"Wrong choice\n";
}
}while(choice!=4);

return 0;
}

//C++ program to check balanced parenthesis using Stack Data Structure.....

#include<iostream>
#include<string.h>
using namespace std;
#define MAX 20

struct stack
{
char stk[MAX];
int top;
}s;
void push(char item)
{
if (s.top == (MAX - 1))
cout<<"Stack is Full\n";
else
{
s.top = s.top + 1; // Push the char and increment top
s.stk[s.top] = item;
}
}
void pop()
{
if (s.top == - 1)
{
cout<<"Stack is Empty\n";
}
else
{
s.top = s.top - 1; // Pop the char and decrement top
}
}
int main()
{
char exp[MAX];
int i = 0;
s.top = -1;
cout<<"\n INPUT THE EXPRESSION : ";
cin>>exp;
for(i=0; i<strlen(exp); i++)
{
if(exp[i]=='(' || exp[i]=='[' || exp[i]=='{')
{
push(exp[i]); // Push the open bracket
continue;
}
else if(exp[i]==')' || exp[i]==']' || exp[i]=='}') // If a closed bracket is
encountered
{
if(exp[i] == ')')
{
if(s.stk[s.top] == '(')
{
pop(); // Pop the stack until closed
bracket is found
}
else
{
cout<<"\n UNBALANCED EXPRESSION \n";
break;
}
}
if(exp[i] == ']')
{
if(s.stk[s.top] == '[')
{
pop(); // Pop the stack until closed bracket is found
}
else
{
cout<<"\n UNBALANCED EXPRESSION \n";
break;
}
}
if(exp[i] == '}')
{
if(s.stk[s.top] == '{')
{
pop(); // Pop the stack until closed bracket is
found
}
else
{
cout<<"\n UNBALANCED EXPRESSION \n";
break;
}
}
}

}
if(s.top == -1)
{
cout<<"\n BALANCED EXPRESSION \n"; // Finally if the stack is empty,
display that the expression is balanced
}

}
//C++ program to check Balanced Parenthesis Expression using Stack Data
Structure....
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 30

class BPExprStack
{
public:
int top;
int stack[MAX];
BPExprStack()
{
top=-1;
}
void push(char);
char pop();
int match(char a, char b);
int check(char []);
};

int BPExprStack::check(char exp[])


{
int i;
char temp;
for(i=0; i<strlen(exp); i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push(exp[i]);

if(exp[i]==')' || exp[i]=='}' || exp[i]==']')


if(top==-1)
{
cout<<"Right/closing Parenthesis are more then the
left/starting parenthesis\n";
return 0;
}
else
{
temp=pop();
if(!match(temp,exp[i]))
{
cout<<"Mismatched Parenthesis are:";
cout<<temp<<" and "<<exp[i]<<"\n";
return 0;
}
}
}
if(top==-1) //stack empty
{
cout<<"Balanced Parenthesis \n";
return 1;
}
else
{
cout<<"Left/starting Parenthesis are more then the Right/closing
parenthesis \n";
return 0;
}
}
int BPExprStack::match(char a, char b)
{
if (a=='[' && b==']')
return 1;
if (a=='{' && b=='}')
return 1;
if (a=='(' && b==')')
return 1;

return 0;

}
void BPExprStack::push(char item)
{
if (top == (MAX - 1))
cout<<"Stack is Overflow\n";
else
{
top = top + 1; // Push the char and increment top
stack[top] = item;
}
}
char BPExprStack::pop()
{
if (top == - 1)
{
cout<<"Stack Underflow\n";
return 0;
}
else
{
return (stack[top--]); // Pop the char and decrement top
}
}
int main()
{
BPExprStack obj;
char exp[MAX];
int valid;

cout<<"\n ENTER AN EXPRESSION : ";


cin.getline(exp, MAX);

valid=obj.check(exp);

if(valid==1)
cout<<"Valid Expression";
else
cout<<"Invalid Expression";
return 0;
}
//C program to check balanced parentheses using stack
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20

struct stack
{
char stk[MAX];
int top;
}s;
void push(char item)
{
if (s.top == (MAX - 1))
printf ("Stack is Full\n");
else
{
s.top = s.top + 1; // Push the char and increment top
s.stk[s.top] = item;
}
}
void pop()
{
if (s.top == - 1)
{
printf ("Stack is Empty\n");
}
else
{
s.top = s.top - 1; // Pop the char and decrement top
}
}
int main()
{
char exp[MAX];
int i = 0;
s.top = -1;
printf("\nINPUT THE EXPRESSION : ");
scanf("%s", exp);
for(i = 0;i < strlen(exp);i++)
{
if(exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
{
push(exp[i]); // Push the open bracket
continue;
}
else if(exp[i] == ')' || exp[i] == ']' || exp[i] == '}') // If a closed
bracket is encountered
{
if(exp[i] == ')')
{
if(s.stk[s.top] == '(')
{
pop(); // Pop the stack until closed bracket is
found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}
}
if(exp[i] == ']')
{
if(s.stk[s.top] == '[')
{
pop(); // Pop the stack until closed bracket is
found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}
}
if(exp[i] == '}')
{
if(s.stk[s.top] == '{')
{
pop(); // Pop the stack until closed bracket is
found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}
}
}
}
if(s.top == -1)
{
printf("\nBALANCED EXPRESSION\n"); // Finally if the stack is
empty, display that the expression is balanced
}
}
//Simple Queue
#include <iostream>
using namespace std;
int Q[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Q Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in Q : "<<endl;
cin>>val;
rear++;
Q[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Q Underflow ";
//return ;
} else {
cout<<"Element deleted from Q is : "<< Q[front] <<endl;
front++;
}
}
void Display() {
if (front == - 1)
cout<<"Q is empty"<<endl;
else {
cout<<"Q elements are : ";
for (int i = front; i <= rear; i++)
cout<<Q[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to Q"<<endl;
cout<<"2) Delete element from Q"<<endl;
cout<<"3) Display all the elements of Q"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
//Circular Queue
#include <iostream>
using namespace std;

int cqueue[5];
int front = -1, rear = -1, n=5;

void insertCQ(int val)


{
if ((front == 0 && rear == n-1) || (front == rear+1))
{
cout<<"Queue Overflow \n";
//return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == n - 1 && front!=0)
rear = 0;
else
rear = rear + 1;
}
cqueue[rear] = val ;
}
void deleteCQ()
{
if (front == -1)
{
cout<<"Queue Underflow\n";
//return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;

if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ()
{
int f = front, r = rear;
if (front == -1)
{
cout<<"Queue is empty"<<endl;
//return;
}
cout<<"Queue elements are :\n";
if (f <= r)
{
while (f <= r)
{
cout<<cqueue[f]<<" ";
f++;
}
}
else
{
while (f <= n - 1)
{
cout<<cqueue[f]<<" ";
f++;
}
f = 0;
while (f <= r)
{
cout<<cqueue[f]<<" ";
f++;
}
}
cout<<endl;
}
int main() {

int ch, val;


cout<<"1)Insert\n";
cout<<"2)Delete\n";
cout<<"3)Display\n";
cout<<"4)Exit\n";
do
{
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;

case 2:
deleteCQ();
break;

case 3:
displayCQ();
break;

case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch != 4);
return 0;
}
//DEQueue
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1)
{
cout<<"\n insertion is not possible, overflow!!!!";
}
else
{
if(f==-1)
{
f++;
r++;
}
else
{
r=r+1;
}
a[r]=i;
cout<<"\nInserted item is"<<a[r];
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i; // or r=0; a[r]=i;
cout<<"\n inserted element is:"<<i;
} else if(f!=0) {
a[--f]=i;
cout<<"\n inserted element is:"<<i;
} else {
cout<<"\n insertion is not possible, overflow!!!";
}
}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[f];
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"the deleted element is:"<<a[r];
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty";
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
}
}
int main() {
int c,i;
dequeue d;
do {//perform switch opeartion
cout<<"\n 1.insert at beginning";
cout<<"\n 2.insert at end";
cout<<"\n 3.show";
cout<<"\n 4.deletion from front";
cout<<"\n 5.deletion from rear";
cout<<"\n 6.exit";
cout<<"\n enter your choice:";
cin>>c;
switch(c) {
case 1:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_beg(i);
break;
case 2:
cout<<"enter the element to be inserted";
cin>>i;
d.insert_at_end(i);
break;
case 3:
d.show();
break;
case 4:
d.delete_fr_front();
break;
case 5:
d.delete_fr_rear();
break;
case 6:
exit(1);
break;
default:
cout<<"invalid choice";
break;
}
} while(c!=7);
}
//Program to convert the infix expression to a postfix expression
#include<iostream>
using namespace std;
#include<conio.h>

class stack
{
public:
char stack_array[50];
int top;
stack();
void push(char symbol);
char pop();
int empty();
int full();
};
stack::stack()
{
top=-1;
}
void stack::push(char symbol)
{
if(full())
{
cout<<"\n Stack overflow: \n";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char stack::pop()
{
if(empty())
return ('#'); // indicates stack is empty
else
return(stack_array[top--]);
}
int stack::empty()
{
if(top==-1)
return (1);
else
return (0);
}
int stack::full()
{
if(top==49)
return 1;
else
return 0;
}

class Expression
{
char infix[50];
char postfix[50];
public:
void read();
int white_space(char symbol);
void convertToPostfix();
int findPrecedence(char symbol);
};
void Expression::read()
{
cout<<" \n Enter an infix expression:";
cin>>infix;
}
int Expression::white_space(char symbol)
{
if (symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void Expression::convertToPostfix()
{
stack s;
int precedence, p;
char entry1, entry2;
p=0;
for(int i=0;infix[i]!='\0'; i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while( (entry2=s.pop()) != '(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=findPrecedence(entry1);
entry2=s.pop();

while(precedence<=findPrecedence(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>findPrecedence(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1; // other than symbols
break;
}
}
}
while(!s.empty())
postfix[p++]=s.pop();

postfix[p]='\0';
cout<<"The Postfix Expression is: "<<postfix;
}

int Expression::findPrecedence(char symbol)


{
switch(symbol)
{
case '/':
return (4);
case '*':
return (3);
case '+':
return (2);
case '-':
return (1);
case '(':
return (0);
default:
return (-1);
}
}
int main()
{
Expression exp;
exp.read();
exp.convertToPostfix();
return 0;
}
//Program to illustrate Tower of Honai Problem
#include<iostream>
using namespace std;
//tower of HANOI function implementation
void TOH(int n,char Sour, char Aux,char Des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
return;
}

TOH(n-1,Sour, Des, Aux);


cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;
TOH(n-1,Aux,Sour, Des);
}
//main program
int main()
{
int n;

cout<<"Enter no. of disks:";


cin>>n;
//calling the TOH
TOH(n,'A','B','C');
return 0;
}

You might also like