Stack and queue are the simplest of complex data structures.
There are two basic methods for organizing the manipulation of a data structure:
- FIFO (First In First Out),
- LIFO (Last In Last Out).
FIFO - is a method of organizing the manipulation of a data structure (queue), where elements that we choose are inserted into the end (tail) of structure and extracted of the entry (head) of structure solely.
LIFO - is a method of organizing the manipulation of a data structure (stack), where elements that we choose are inserted and extracted into / from the end of structure solely.
==>
Stack - is a data structure that is orginised according to the principe of LIFO. Queue - is a data structure that is orginised according to the principe of FIFO.
==>
Methods and difficulties
- push()
PURPOSE: to add element into the end (top) of the stack. DIFFICULTY: O(1).
- pop()
PURPOSE: to return element of the end (top) of the stack. DIFFICULTY: O(1).
- peek()
PURPOSE: to return element of the end (top) of the stack but doesn't delete this item. DIFFICULTY: O(1).
- count()
PURPOSE: to return a number of a quantity of elements of the stack (depth of the stack). DIFFICULTY: O(1).
P.S. We are going to use an array for easy reading but an usage a linked list is better because the linked list allows to improve our structure.