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|
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Stack Permutations (Check if an array is stack permutation of other)
- Difference between Stack and Queue Data Structures
- Growable array based stack
- Check if an array is stack sortable
- Range Sum and Update in Array : Segment Tree using Stack
- Count the number of pop operations on stack to get each element of the array
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Find maximum in stack in O(1) without using additional stack
- Difference between Static allocation and Stack allocation
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Generate Array whose difference of each element with its left yields the given Array
- Difference between pointer to an array and array of pointers
- Sum of elements in an array whose difference with the mean of another array is less than k
- Difference between Programmable Logic Array and Programming Array Logic
- Array formed from difference of each element from the largest element in the given array
- Difference between highest and least frequencies in an array
- Triplets in array with absolute difference less than k
- Maximum possible difference of two subsets of an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.