Linked List
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.
Traversal of nodes can be done in the forward direction only due to the linking
of every node to its next node.
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.
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.
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 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.
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.
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.
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 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.
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.
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
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).
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.
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.
Addition of Polynomials:
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,
Q (x) = 5x3 + 4 x2 - 5
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: