Linked List Data Structure & ADT

Embed Size (px)

Citation preview

Linked List Data Structure & ADTAADS-9

Array storage for Lists An array is an intuitive suggestion for storing a list(ordered set) In low level languages, an array is a derived data type (aggregate data type) that gets a set of consecutive memory locations allotted

Array Storage & Sequential Allocations This means array is allotted sequential locations to hold the array elements If the application has to maintain the array filled from top every time a modification by way of add or delete is done, elements will have to be shifted Either to reclaim the empty space occurred due to deletion or To create a vacant space for insertion of a new element

IllustrationConsider an Array storage for LIST as follows

Complexity Analysis We find that if the List is maintained full from TOP then adding to the TAIL is trivial(constant timeO(1)) but adding at the TOP needs N shifts-O(N) Deleting from the TAIL is simple(O(1)) but deletion from TOP needs N-1 shifts-(O(N)) Now, on an average, there could be N/2 shifts both for ADD and DELETE if the List has to maintain its compact nature and sequential order, as seen next

Average case situation There could be 1 shift, 2 shifts, and finally N-1 or N shifts could be there depend on the location Hence, total number of shifts=1+2+3+ +N =N(N+1)/2 Total possible cases =N So average case number of shifts=N(N+1)/2N =N/2 Complexity of modification changes from O(1) to O(N/2) to O(N)

Linked Storage If we can allocate storage for elements in a linked manner in independent units form, then the modification operations could be taking only O(1) time for both ADD and DELETE Because, we can readjust the links in the instance of ADD or DELETE operations rather than shifting the elements per se This gives rise to the type/class of Linked List Data Structures For Linked List ADD or DELETE will be O(1)

Linked List Data Structure Linked List is a linear Data Structure provided by the Linear Referencing Technique incorporated into the elements in the List One Element in a Linked List DS is called a Node Linked List is like a chain, with the links provided by the references to the neighbourhood Links have no other purpose than the role as reference to the other Nodes

Structure of Node in a Linked List Each Node in the Linked List shall have Data Fields and Reference Fields One Node may have only one Reference or else two References

DATA FIELD

REFERENCE FIELD

REFERENCE FIELD

DATA FIELD

REFERENCE FIELD

D

R

R

D

R

