Stacks and Queues

Stacks and queues are types of lists, although they managed adding and removing elements differently. Normal arrays allow you to add and remove elements at any index within the array. Stacks use First In, First Out (FIFO) and queues use a Last In, First Out (LIFO) method of processing data and elements.

Queues act like queues at shopping markets, the first person in the queue is the first person to be served. Whereas, stacks act like stacks of pancakes, the first pancake put on the plate is the last pancake to be eaten. The last pancake, the one at the top of the stack, will be the first pancake to be eaten.

Some programming languages have specific types of lists that act as stacks or queues. Other languages allow you to treat any create list as a stack of a queue at the same time. For example, the programming language JavaScript has four methods that can be applied to any created list of elements.

push() adds an element to the end of an array.

pop() removes and element from the end of the array.

shift() adds an element to the start of an array.

unshift() removes and element from the start of the array.

Leave a Reply