Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

North South University

Student Name: Anindita Das Mishi

Student ID No: 2211364642

Course name and code: CSE225.06

Title of the assignment: Balance Parenthesis Problem


Stack.h

#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

const int MAX_SIZE = 20;


template <class T>
class Stack
{
public :
T temp[MAX_SIZE];
int top;
Stack();
void Push(T);
void Pop();
T topPosition();
bool IsEmpty();
void print();
};
#endif // STACK_H_INCLUDED

Stack.cpp

#include "Stack.h"
#include<iostream>
using namespace std;

template <class T>


Stack<T>::Stack()
{
top=-1; //Stack is empty
}

template <class T>


void Stack<T>::Push(T data)
{
if(top==MAX_SIZE-1)
{
cout<<"List is full."<<endl;
return;
}
else
{
top=top+1;
temp[top]=data;
}
}

template<class T>
void Stack<T>::Pop()
{
if(top==-1)
{
cout<<"List is Empty."<<endl;
return;
}
else
top=top-1;
}

template <class T>


T Stack<T>::topPosition()
{
return temp[top];
}

template<class T>
bool Stack<T>::IsEmpty()
{
if(top==-1)
{
return true;
}
else
return false;
}

template <class T>


void Stack<T>::print()
{
int i;
cout<<"Stack: "<< endl;
for(i=0; i<=top; i++)
{
cout<<temp[i] ;
}
cout << endl;
}

Main.cpp

#include "src\Stack.cpp"
#include <iostream>
using namespace std;

int main()
{
Stack<char> stack;
Stack<char> bp;
stack.Push('(');
stack.Push('(');
stack.Push('{');
stack.Push('(');
stack.Push('(');
stack.Push(')');
stack.Push(')');
stack.Push('}');
stack.Push(')');
stack.Push(')');

for(int i=0; i<=stack.top; i++)


{
if(stack.temp[i]=='('||stack.temp[i]=='{'||stack.temp[i]=='[')
{
bp.Push(stack.temp[i]);
}
else if(stack.temp[i]==')')
{
if(bp.IsEmpty()||bp.topPosition()!='(')
{
cout<<"Not Balanced"<<endl;
return 0;
}
else
bp.Pop();
}
else if(stack.temp[i]=='}')
{
if(bp.IsEmpty()||bp.topPosition()!='{')
{
cout<<"Not Balanced"<<endl;
return 0;
}
else
bp.Pop();
}
else if(stack.temp[i]==']')
{
if(bp.IsEmpty()||bp.topPosition()!='[')
{
cout<<"Not Balanced"<<endl;
return 0;
}
else
bp.Pop();
}
}
if(bp.IsEmpty())
{
cout << "Balanced" << endl;
}
else
{
cout<< "Not Balanced" << endl;
}
}

You might also like