You are on page 1of 12

1) Write a code for Push and if(top==-1)

popped item from stack and return 1;


display the stack?
else
#include<iostream>
return 0;
#define SIZE 5
}
using namespace std;

class STACK
int STACK::isFull()
{
{
private:
if(top==(SIZE-1))
int num[SIZE];
return 1;
int top;
else
public:
return 0;
STACK();
}
int push(int);

int pop();
int STACK::push(int n)
int isEmpty();
{
int isFull();
if(isFull())
void displayItems();
{
};
return 0;
STACK::STACK()
}
{
++top;
top=-1;
num[top]=n;
}
return n;

}
int STACK::isEmpty()

{
int STACK::pop() {

{ cout<<endl;

int temp; cout<<"0 for Exit from stack code"<<endl;

if(isEmpty()) cout<<"1 for Push Item in stack"<<endl;

return 0; cout<<"2 for Popped Item from stack"<<endl;

temp=num[top]; cout<<"3 for Display Items in stack (Print


STACK)."<<endl;
--top;

return temp;
cout<<"Enter your choice: ";

cin>>choice;
}

switch(choice)
void STACK::displayItems()
{
{
case 0: break;
int i;

cout<<"STACK is: ";


case 1:
for(i=(top); i>=0; i--)
cout<<"Enter item to insert in stack" <<endl;
cout<<num[i]<<" ";
cin>>n;
cout<<endl;
temp=stk.push(n);
}
if(temp==0)

cout<<"Stack is full."<<endl;
int main()
else
{
cout<<temp<<" inserted."<<endl;
STACK stk;
break;
int choice, n,temp;

do
case 2:
temp=stk.pop(); default:

if(temp==0) cout<<"You entered Invalid choice."<<endl;

cout<<"Stack is empty."<<endl; }

else }

cout<<temp<<" is removed (popped)."<<endl; while(choice!=0);

break;

case 3: return 0;

stk.displayItems();

break; }

Output
2) Implementation of Queue void dequeue();

void enqueue(X x);

X peek();
#include <iostream>
int size();
#include <cstdlib>
bool isEmpty();
using namespace std;
bool isFull();

};
// define default capacity of the queue

#define SIZE 10
// Constructor to initialize queue

template <class X>


// Class for queue
queue<X>::queue(int size)
template <class X>
{
class queue
arr = new X[size];
{
capacity = size;
X *arr; // array to store queue
elements front = 0;

int capacity; // maximum capacity of rear = -1;


the queue
count = 0;
int front; // front points to front
element in the queue (if any) }

int rear; // rear points to last


element in the queue
// Utility function to remove front element
int count; // current size of the from the queue
queue
template <class X>

void queue<X>::dequeue()
public:
{
queue(int size = SIZE); //
// check for queue underflow
constructor
if (isEmpty())
{

cout << "UnderFlow\nProgram rear = (rear + 1) % capacity;


Terminated\n";
arr[rear] = item;
exit(EXIT_FAILURE);
count++;
}
}

cout << "Removing " << arr[front] <<


'\n'; // Utility function to return front element in
the queue

template <class X>


front = (front + 1) % capacity;
X queue<X>::peek()
count--;
{
}
if (isEmpty())

{
// Utility function to add an item to the
queue cout << "UnderFlow\nProgram
Terminated\n";
template <class X>
exit(EXIT_FAILURE);
void queue<X>::enqueue(X item)
}
{
return arr[front];
// check for queue overflow
}
if (isFull())

{
// Utility function to return the size of the
cout << "OverFlow\nProgram queue
Terminated\n";
template <class X>
exit(EXIT_FAILURE);
int queue<X>::size()
}
{

return count;
cout << "Inserting " << item << '\n';
}
q.enqueue("c");

// Utility function to check if the queue is


empty or not
cout << "Front element is: " << q.peek()
template <class X> << endl;

bool queue<X>::isEmpty() q.dequeue();

return (size() == 0); q.enqueue("d");

cout << "Queue size is " << q.size() <<


endl;
// Utility function to check if the queue is
full or not

template <class X> q.dequeue();

bool queue<X>::isFull() q.dequeue();

{ q.dequeue();

return (size() == capacity);

} if (q.isEmpty())

cout << "Queue Is Empty\n";

// main function else

int main() cout << "Queue Is Not


Empty\n";
{

// create a queue of capacity 4


return 0;
queue<string> q(4);
}

q.enqueue("a");

q.enqueue("b");

Output
3) Convert infix to postfix {

stack? if(full())

#include<iostream> {

#include<conio.h> cout<<"\n Stack overflow:\n";

using namespace std; }

