Stack is an ordered collection of items where the addition of new items and removal of existing items takes place at the same end. The recently added item is the one that is in position to be removed first. This ordering principle is called: LIFO - Last In First Out.
The stack operations are:
Stack() creates a new stack that is empty. Requires no parameters and returns an empty stack.
push(item) adds a new item to the top of the stack. It requires the items and returns nothing.
pop() removes the top item from the stack. It requires no parameters and returns the item. The stack is modified.
peek() return the top item from the stack. Requires no parameters. The stack is not modified.
isEmpty() test to see if the stack is empty. Requires no parameters and returns a boolean value.
size() return the number of items in the stack. Requires no parameters and returns an integer.
time t (operation) | Stack operation | Stack content | Return value |
t | Stack() | [] | |
isEmpty() | [] | True | |
t+1 | push("white") | [white] | |
t+2 | push("red") | [white, red] | |
t+3 | push("green") | [white, red ,green] | |
t+4 | push("blue") | [white, red, green, blue] | |
t+5 | push("black") | [white, red, green, blue, black] | |
peek() | [white, red, green, blue, black] | black | |
size() | [white, red, green, blue, black] | 5 | |
t+6 | pop() | [white, red, green, blue] | black |
t+7 | pop() | [white, red, green] | blue |
t+8 | pop() | [white, red] | green |
t+9 | pop() | [white] | red |
isEmpty() | [white] | False | |
peek() | [white] | white | |
size() | [white] | 1 |
Queues is an ordered collection of items where the addition of new items happens at one "rear" and removal of existing items takes place at the other end "front".
The recently added item is the one that is in position to be removed last. This ordering principle is called: FIFO - First In First Out
The queue operations are:
Queue() creates a new queue that is empty. Requires no parameters and returns an empty queue
enqueue(item) adds a new item to the rear of the queue. It requires the items and returns nothing
dequeue() removes the front item from the queue. It requires no parameters and returns the item. The queue is modified.
isEmpty() test to see if the queue is empty. Requires no parameters and returns a boolean value.
size() return the number of items in the queue. Requires no parameters and returns an integer.
time t (operation) | Queue operation | Queue content | Return value |
t | Queue() | [] | |
isEmpty() | [] | True | |
t+1 | enqueue("white") | [white] | |
t+2 | enqueue("red") | [white, red] | |
t+3 | enqueue("green") | [white, red ,green] | |
t+4 | enqueue("blue") | [white, red, green, blue] | |
t+5 | enqueue("black") | [white, red, green, blue, black] | |
size() | [white, red, green, blue, black] | 5 | |
t+6 | dequeue() | [red, green, blue, black] | white |
t+7 | isEmpty() | [red, green, blue, black] | False |