You are on page 1of 13

Linked List:

o Linked List can be defined as collection of objects called Nodes that are
randomly stored in the memory.
o A node contains two fields (i) Data stored & (ii) Link (i.e. Address) of the
Next Node.
o The last node of the Linked List contains pointer to the null.

The Linked List operates in similar way to the game of Treasure Hunt. In a way
that, previous clue includes the information about the next clue.

Need for Linked List Data Structure

Dynamic Data Structure: The size of memory can be allocated or de-allocated


at run time based on the operation insertion or deletion.

Ease of Insertion/Deletion: The insertion and deletion of elements are simpler


than Arrays since no elements need to be shifted after insertion and deletion,
Just the addresses need to be updated.

Efficient Memory Utilization: As we know Linked List is a Dynamic Data


Structure the size increases or decreases as per the requirement so this avoids
the wastage of memory.

Implementation: Various Advanced Data Structures can be implemented using


a Linked List like Stack, Queue, Graph, Hash Maps, etc.
Array vs Linked List:

Operations performed on the Linked list


The basic operations that are supported by Linked List are -

o Insertion - This operation is performed to add an element into the Linked


List.
o Deletion - It is performed to delete an element from the Linked List.
o Display - It is performed to display the elements of the Linked List.
o Search - It is performed to search an element from the list using the given
key.
Types of Linked List:

There are mainly three types of Linked Lists:


1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

1. Singly Linked List

Traversal of nodes can be done in the forward direction only due to the linking
of every node to its next node.

2. Doubly Linked List

Traversal of nodes can be done in both forward and backward directions as


every node contains an additional prev pointer that points to the previous
node.
3. Circular Linked List

A Circular Linked List is a type of Linked List in which the first and the last nodes
are also connected to each other to form a circle, there is no NULL at the end.

Advantages of Linked List

The advantages of using the Linked List are:

o Dynamic Data Structure - The size of the Linked List may vary according
to the requirements. Linked list does not have a fixed size.

o Insertion and Deletion - Unlike Arrays, insertion and deletion in Linked


List is easier. Array elements are stored in the consecutive location,
whereas the elements in the Linked List are stored at a random location.

To insert or delete an element in an Array, we have to shift the


elements for creating the space. Whereas, in Linked List, instead of
shifting, we just have to update the address of the pointer of the node.
o Memory efficient - The size of a Linked List can grow or shrink according
to the requirements, so memory consumption in Linked List is efficient.

o Implementation - We can implement both Stacks and Queues using


Linked List.

Disadvantages of Linked List

The disadvantages of using the Linked List are:

o Memory usage - In Linked List, node occupies more memory than Array.
Each node of the Linked List occupies two types of variables, i.e., one is a
simple variable, and another one is the pointer variable.

o Traversal - Traversal is not easy in the Linked List. If we have to access an


element in the Linked List, we cannot access it randomly, while in case of
an Array we can randomly access it by index.

For example, if we want to access the 3rd node, then we need to traverse
all the nodes before it. So, the time required to access a particular node
is more in comparison to Array.

o Backtracking or Reverse traversing - Backtracking or Reverse traversing


is difficult in a Linked List. In a Doubly-Linked List, it is easier but requires
more memory to store the back pointer as well.
Applications of Linked List:

 Memory Management: Memory Management is one of the operating


system's key features. It decides how to allocate and reclaim storage for
processes running on the system.

We can use the Linked List to keep track of portions of memory


available for allocation.

 Virtual Memory: An interesting application of Linked Lists is found in the


way systems support Virtual Memory.

 Support for other data structures: Some other Data Structures like
Stacks, Queues, Hash Tables, Graphs can be implemented using the
Linked List.

 Polynomial Representation: With the help of the Linked List, the


polynomials can be represented as we can perform the operations on the
polynomial.

Advantages of Doubly Linked List:

 Due to the next and prior pointers, it supports traversing in both ways
compared to the Singly Linked List (which only supports one direction).

 Deletion of nodes is easy as compared to a Singly Linked List. In Singly


Linked List deletion requires a pointer to the node and previous node to
be deleted but in the Doubly Linked List, it only required the pointer
which is to be deleted.
 It is also easy to Reverse a Doubly Linked List. It can be reversed by
switching the next & previous pointers for each node and updating the
head node to point to the last node.

 Doubly Linked Lists have a low overhead compared to other data


structures such as Arrays.

Disadvantages of Doubly Linked List:

 As it is having an additional previous pointer for each node, it uses more


memory than a Singly Linked List.

 Traversing a Doubly Linked List can be slower than traversing a Singly


Linked List.

 Implementing and maintaining Doubly Linked Lists can be more complex


