Linked list

39
Chapter 4 Linked Lists Chapter 4 Chapter 4 Linked Lists Linked Lists Singly Linked Lists and Chains Representing Chains in C Linked Stacks and Queues Polynomials Additional List Operations Equivalence Classes Sparse Matrices Doubly Linked Lists X

Transcript of Linked list

Page 1: Linked list

Chapter 4Linked Lists Chapter 4Chapter 4

Linked Lists Linked Lists Singly Linked Lists and Chains

Representing Chains in CLinked Stacks and Queues

PolynomialsAdditional List Operations

Equivalence ClassesSparse Matrices

Doubly Linked Lists

X

Page 2: Linked list

Singly Linked Lists and ChainsSingly Linked Lists and Chains

Page 3: Linked list

3Singly Linked Lists and Chains

• An array and a sequential mapping is used to represent simple data structures in the previous chapters

• This representation has the property that successive nodes of the data object are stored a fixed distance apart(1) If the element aij is stored at location Lij, then aij+1 is at the

location Lij+1

(2) If the i-th element in a queue is at location Li, then the (i+1)-th element is at location Li+1 % n for the circular representation

(3) If the topmost node of a stack is at location LT , then the node beneath it is at location LT-1, and so on

• When a sequential mapping is used for ordered lists, operations such as insertion and deletion of arbitrary elements become expensive

Page 4: Linked list

4Singly Linked Lists and Chains

• In the sequential representation, if we want to add the word GAT, …

• An elegant solution to the problem of data movement in sequential representations is achieved by using linked representations

• Sequential vs. linked representations– Successive items of an ordered list are located a fixed

distance apart vs. these items may be placed anywhere in memory

– The order of elements is the same as in the ordered list vs. these two sequences need not be the same

Page 5: Linked list

5Singly Linked Lists and Chains

• In a linked representation– To access list elements in the correct order, with each

element we store the address or location of the next element in the list

– A linked list is comprised of nodes; each node has zero or more data fields and one or more link or pointer fields

Page 6: Linked list

6Singly Linked Lists and Chains

– The nodes do not actually reside in sequential locations– The actual locations of nodes are immaterial– In a singly linked list, each node has exactly one

pointer field– A chain is a singly linked list that is comprised of zero

or more nodes– When the # of nodes is zero, the chain is empty

Page 7: Linked list

7Singly Linked Lists and Chains

• To insert GAT between FAT and HAT

Page 8: Linked list

8Singly Linked Lists and Chains

• To delete GAT from the list

temp

Page 9: Linked list

Representing Chains in CRepresenting Chains in C

Page 10: Linked list

10Representing Chains in C

• To make linked representations possible, we need …

– Example 4.2~4.5 [Two-node linked list] …

Page 11: Linked list

11Example 4.2 [Two-node linked list]

• To create a linked list with two nodes 10 & 20

Page 12: Linked list

12Example 4.3 [List insertion]

• To insert a node with a data field of 50 after some arbitrary node x

Page 13: Linked list

13Example 4.4 [List deletion]

• To delete an arbitrary node from a list

Page 14: Linked list

14Example 4.5 [Printing out a list]

• To print data fields of the nodes in a list

Page 15: Linked list

Linked Stacks and QueuesLinked Stacks and Queues

Page 16: Linked list

16Linked Stacks and Queues

Page 17: Linked list

17Linked Stacks and Queues

• To insert or delete a node from the top of the stack …

Page 18: Linked list

18

• To add a node to the rear of the queue and delete a node at the front …

Page 19: Linked list

19Linked Stacks and Queues

• Advantages– Computationally & conceptually simple– No longer need to shift elements to make space– Computation can proceed as long as there is memory

available• Disadvantages

– Need additional space for the link field. This overhead can be overridden by …(1) the ability to represent lists in a simple way, and (2) the reduced computing time required by linked representations

Page 20: Linked list

PolynomialsPolynomialsPolynomial Representation

Adding Polynomials Erasing Polynomials

Circular List Representation of PolynomialsSummary

Page 21: Linked list

21Polynomial Representation

• To represent the polynomial– Each term can be represented as a node containing

coefficient and exponent fields, as well as a pointer to the next term

Page 22: Linked list

22Adding Polynomials

• To add two polynomials

Page 23: Linked list

23Erasing Polynomials

• To erase a polynomial

Page 24: Linked list

24Circular List Representation of Polynomials• We can free all nodes of a polynomial more

efficiently …– Circular List: the link field of the last node points to

the first node in the list– Chain: a singly linked list in which the last node has a

null link

Page 25: Linked list

25Circular List Representation of Polynomials• An efficient erase algorithm for circular lists, by

maintaining an avail list (as a chain) of nodes• When we need a new node, we examine the avail

list. If the list is not empty, then we may use one of its nodes. Only when the list is empty do we need to use malloc to create a new node

Page 26: Linked list

26Circular List Representation of Polynomials• We may erase a circular list in a fixed amount of

time independent of the # of nodes in the list

Page 27: Linked list

27Circular List Representation of Polynomials• When we implement the other polynomial

operations since we must handle the zero polynomial as a special case

• To avoid this special case, we introduce a header node into each polynomial

Page 28: Linked list

Additional List OperationsAdditional List OperationsOperations for Chains

Operations for Circularly Linked Lists

Page 29: Linked list

29Operations for Chains

• To invert (or reverse) a chain

Page 30: Linked list

30Operations for Chains

• To concatenate two chains

Page 31: Linked list

Sparse MatricesSparse MatricesSparse Matrix Representation

Sparse Matrix lnputSparse Matrix Output

Erasing a Sparse Matrix

Page 32: Linked list

32Sparse Matrix Representation

Page 33: Linked list

33

Page 34: Linked list

Doubly Linked ListsDoubly Linked Lists

Page 35: Linked list

35Doubly Linked Lists

• One difficulty with singly linked lists: if we are pointing to a specific node, say p, then we can move only in the direction of the links

• The same problem arises when one wishes to delete an arbitrary node from a singly linked list

• Doubly linked lists: each node has two link fields, one linking in the forward direction and the other linking in the backward direction

Page 36: Linked list

36Doubly Linked Lists

• A doubly linked list may or may not be circular

Page 37: Linked list

37Doubly Linked Lists

• To insert a node

• To delete a node

Page 38: Linked list

38Doubly Linked Lists

Page 39: Linked list

Q&AQ&A