In safety-critical firmware, dynamic memory allocation (heap) is often forbidden due to fragmentation risks. Data structures like stacks and queues must have their maximum size fixed at compile-time to ensure deterministic memory usage.
Your task is to implement a Class Template named StaticStack. It must accept two template parameters:
typename T: The type of data to store.int Capacity: The maximum number of elements (Non-Type Template Parameter).The class must encapsulate a raw array T data[Capacity] and a top_index. Implement the following methods:
void push(T val): Add element. If full, print "Stack Overflow" and ignore.void pop(): Remove top element. If empty, print "Stack Underflow" and ignore.T peek(): Return top element. If empty, return 0 (or default construct T()) and print nothing (error handling logic varies, but for this exercise, assume safe usage or check empty first).bool isEmpty(): Returns true if empty.Program Flow:
main function (provided) instantiates a StaticStack<int, 3> (Capacity = 3).N (number of commands).N times.command.val and call push(val).pop().peek().Input Format:
N.N lines: Command string, followed by val only if command is "PUSH".Output Format:
PUSH: No output (unless overflow).POP: No output (unless underflow).PEEK: Print Top: <value>.EMPTY: Print Empty: Yes or Empty: No.Stack Overflow or Stack Underflow exactly.Example:
Example 1
Input:
6
PUSH 10
PUSH 20
PUSH 30
PUSH 40
PEEK
POPOutput:
Stack Overflow
Top: 30(Explanation: Stack size is 3. Pushing 10, 20, 30 fills it. Pushing 40 triggers Overflow. Peek shows 30. Pop removes 30.)
Constraints:
N range: 1 to 20int, but class must be a template.std::vector or new/malloc.
Input
6 PUSH 10 PUSH 20 PUSH 30 PUSH 40 PEEK POP
Expected Output
Stack Overflow Top: 30