than Singly Linked Lists.

Applications of Doubly Linked List:

 It is used in the navigation systems where front and back navigation is


required.

 It is used by the browser to implement backward and forward navigation


of visited web pages that is a back and forward button.

 It is also used by various applications to implement undo and redo


functionality.

 Other Data Structures like Stacks, Hash Tables, Binary Trees can also be
constructed or programmed using a Doubly-Linked List.

 It is used in implementing Graph algorithms & LRU Cache.


Advantages of Circular Linked List:

 It is possible to traverse from the last node back to the first i.e. the head
node.

 The starting node does not matter as we can traverse each and every
node despite whatever node we keep as the starting node.

 There is no need for a NULL function to code. The Circular Linked List
never identifies a NULL identifier unless it is fully assigned.

 The previous node can be easily identified.

 Circular Linked Lists are beneficial for end operations as start and finish
coincide.

Disadvantages of Circular Linked List:

 If the Circular Linked List is not handled properly then it can lead to an
infinite loop as it is circular in nature.

 In comparison to Singly Linked List & Doubly Linked List, the Circular
Linked List is more complex in nature.

 Direct accessing of elements is not possible in case of Circular Linked List.

 It is generally a complex task to reverse a Circular Linked List.


Applications of Circular Linked List:

 Circular Linked Lists can be used for applications in which the entire list is
accessed in a loop.

 It can also be used by the Operating System to share time with different
users. Generally, it uses a Round Robin time-sharing method.

 Multiplayer games utilize a circular list to switch between players in a


loop.

 Implementation of advanced data structures such as Fibonacci Heap.

 We can access the Browser Cache by hitting the BACK key.


Polynomial Representation through Linked List

While representing a polynomial using a Linked List, each polynomial term


represents a node in the Linked List.

To get better efficiency in processing, we assume that the term of every


polynomial is stored within the Linked List in the order of decreasing exponents.

Each node of a linked list representing polynomial constitute three parts:

o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, Link points to the next term (next node).

The structure of a node of a linked list that represents a polynomial

Consider a polynomial P(x) = 7x4 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the
coefficients, and 4,3,2,0 are the exponents of the terms in the polynomial. On
representing this polynomial using a Linked List:

Observe that the number of nodes equals the number of terms in the
polynomial. So, we have 4 nodes. Moreover, the terms are stored in decreasing
order of exponents in the Linked List.

Such representation of Polynomial using Linked Lists makes the operations like
subtraction, addition, multiplication, etc., on polynomial very easy.
Polynomial of Multiple Variables

We can represent a polynomial with more than one variable, i.e., it can be two
or three variables. Below is a node structure suitable for representing a
polynomial with three variables X, Y, Z using a Singly Linked List.

Consider a polynomial P(x, y, z) = 10x2y2z + 17 x2y z2 - 5 xy2 z+ 21y4z2 + 7.


On representing this polynomial using Linked List as:

Terms in such a polynomial are ordered accordingly to the decreasing degree in


x. Those with the same degree in x are ordered according to decreasing degree
in y. Those with the same degree in x and y are ordered according to decreasing
degrees in z.

Addition of Polynomials:

To add two polynomials, we traverse the list P and Q. We take corresponding


terms of the list P and Q and compare their exponents. If the two exponents are
equal, the coefficients are added to create a new coefficient. If the new
coefficient is equal to 0, then the term is dropped, and if it is not zero, it is
inserted at the end of the new linked list containing the resulting polynomial.

If one of the exponents is larger than the other, the corresponding term is
immediately placed into the new linked list, and the term with the smaller
exponent is held to be compared with the next term from the other list.

If one list ends before the other, the rest of the terms of the longer list is inserted
at the end of the new linked list containing the resulting polynomial.
Example,

P(x) = 3x4 + 2x3 - 4 x2 + 7

Q (x) = 5x3 + 4 x2 - 5

These polynomials are represented using a linked list in order of decreasing


exponents as follows:

We then compare the Exponent of the next term of the List ‘P’ with the
Exponents of the present term of List ‘Q’. Since the Two Exponents are equal, so
their coefficients are added and appended to the New List as shown in below
figure.
Then we move to the next term of ‘P’ and ‘Q’ lists and compare the Exponents.
Since, Exponents of both these terms are equal and after addition of their
coefficients we get 0. So, the term is dropped and no node is appended to the
New List as shown in below figure.

Moving to the next term of two lists ‘P’ and ‘Q’, we find that the corresponding
terms have the same exponents equal to 0. We add the coefficients and append
them to the New List for the resulting polynomial as shown in the below figure.

Final Output:

P(x) + Q(x) = 3x4 + 7x3 + 2

You might also like