Open In App

Difference between Stack and Array

Last Updated : 21 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Stack: 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 a stack is called push operation, and the 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 the stack is given below: 

 

Array: 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:

Basis of Comparison                          Stacks Array
Definition Stack is a linear data structure represented by a sequential collection of elements in a fixed an order An array is a collection of related data values called elements each identified by an indexed array
Principle 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[4]
Operations Insertion and deletion in stacks take place only from one end of the list called the top. Insertion and deletion in the array can be done at any index in the array.
Storage The stack has a dynamic size. The array has a fixed size.
Data Types The stack can contain elements of different data types. The array contains elements of the same data type.
Methods We can do only a linear search We can do both linear and Binary search
Data Access Random access to elements is not allowed in stacks Random access to elements is allowed in arrays
Implementation We can implement a stack using the array We cannot implement an array using stack
Methods There are limited number of operations can be performed on a stack: push, pop, peek, etc. It is rich in methods or operations that can be perform on it like sorting, traversing, reverse, push, pop, etc.
Pointers It has only one pointer- the top. This pointer indicates the address of the topmost element or the last inserted one of the stack. In arrays, memory can be allocated in compile-time and is also known as static memory allocation.

 Complexity Analysis:

Operation Stack Operation  Array
push O(1) Insert O(n)
pop O(1) Delete O(n)
peek O(1) Access O(1)
isEmpty O(1) Traversal O(n)

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads