Open In App

When to use Array over Linked List and vice versa?

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

Array:

An array is a linear data structure that is a collection of homogeneous elements. Arrays are stored in contiguous memory locations. An array is a static data structure that combines data of similar types.

Example of an Array

Example of an Array

Linked List:

A linked list is a linear data structure that contains nodes and pointers. Each node contains data and pointers to another node. It is an ordered collection of data elements called nodes and the linear order is maintained by pointers. Like an array, a linked list also contains elements of homogeneous data types.

Example of a Linked List

Example of a Linked List

Scenarios in which we use Linked Lists:

  • Linked Lists are used when the number of elements is not known in advance i.e. size is not known as linked lists support dynamic memory allocation.
  • Linked lists are simple and can be used to implement other data structures like stack, queue, and tree.
  • Linked Lists can be used for the manipulation of polynomials.
  • Linked lists are used for performing arithmetic operations on long integers.
  • Linked List can be used in cases when faster insertion and deletion are required. Linked takes O(1) time complexity for insertion and deletion while array takes O(N).

Scenarios in which we use Arrays:

  • Arrays are used when we require random access to elements.
  • Arrays are used as the base of all sorting algorithms.
  • Arrays are used when the number of elements(size of an array) is known in advance as the array supports static memory allocation.
  • Arrays are used to implement matrices.
  • Arrays can be used to implement various data structures like stacks, queues, and trees.

Related articles:


Similar Reads

Minimize cost to modify the Array such that even indices have even elements and vice versa
Given an array arr[] of size N and two integers X and Y, the task is to find the minimum cost required to modify the array such that even indices have even elements and odd indices have odd elements on them, either by swapping two elements of the array with a cost of X or by incrementing or decrementing 1 from the element with a cost of Y. Examples
7 min read
Check if array sum of first half is divisible by sum of other half or vice versa
Given an array arr[] of size N, the task is to check if the sum of the left subarray is divisible by the sum of the right subarray or vice-versa. Print Yes if it was, otherwise No. Here, the left subarray will contain the string from 0 to mid=(N-1)/2 and the right subarray will contain the string from mid+1 to N-1. Example: Input: arr[] = [1, 2, 3,
5 min read
Check if Array forms an increasing-decreasing sequence or vice versa
Given an array arr[] of N integers, the task is to find if the array can be divided into 2 sub-array such that the first sub-array is strictly increasing and the second sub-array is strictly decreasing or vice versa. If the given array can be divided then print "Yes" else print "No". Examples: Input: arr[] = {3, 1, -2, -2, -1, 3} Output: Yes Explan
14 min read
Substrings starting with vowel and ending with consonants and vice versa
Given a string s, count special substrings in it. A Substring of S is said to be special if either of the following properties is satisfied. It starts with a vowel and ends with a consonant.It starts with a consonant and ends with a vowel. Examples: Input : S = "aba" Output : 2 Substrings of S are : a, ab, aba, b, ba, a Out of these only 'ab' and '
17 min read
Program to Convert Km/hr to miles/hr and vice versa
Given a speed in km/hr, Convert it to m/hr and m/hr to km/hr. Examples: Input : 150 (km/hr) Output : 93.21 Input : 100 (m/hr) Output : 160.92693917 The conversion formula for kph to mph is - 1 kilo-meter = 0.621371192 miles The conversion formula for mph to kph is - 1 miles = 1.60934 kilo-meter C/C++ Code // Cpp program for conversion of // kmph to
4 min read
Program to convert speed in km/hr to m/sec and vice versa
Convert speed given in km/hr to m/sec and vice versa. Examples: Input : kmph = 72, mps = 10 Output : speed_in_mps = 20 speed_in_kmph = 36 Input : kmph = 54, mps = 15 Output : speed_in_mps = 15 speed_in_kmph = 54 Approach: 1 km = 1000 m and 1 hr = 3600 sec (60 min x 60 sec each). Conversion from Km/h to m/sec- When converting km/hr to m/sec, multipl
4 min read
Minimize changes to make all characters equal by changing vowel to consonant and vice versa
Given a string str[] of lower-case characters, the task is to make all characters of the string equal in the minimum number of operations such that in each operation either choose a vowel and change it to a consonant or vice-versa. Examples: Input: str[] = "geeksforgeeks"Output: 10Explanation: To make all the characters equal, make the following ch
9 min read
Count unique Strings by replacing Consonant with closest Vowel and vice versa
Given a string S consisting of lowercase English alphabets, of length N, the task is to find the total number of distinct strings that can be obtained by performing the given operations on every character of the string S: If the character is a consonant, change the character to its closest vowel.If the character is a vowel, change the character to
6 min read
Make all characters in given string equal by changing vowel into consonant and vice versa
Given a string str containing lowercase characters, the task is to find the minimum number of operations needed to make all the characters equal. In one operation, any consonant can be converted to any vowel or any vowel can be converted to any consonant. Examples: Input: str = "banana"Output: 3Explanation: Convert all consonants to Vowel character
14 min read
Convert from any base to decimal and vice versa
Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. The value of A is 10, the value of B is 11 and so on. Write a function to convert the number to decimal. Examples: Input number is given as string and output is an integer.Input: str = "1100", base
16 min read
Article Tags :
Practice Tags :