Difference between Stack and Array
A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into stack is called push operation, and deletion of an element from the stack is called pop operation. In stack we always keep track of the last element present in the list with a pointer called top.
The diagrammatic representation of stack is given below:
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
The diagrammatic representation of Array is given below:
Difference between Stack and Array Data Structures:
|Stacks are based on the LIFO principle, i.e., the element inserted at the last, is the first element to come out of the list.||In the array the elements belong to indexes, i.e., if you want to get into the fourth element you have to write the variable name with its index or location within the square bracket eg arr|
|Insertion and deletion in stacks takes place only from one end of the list called the top.||Insertion and deletion in array can be done at any index in the array.|
|Stack has a dynamic size.||Array has a fixed size.|
|Stack can contain elements of different data type.||Array contains elements of same data type.|
|We can do only linear search||We can do both linear and Binary search|
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.