Stack Flashcards
1
Q
Stack push
A
void push(T value) { if (isFull()) { cout << "Stack is already full" << endl; return; } topIndex++; stack[topIndex] = value; }
2
Q
Stack pop
A
void pop() { if (isEmpty()) { cout << "Stack is already empty" << endl; return; } topIndex--; }
3
Q
Stack top/peek
A
T top() const { if (isEmpty()) { cout << "Stack is empty" << endl; return T(); } return stack[topIndex]; }
4
Q
Stack isEmpty
A
bool isEmpty() const { return topIndex == -1; }
5
Q
Stack isFull
A
bool isFull() const { return topIndex == MAXSIZE - 1; }
6
Q
Stack clear
A
void clear() { topIndex = -1; }
7
Q
Stack display
A
template <typename T> void display(const Stack<T>& s) { if (s.isEmpty()) { cout << "Stack is empty" << endl; return; } Stack<T> temp = s; while (!temp.isEmpty()) { cout << temp.top() << " "; temp.pop(); } cout << endl; }