Example- Node in C with single Referencetypedef struct node { int data; // will store information node *next; // the reference to the next node };

Discussion on the Example Here, the Node contains only one Reference The Reference Field can hold a NULL value or a valid Pointer to the Next Node in the line of all Nodes. The resulting Linked List is called a Singly Linked ListSample Code

Illustration A Singly Linked List with N nodes can be schematically shown as followsHEADER NODE 1 D 2 NODE 2 D 3 NODE 3 D 4

D

N

D

NULL

NODE N-1

NODE N

Doubly Linked List A Doubly Linked List shall be made of Nodes with two Reference Fields On Reference shall point to the previous Node while other shall point to the next Node

Illustration Figure below shows the scheme of a Doubly Linked List with N NodesHEADER NODE 1 NULL D 2 1 NODE 2 D 3

N-2

D NODE N-1

N

N-1

D NODE N

NULL

C Code for Doubly Linked Listtypedef struct DLnode {int data; // will store information DLnode *previous; // the reference to the previous node DLnode *next; // the reference to the next node

};

Circularly Linked List Circularly Linked List could be a Singly Linked List or Doubly Linked List, with a difference that the First Node carries reference to the Last Node (instead of NULL) and the Last Node has Reference to the First Node (instead of NULL) Last Node will have Node 1 as next node and Node 1 will have Nth node as the previous node

Circularly Linked Singly LinkedTo NODE 1 HEADER NODE 1 D 2 NODE 2 D 3 NODE 3 D 4

D

N

D

1

NODE N-1

NODE N

Illustration A Circularly Linked Doubly Linked List DS is shown belowTo NODE 1 HEADER NODE 1 N D 2 1 NODE 2 D 3

N-2

D NODE N-1

N

N-1

D NODE N

1

Example Given 5 Nodes with address as shown below Each Node contains Name and Age as the data Organize in Alphabetical order of names as1. 2. 3. 4. Singly Linked List Doubly Linked List Circularly Linked Singly Linked List Circularly Linked Doubly Linked List

A3051 Aravind 45 *Next *Previous

A3091 Bhasker 38 *Next *Previous

A4085 Collins 42 *Next *Previous

A4088 Ravinder 50 *Next *Previous

A5095 Sharma 48 *Next *Previous

Names in the order1. 2. 3. 4. 5. Aravind Bhasker Collins Ravinder Sharma

HEADER

Singly Linked List

FF01

A3051

A3051 Aravind 45 A3091

A3091 Bhasker 38 A4085

A4085 Collins 42 A4088

A4088 Ravinder 50 A5095

A5095 Sharma 48 NULL

Doubly Linked ListHEADER

FF01 A3051 Aravind 45 A3091 NULL A4088 Ravinder 50 A5095 A4085 A3091 Bhasker 38 A4085 A3051

A3051 A5095

A4085 Collins 42 A4088 A3091

A5095 Sharma 48 NULL A4088

Circularly Linked Singly Linked ListHEADER

FF01 A3051 Aravind 45 A3091 A3091 Bhasker 38 A4085

A3051 A5095

A4085 Collins 42 A4088

A4088 Ravinder 50 A5095

A5095 Sharma 48 A3051

Circularly Linked Doubly Linked ListHEADER

FF01 A3051 Aravind 45 A3091 A5095 A4088 Ravinder 50 A5095 A4085 A3091 Bhasker 38 A4085 A3051 A5095 Sharma 48 A3051 A4088

A3051 A5095

A4085 Collins 42 A4088 A3091

HEADER

FF01

Linked ListA3091 Bhasker 38 *Next *Previous A4088 Ravinder 50 *Next *Previous A4085 Collins 42 *Next *Previous

A3051 Aravind 45 *Next *Previous

A5095 Sharma 48 *Next *Previous

Summary Linked lists are a way to store data with structures/records so that the system can automatically create a new place to store data whenever necessary Linked List is a dynamic linear data structure Useful for algorithms where data is huge with an unpredictable volume the order of listing needs to be maintained and the modification operations are very frequent

Exercise 1 Given a Polynomial as follows Write a Singly Linked List structure for storing the polynomial Each Node will have two data fields, namely Coefficient Power

f ( x) ! 25 x 30 x 20 x 40 x 65

5

3

2

Solution-Step 1 Define the structure of the Node with single Reference

ADDRESS Power CFNT *Next

Step 2 Generate the Nodes/F4010 5 25 *Next F4185 3 30 *Next F2088 2 20 *Next F3566 1 40 *Next

F 4838 0 65 *Next

Step 3 Link the Nodes to form the List/Singly LinkedF4010 5 25 F4185 F4185 3 30 F2088 F2088 2 20 F3566 F3566 1 40 F4838

F 4838 0 65 NULL

Exercise 2 Develop a scheme for adding two polynomials if the polynomials are stored in Linked List forms Polynomials are as follows\

A p 25 x 30 x 20 x 40 x 65 B p 20 x 30 x 10 x 4 x 254 3 2

5

3

2

Solution The solution shall be a Polynomial with 5th order (highest order among the two) Even though the input polynomials have only 5 terms, there shall be 6 terms for the resultant polynomial Let C be the resultant polynomial Then C=A+B Power of C=Max(Power of A, Power of B) Coefficient of C=(Coefficient of A+ Coefficient of B) while (Power of A= Power of B) else Coefficient of A OR Coefficient of B

Generate Polynomial A Polynomial A is as follows\F4010 5 25 F4185 F4185 3 30 F2088 F2088 2 20 F3566 F3566 1 40 F4838

F 4838 0 65 NULL

Generate Polynomial B

D5520 4 20 F4185

D5585 3 30 F2088

D5088 2 10 F3566

D5566 1 4 F4838

D 5638 0 25 NULL

Initialize C This step will create a C polynomial as a NULL polynomial represented by the Header NodeADDRESS

*NextHEADER

Generate the first Node of C First create a Node for C and then assign the values to the data fieldsF1000 E1010 Power CFNT *Next

E1010

HEADER

Algorithm to calculate the values Read AnPower Read BnPower If AnPower= BnPower, CnPower= AnPower & CnCFNT= AnCFNT+ BnCFNT Else If AnPower{ BnPower, (1)CnPower= Max(AnPower, BnPower) CnCFNT= AnCFNT if AnPower> BnPower Else CnCFNT= BnCFNT if BnPower> AnPower

Algorithm-contd (2)CnPower= Min(AnPower, BnPower) CnCFNT= AnCFNT if AnPower< BnPower Else CnCFNT= BnCFNT if BnPower< AnPower

Finally we get the resultant as followsF1000 E1010 5 25 E1020 E1040 2 30 E1080 E1020 4 20 E1050 E1080 1 44 E1060 E1050 3 60 E1040 E1060 0 90 NULL

E1010

HEADER

Advantages of Linked List DS In Linked List DS, the storage space can dynamically expand or shrink depend on the requirement No location shall be remaining vacant unlike the static array allocation Traversal through the list can be efficiently programmed Modification operations are of constant time complexity That is of O(1) order Useful where the storage requirement is unpredictable

LINKED LIST as ADT We can formulate one Abstract Data Type of the LINKED LIST with the Data Structure Linked List as the basic storage structure in an Algorithm Design Accordingly we can have Singly Linked List ADT Doubly Linked List ADT Circularly Linked List ADT-Singly or Doubly

Procedures in LINKED LIST as ADT Each of the ADT shall have the Procedures for1. 2. 3. 4. Initializing the ADT Modifying Operations on the ADT Querying Operations on the ADT Doing operations for some special purposes in the Algorithm 5. Memory Scavenging Operations(if needed)

DELETE IN Doubly Linked LIST LIST DELETE(L,x) if prev[x]{ NULL then next[prev[x]]nnext[x] else head[L]nnext[x] if next[x] { NULL then prev[next[x]] nprev[x]

IllustrationF1000 F0900 D NODE K-1 F1010 F1000 F1010 D NODE K F1050 F1010 F1050 D F1100

NODE K+1

SolutionF1000 F0900 D NODE K-1 F1050 F1000 F1010 D NODE K F1050 F1000 F1050 D F1100

NODE K+1

NODE K gets deleted from the Linked List

Complexity of DELETE Irrespective of the location of the Node in the List, the operations remain the same and there is no shifting of elements So the time complexity of DELETE is O(1) Or else, the DELETE operation is of constant time operation always

Delete If we want to delete a Node with a given key, we must first call LIST_SEARCH to retrieve a pointer to the Node

Add Similar to DELETE we can have the procedure for ADD as well

Linked List as Fundamental Data Structure in other ADTs Linked List is one of the fundamental data structure like array So, it can be used for implementing the ADT like STACK,QUEUE, PRIORITY QUEUE etc, where the underlying data storage shall be managed by the Linked List scheme

Exercise In a Language, it is required to introduce the constructsC=A+B D=A-B Where A,B and C are polynomials of arbitrary order

Write the scheme and develop into an Algorithm using ADTs Call this ADT as POLYNOMIAL, with + and as overloaded operators for addition and subtraction respectively

Polynomial ADT It will have a Data Structure-(Linked list) It will have procedures/methods/functions for Initializing the polynomial object Taking inputs from the User Adding a Node(Term) and inserting the data in the data fields of the Node Adding subsequent Nodes and Linking the Nodes Displaying the Polynomial in a User friendly manner Removing the Polynomial object from memory when needed

Clues for the solution Define a Polynomial ADT using Singly Linked List for storing the Polynomials of arbitrary Order Create two polynomials A and B

Procedure for + Initialize a third Polynomial C which is NULL Scan through A & B and add the A & B polynomials term wise as shown earlier and transfer each result to the C polynomial accordingly term wise, adding Nodes in the C one by one

Procedure for Similar to ADD, but the difference of the coefficients of A & B are taken for the like terms in A & B

Note While the Polynomial ADT is used to create the Polynomial Objects The + and are the operations on these Objects and hence the operators are overloaded with the procedure to Add or Subtract the Polynomials

+ Procedure Procedure Add(A,B)Read AnPower Read BnPower If AnPower= BnPower, CnPower= AnPower & CnCFNT= AnCFNT+ BnCFNT Else If AnPower{ BnPower, (1)CnPower= Max(AnPower, BnPower) CnCFNT= AnCFNT if AnPower> BnPower Else CnCFNT= BnCFNT if BnPower> AnPower

+ Procedure -contd(2)CnPower= Min(AnPower, BnPower) CnCFNT= AnCFNT if AnPower< BnPower Else CnCFNT= BnCFNT if BnPower< AnPower

-Procedure Procedure Subtract(A,B) Read AnPower Read BnPower If AnPower= BnPower, CnPower= AnPower & CnCFNT= AnCFNT- BnCFNT Else If AnPower{ BnPower, (1)CnPower= Max(AnPower, BnPower) CnCFNT= AnCFNT if AnPower> BnPower Else CnCFNT= -(BnCFNT) if BnPower> AnPower

-Procedure -Contd(2)CnPower= Min(AnPower, BnPower) CnCFNT= AnCFNT if AnPower< BnPower Else CnCFNT= -(BnCFNT) if BnPower< AnPower

Write a Procedure for multiplying(Convolving) two polynomials A=ax+b B=cx+d Convolve(A,B)=acx2 +(bc+ad)x+bd

Exercise Create a Search sublist in the search operation required in a Mobile Contact List search function so that the sublist can be scanned in a circular fashion and also to use any contact number for making a call(only that function available!!) Assume that that the location of each Contact Record is accessible directly by searching the name using the starting letters as keys

Linking Shape Objects

Linking Shape Objects