Question 3 - Stack using Array
itsLu
c_cpp
a year ago
1.2 kB
15
Indexable
/* 3- Write a class to simulate a stack using array. Class must have a constructor and {push, pop, isFull, isEmpty, doubleSize} functions. */ #include <iostream> using namespace std; class ArrayStack { int stackSize, *arr, top; ArrayStack(int s = 1) { stackSize = s; arr = new int [stacksize]; top = 0; } bool isFull() { return top == stackSize; } bool isEmpty() { return top == 0; } void doubleSize() { int *temparr = new int [stackSize * 2]; for (int k = 0 ; k < top ; k++) temparr[k] = arr[k]; stackSize*=2; delete[]arr; arr = temparr; } void push(int item) { if (isFull()) doubleSize(); arr[top] = item; top++; } int pop() { if (isEmpty()) { cout << "Stack is empty!" << endl; return -1; } top--; return arr[top]; } //Destructor مش لازم تكتبه دي حتة زيادة الدكتور ماطلبهاش في السؤال ~ArrayStack() { delete[] arr; cout << "Stack is deleted!" << endl; } };
Editor is loading...
Leave a Comment