Docsity

Data Structure And Algorithum, Assignments of Programming Languages

Toggle sidebar
bg1
DATA STRUCTURES AND ALGORITHM
ASSIGNMENT # 2
GROUP MEMBERS
NAME: Laiba Nasir(01-131212-017)
NAME: Minahil Naseer(01-131212-018)
ADT STACK:
#include <iostream>
#include<stack>
using namespace std;
template <class ItemType>
class Stack
{
private:
int top;
int maxStack;
ItemType* items;
public:
Stack();
Stack(int);
~Stack();
bool isEmpty();
bool isFull();
void push(ItemType newItem);
void pop(ItemType& item);
};
template<class ItemType>
Stack<ItemType>::Stack() {
maxStack = 100;
top = -1;
items = new ItemType[500];
}
template<class ItemType>
Stack<ItemType>::Stack(int maxStack) {
this->maxStack = maxStack;
top = -1;
items = new ItemType[maxStack];
}
template<class ItemType>
Stack<ItemType>::~Stack()
{
pf3
pf4

Partial preview of the text

Download Data Structure And Algorithum and more Assignments Programming Languages in PDF only on Docsity!

DATA STRUCTURES AND ALGORITHM

ASSIGNMENT # 2

GROUP MEMBERS

NAME: Laiba Nasir(01- 131212 - 017)

NAME: Minahil Naseer(01- 131212 - 018)

ADT STACK:

#include #include using namespace std; template class Stack { private: int top; int maxStack; ItemType* items; public: Stack(); Stack(int); ~Stack(); bool isEmpty(); bool isFull(); void push(ItemType newItem); void pop(ItemType& item); }; template Stack::Stack() { maxStack = 100; top = - 1; items = new ItemType[500]; } template Stack::Stack(int maxStack) { this->maxStack = maxStack; top = - 1; items = new ItemType[maxStack]; } template Stack::~Stack() {

delete[] items; } template bool Stack::isEmpty() { return (top == - 1); } template bool Stack::isFull() { return (top == maxStack - 1); } template void Stack::push(ItemType newItem) { if (isFull()) { cout << "StacK Overflow" << endl; exit(1); } top++; items[top] = newItem; } template void Stack::pop(ItemType& item) { if (isEmpty()) { cout << "Stack Underflow" << endl; exit(1); } item = items[top]; top--; }

DRIVER CODE:

#include #include #include #include"Stack.h" using namespace std; int main() { Stack s; string exp, push = " ", pop = " "; char ch; ofstream writeFile("input.txt"); ifstream readFile("input.txt"); ofstream myFile("output.txt"); cout << "Enter the expression : "; cin >> exp; writeFile << exp; readFile >> exp; for (int i = 0; i < exp.length(); i++) { ch = exp[i];