Skip to content
Related Articles
Open in App
Not now

Related Articles

Difference between Stack and Array

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jun, 2022
Improve Article
Save Article

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                         StacksArray
DefinitionStack is a linear data structure represented by a sequential collection of elements in a fixed an orderAn array is a collection of related data values called elements each identified by an indexed array
PrincipleStacks 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]
OperationsInsertion 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.
StorageThe stack has a dynamic size.The array has a fixed size.
Data TypesThe stack can contain elements of different data types.The array contains elements of the same data type.
MethodsWe can do only a linear searchWe can do both linear and Binary search
Data AccessRandom access to elements is not allowed in stacksRandom access to elements is allowed in arrays
ImplementationWe can implement a stack using the arrayWe cannot implement an array using stack
MethodsThere 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.
PointersIt 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:

OperationStackOperation Array
pushO(1)InsertO(n)
popO(1)DeleteO(n)
peekO(1)AccessO(1)
isEmptyO(1)TraversalO(n)
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!