160 likes | 187 Views
Explore the utility of linked lists in creating polynomials, sorting, tree construction, and multilist organization. Understand algorithms for polynomial creations, additions, and multiplications. Dive into Radix Sort for efficient number sorting.
E N D
Application of linked list Geletaw S.
Linked list can be used in various environment. Some of the common examples are • Creation of a polynomial • Polynomial manipulation • Sorting • Creation of a tree • Tree Traversals • Multilist Organization
Creation of a Polynomial • A node is used to create a list which consist of various components of the polynomial • Example of a polynomial 4x^3 + 3x^2 + 2x + 1 Node1 Node2 Node3 Node4
The List contain node that has information about the polynomial degrees and coefficients Example4x^3 Co-eff Power PointerField 4 3 .
Similarly for the polynomial 4x^3 + 3x^2 + 2x + 1, the linked list created is as follows Structure used to create a node: Struct node { int power; int coeff; struct node * next; // to point the next node in the list } 4 3 3 2 2 1 1 0
Procedure to create a polynomial Void create( ) { int s,c1; Struct node * p1, *n; root = p1 = NULL; cout<<“ enter the higher degree”; cin>>deg; s = sizeof ( struct node *); for (I = deg; i>+0;I - - ) { cout<<“co-efficient :”; cin>> c1; n=(struct node *) new(s); n -> coeff = c1; n -> pow = I; n -> next = NULL; if (root = = NULL) root = n; else p1 -> next = n; p1=n; } }
Algorithm to create a polynomial • The highest degree of the polynomial is obtained first • Based on the degree the node is created • The co-efficient and the corresponding power value is stored in the node • The NEXT pointer is updated accordingly
Type declaration for array implementation of the polynomial ADT Typedefstruct { intcoeffarray[ Maxdegree + 1 ]; intHighpower; } *Polynomial; Procedure to initialize a polynomial to zero Void zeroPolynomial( Polynomial Poly ) { int i; for( i=0; i<=Maxdegree; i++ ) Poly -> CoeffArray[ i ] = o Poly -> HighPower = o; }
Procedure to add two Polynomials Void AddPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomail PolySum) { int i; zeroPolynomial( PolySum ); Polysum -> HighPower = Max( Poly1 -> HighPower,Poly2 -> Highpower); for( i= PolySum -> HighPower; i>=0; i++) PolySum ->CoeffArray[ i ] = Poly1 -> coeffArray [ i ] + Poly2 ->coeffArray[ i ]; }
PolySum Poly 1 4x^3 + 3x^2 + 2x + 1 Poly 2 5x^2 + 6x + 3 ( + ) ----------------------------------- PolySum 4x^3 + 8X^2 + 8x + 4
Procedure to Multiply 2 Polynomials Void MultPolynomial( const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd) { int i,j; zeroPolynomial( PolyProd ); PolyProd -> HighPower = Poly1 -> highPower + Poly2 ->HighPower; if( PolyProd -> HighPower > MaxDegree) error( “ Exceeded array Size”); else for( i=0; i<= Poly1 -> highPower; i++) for( j=0; j<= Poly2 -> HighPower; J++) PolyProd -> coeffArray [ i+j ] = Poly1 -> coeffArray [i] * Poly2 -> coeffArray [j]; }
PolyProd Poly 1 4x^3 + 3x^2 + 2x + 1 Poly 2 5x^2 + 6x + 3 ( * ) ----------------------------------- 20x^5 + 24x^4 + 12x^3 + 15x^4 + 18x^3 + 6x^2 + 10x^3 + 12x^2 + 6x + 5x^2 + 6x + 3 PolyProd 20x^5 + 39x^4 + 40x^3 + 23x^2 + 12x + 3
Radix Sort • It is performed using buckets from 0 to 9 • The digit position of a number is used to perform sorting • The number of passes in a radix sort depends on the number of digits in the given number • It is also called as Card Sort
Example 42, 24, 11, 36, 54, 10, 19, 27, 45. The number of digits n the numbers are 2, hence the number of passes are 2 Pass 1
The number has been placed in their corresponding pockets based on their unit digit. 10 is placed in pocket 0 27 is placed in pocket 7 Collect back all the nos from the pockets 10,11,42,32,24,54,45,36,27,19
Now place all the nos in the pockets based on tenth digit position. 10 is placed in pocket 1 27 is placed in pocket 2