Introduction:
The stack is a fundamental data structure based on the concept of Last-In-First-Out (LIFO)
, where the last added element is the first one to be removed and processed.
Some functions in Stack are described as bellow:
bool is_full()
: returns true if the stack is full (i.e., size == MAX_SIZE), otherwise, return false.bool is_empty()
: returns true if the stack is empty(i.e., size == 0); otherwise, returns false.void clear()
: Remove all the elements in the queue. Make the queue is empty.void push(int element)
: Add an element
to the top of the stack. If the stack is full, no action is performedint pop()
: remove and return the last element in the stack. If the stack is empty, no action is performed. Return error code -1 if the stack is emptyint top()
: return without removing the last element of the stack. Return error code -1 if the stack is empty.
static const int MAX_SIZE = 4; // Capacity of the stack
int data[MAX_SIZE]; // Linearly stores data in a fixed-size
array
int size; // Current number of elements in the
array
// Default constructor initializes an empty stack
List() {
size = 0;
}
Note: The capacity of the stack is MAX_SIZE = 4
. If an operation cannot be performed, the stack remains unchanged.
Based on the above configuration and function definitions,, complete the table in your question paper:
Id | Operators | data[] | size | is_empty() | is_full() |
---|---|---|---|---|---|
0 | Stack myStack = Stack(); | [] | 0 | true | false |
1 | myStack.push(5) | [5] | 1 | false | false |
2 | myStack.push(8) | [5, 8] | 2 | false | false |
3 | myStack.push(3) | ||||
4 | myStack.push(7) | ||||
5 | myStack.push(10) | ||||
6 | myStack.pop() | ||||
7 | myStack.push(2) | ||||
8 | myStack.top() | ||||
9 | myStack.push(9) | ||||
10 | myStack.top() | ||||
11 | myStack.push(4) | ||||
12 | myStack.pop() | ||||
13 | myStack.clear() |