Stack vs. Queue
First, we will look at what is stack and what is queue individually, and then we will
discuss the differences between stack and queue.
What is a Stack?
A Stack is a linear data structure. In case of an array, random access is possible, i.e., any
element of an array can be accessed at any time, whereas in a stack, the sequential
access is only possible. It is a container that follows the insertion and deletion rule. It
follows the principle LIFO (Last In First Out) in which the insertion and deletion take
place from one side known as a top. In stack, we can insert the elements of a similar
data type, i.e., the different data type elements cannot be inserted in the same stack.
The two operations are performed in LIFO, i.e., push and pop operation.
The following are the operations that can be performed on the stack:
o push(x): It is an operation in which the elements are inserted at the top of the
stack. In the push function, we need to pass an element which we want to insert
in a stack.
o pop(): It is an operation in which the elements are deleted from the top of the
stack. In the pop() function, we do not have to pass any argument.
o peek()/top(): This function returns the value of the topmost element available
in the stack. Like pop(), it returns the value of the topmost element but does
not remove that element from the stack.
First, we will look at what is stack and what is queue individually, and then we will
discuss the differences between stack and queue.
What is a Stack?
A Stack is a linear data structure. In case of an array, random access is possible, i.e., any
element of an array can be accessed at any time, whereas in a stack, the sequential
access is only possible. It is a container that follows the insertion and deletion rule. It
follows the principle LIFO (Last In First Out) in which the insertion and deletion take
place from one side known as a top. In stack, we can insert the elements of a similar
data type, i.e., the different data type elements cannot be inserted in the same stack.
The two operations are performed in LIFO, i.e., push and pop operation.
The following are the operations that can be performed on the stack:
o push(x): It is an operation in which the elements are inserted at the top of the
stack. In the push function, we need to pass an element which we want to insert
in a stack.
o pop(): It is an operation in which the elements are deleted from the top of the
stack. In the pop() function, we do not have to pass any argument.
o peek()/top(): This function returns the value of the topmost element available
in the stack. Like pop(), it returns the value of the topmost element but does
not remove that element from the stack.