\(t\) is a variable that refers to the top. Initial value is -1 (stack empty)
Operation
Return Type
Function
push(element)
void
inserts element at top position
pop()
element
removes topmost element and returns the removed element
top()
element
returns the topmost element
size()
int
returns no of elements
isEmpty()
boolean
checks if empty
Algorithm push(element)
if t = n-1
overflow
t = t + 1
a[t] = element
Algorithm pop()
if t = -1
underflow
t = t - 1
return a[t]
Algorithm size()
return (t+1)
Algorithm top()
return a[t]
Algorithm isEmpty()
if t = -1
return true
Weβre basically checking if all the brackets are matched
while there are symbols in the expression do
if symbol is variable
do nothing
if symbol is opening
push it to the stack
if symbol is closing symbol
if stack is empty
invalid
else
valid
if stack is empty // once evaluation of expression is over
valid
else
invalid
Token
Stack
Reason
Last Updated: 2023-01-25 ; Contributors: AhmedThahir