class stack else

{ {

public: top=top+1;

char stack_array[50]; stack_array[top]=symbol;

int top; }

stack() }

{ char pop()

top=-1; {

} if(empty())

void push(char symbol) return('#');


else cout<<"\n Enter an infix expression that you
want to convert into postfix";
return(stack_array[top--]);
cin>>infix;
}
}
int empty()
int white_space(char symbol)
{
{
if(top==-1)
if(symbol==' ' || symbol=='\t' || symbol=='\0')
return(1);
return 1;
else
else
return(0);
return 0;
}
}
int full()
void ConvertToPostfix()
{
{
if(top==49)
stack s;
return(1);
int l,precedence,p;
else
char entry1,entry2;
return(0);
p=0;
}
for(int i=0;infix[i]!='\0';i++)
};
{
class Expression
entry1=infix[i];
{
if(!white_space(entry1))
char infix[50];
{
char postfix[50];
switch(entry1)
public:
{
void read()
case '(':
{
s.push(entry1);
break; break;

case ')': default:

while((entry2=s.pop())!='(') postfix[p++]=entry1;

postfix[p++]=entry2; break;

break; }

case '+': }

case '-': }

case '*': while(!s.empty())

case '/': postfix[p++]=s.pop();

if(!s.empty())

{ postfix[p]='\0';

precedence=prec(entry1); cout<<"\n The postfix expression is:


"<<postfix<<endl;
entry2=s.pop();
}
while(precedence<=prec(entry2))
int prec(char symbol)
{
{
postfix[p++]=entry2;
switch(symbol)
if(!s.empty())
{
entry2=s.pop();
case '/': return(4);
else
case '*': return(3);
break;
case '+': return(2);
}
case '-': return(1);
if(precedence>prec(entry2))
case '(': return(0);
s.push(entry2);
default: return(-1);
}
}
s.push(entry1);
}
}; expr.read();

int main() expr.ConvertToPostfix();

{ cout<<"\n\n Do you want to continue if yes


press y otherwise press n (y/n): ";
char choice='y';
cin>>choice;
Expression expr;
}
while(choice=='y')
}
{

Output

4) Balance Parenthesis public:

void createstack()
#include<iostream>
{
#include<conio.h>
top=1;
using namespace std;
}
const char size=10;
bool isempty()
class stack{
{
private:
return (top==-1);
int top;
}
char arr[size];
bool isFull()

{ }

return (top==size-1); void display()

} for(int i=0;i<10;i++)

void push(char value) {

if(isFull()) cout<<arr[i];

{ }

cout<<"Stack is full"<<"\n"; }

} };

else{ int main()

top=top+1; {

arr[top]=value; stack s1;

s1.createstack();

} int cont=1;

} char ch;

int pop(){ while(ch!='x'&&cont)

if(isempty()){ {

cout<<"Stack is empty"; ch=getche();

} if(ch=='('||ch==')')

else{ {

top--; if(ch=='('){

s1.push('(');
} cout<<endl<<"balanced";

else if(s1.isempty()) }

{ else{

cont=0; cout<<endl<<"not balanced";

} }

else{ return 0;

s1.pop(); }

if(ch=='x' && s1.isempty()) }

Output

You might also like