Application of Linked ListIn this article, we will understand the linked list applications in detail. What do you mean by Linked list?A linked list is a linear data structure consisting of elements called nodes where each node is composed of two parts: an information part and a link part, also called the next pointer part. Linked list is used in a wide variety of applications such as
Polynomial ManipulationPolynomial manipulations are one of the most important applications of linked lists. Polynomials are an important part of mathematics not inherently supported as a data type by most languages. A polynomial is a collection of different terms, each comprising coefficients, and exponents. It can be represented using a linked list. This representation makes polynomial manipulation efficient. 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. Also, no two terms have the same exponent, and no term has a zero coefficient and without coefficients. The coefficient takes a value of 1. Each node of a linked list representing polynomial constitute three parts:
The structure of a node of a linked list that represents a polynomial is shown below: Consider a polynomial P(x) = 7x2 + 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, we have Observe that the number of nodes equals the number of terms in the polynomial. So we have 4 nodes. Moreover, the terms are stored to decrease exponents in the linked list. Such representation of polynomial using linked lists makes the operations like subtraction, addition, multiplication, etc., on polynomial very easy. 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. Let us consider an example an example to show how the addition of two polynomials is performed, 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: To generate a new linked list for the resulting polynomials that is formed on the addition of given polynomials P(x) and Q(x), we perform the following steps,
Example:C++ program to add two polynomials Explanation: In the above example, we have created an example of sum of two polynomial using array. Output: Below is the output of this example. Addition of long positive integer using linked listMost programming languages allow restrictions on the maximum value of integers stored. The maximum value of the largest integers is 32767, and the largest is 2147483647. Sometimes, applications such as security algorithms and cryptography require storing and manipulating integers of unlimited size. So in such a situation, it is desirable to use a linked list for storing and manipulating integers of arbitrary length. Adding long positive integers can be performed effectively using a circular linked list. As we know that when we add two long integers, the digits of the given numbers are individually traversed from right to left, and the corresponding digits of the two numbers along with a carry from prior digits sum are added. So to accomplish addition, we must need to know how the digits of a long integer are stored in a linked list. The digits of a long integer must be stored from right to left in a linked list so that the first node on the list contains the least significant digit, i.e., the rightmost digit, and the last node contains the most significant digit, i.e., leftmost digit. Example: An integer value 543467 can be represented using a linked list as For performing the addition of two long integers, the following steps need to be followed:
The first positive long integer 543467 is represented using a linked list whose first node is pointed by NUM1 pointer. Similarly, the second positive long integer 48315 is represented using the second linked list whose first node is pointed by NUM2 pointer. These two numbers are stored in the third linked list whose first node is pointed to by the RESULT pointer. Example:C++ program for addition of two polynomials using Linked Lists Explanation: In the above example, we have created an example of sum of two polynomial using linked list. Output: Below is the output of this example. Polynomial of Multiple VariablesWe 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 represnting this polynomial using linked list are: 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. ExampleSimple C++ program to multiply two polynomials Explanation: In the above example, we have created an example of multiple of two polynomial using arrays. Output: Below is the output of this example. Some other applications of linked list:
Next TopicTopological Sorting |