2025-10-06
Testing CenterStacks in CSpeek for examining the element at the top of a stackclearisEmptylenstrin+| Stack Method | What It Does |
|---|---|
s.isEmpty() |
Returns True if s is empty or False otherwise. |
s.__len__() |
Same as len(s). Returns the number of items in s. |
s.__str__() |
Same as str(s). Returns the string representation of s. |
s.__iter__() |
Same as iter(s), or for item in s:. Visits each item in s, from bottom to top. |
s.__contains__(item) |
Same as item in s. Returns True if item is in s or False otherwise. |
s1.__add__(s2) |
Same as s1 + s2. Returns a new stack containing the items in s1 and s2. |
s.__eq__(anyObject) |
Same as s == anyObject. Returns True if s and anyObject are equal. |
s.clear() |
Makes s become empty. |
s.peek() |
Returns the item at the top of s. Precondition: s must not be empty; raises a KeyError if the stack is empty. |
s.push(item) |
Adds item to the top of s. |
s.pop() |
Removes and returns the item at the top of s. Precondition: s must not be empty; raises a KeyError if the stack is empty. |
| Operation | State of Stack after the Operation | Value Returned | Comment |
|---|---|---|---|
s = <Stack Type>() |
Initially, stack is empty. | ||
s.push('a') |
a | The stack contains the single item. | |
s.push('b') |
ab | b is the top item. | |
s.push('c') |
abc | c is the top item. | |
s.isEmpty() |
abc | False | The stack is not empty. |
len(s) |
abc | 3 | The stack contains three items. |
s.peek() |
abc | c | Return the top item without removing it. |
| Operation | State of Stack after the Operation | Value Returned | Comment |
|---|---|---|---|
s.pop() |
ab | c | Remove and return the top item. b is now the top item. |
s.pop() |
a | b | Remove and return the top item. a is now the top item. |
s.pop() |
a | Remove and return the top item. | |
s.isEmpty() |
True | The stack is empty. | |
s.peek() |
KeyError | Peeking at an empty stack raises an exception. | |
s.pop() |
KeyError | Popping an empty stack raises an exception. | |
s.push('d') |
d | d is the top item. |
s1=ArrayStack ()s2=LinkedStack ([20, 40, 60])| Example Expression | Status | Reason |
|---|---|---|
(…)….(…) |
Balanced | |
(…)….(… |
Unbalanced | Missing a closing ) at the end |
)…(...(…) |
Unbalanced | The closing ) at the beginning has no matching opening ( and one opening has no closing |
[…(…)…] |
Balanced | |
[…(…]…) |
Unbalanced | The bracketed sections are not nested properly |
| Infix Form | Postfix Form | Value |
|---|---|---|
| 34 | 34 | 34 |
| 34 + 22 | 34 22 + | 56 |
| 34 + 22 * 2 | 34 22 2 * + | 78 |
| 34 * 22 + 2 | 34 22 * 2 + | 750 |
| (34 + 22) * 2 | 34 22 + 2 * | 112 |
Infix evaluation: 34 + 22 * 2 → 34 + 44 → 78Postfix evaluation: 34 22 2 * + → 34 44 + → 78infix expressions is for the convenience of the human beings who read them and write themTracing the Evaluation of a Postfix Expression : 4 5 6 * +3 −
| Portion of Postfix Expression Scanned So Far | Operand Stack | Comment |
|---|---|---|
| No tokens have been scanned yet, so the stack is empty | ||
| 4 | 4 | Push the operand 4 |
| 4 5 | 4 5 | Push the operand 5 |
| 4 5 6 | 4 5 6 | Push the operand 6 |
| 4 5 6 * | 4 30 | Replace the top two operands by their product |
| 4 5 6 * + | 34 | Replace the top two operands by their sum |
| 4 5 6 * + 3 | 34 3 | Push the operand 3 |
| 4 5 6 * + 3 - | 31 | Replace the top two operands by their difference and pop the final value |
4 + 5 * 6 − 3 to a Postfix Expression| Portion of Infix Expression Scanned So Far | Operator Stack | Postfix Expression | Comment |
|---|---|---|---|
| No tokens have been scanned yet, so the stack and PE are empty | |||
| 4 | 4 | Append 4 to the PE | |
| 4 + | + | 4 | Push + onto the stack |
| 4 + 5 | + | 4 5 | Append 5 to the PE |
| 4 + 5 * | + * | 4 5 | Push * onto the stack |
| 4 + 5 * 6 | + * | 4 5 6 | Append 6 to the PE |
| 4 + 5 * 6 − | − | 4 5 6 * + | Pop * and +, append them to the PE, and push – onto the stack |
| 4 + 5 * 6 − 3 | − | 4 5 6 * + 3 | Append 3 to the PE |
| 4 + 5 * 6 − 3 (end) | 4 5 6 * + 3 − | Pop the remaining operators off the stack and append them to the PE |