Professional Documents
Culture Documents
Linked List (1)
Linked List (1)
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 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.
Support for other data structures: Some other Data Structures like
Stacks, Queues, Hash Tables, Graphs can be implemented using the
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).
Other Data Structures like Stacks, Hash Tables, Binary Trees can also be
constructed or programmed using a Doubly-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.
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.
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.
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.
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.
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: