07.STACKS
Last updated
Last updated
a list of homogenous elements, wherein the addition & deletion of elements occurs only at one end (top of the stack).
The stack is a last-in-first out concept (LIFO) or first-in-last-out (FILO) data structure
the bottom element of the stack is the earliest/first added one - this is the last one to be deleted
the top element is the last added item
the last added item will be the first one to be deleted
isEmptyStack: Checks whether the stack is empty
isFullStack: Checks whether the stack is full
this can only be done if the implementation is an array list
push: Adds a new element to the top of the stack
top: Returns ore retrieves the information from top of the stack
pop: Removes the top of the stack
Update the ArrayList class to define the three main stack functions: push, top, & pop
Update the LinkedList class to define the three main stack functions: push, top & pop
Define a new class Stack that inherits the class ArrayList
Define a new class Stack that inherits the class LinkedList
postfix expression simplifies the manipulation of arithmetic expression
PROCEDURE
push(6)
push(3)
whenever an operator is encountered, the last two pushed elements (6,3) in this case must be handled using the plus sign
x = top()
x will be 3
pop()
this removes 3 from the stack
y = top()
y will be 6
pop()
this removes 6 from the stack
push(x +y)
the stack will now have 9 which is the sum of 6 and 3
push(2)
m = top()
m will be 2
pop()
this removes 2 from the stack
n = top()
n will be 9
pop()
this removes 9 from the stack
push(m * n)
the stack will now have 18 which is the product of 9 and 2