Linked List, Types of Linked LIst, Various Operations, Applications of Linked List
Linked list
-
Upload
bhargav0077 -
Category
Education
-
view
171 -
download
0
Transcript of 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
Singly Linked Lists and ChainsSingly Linked Lists and Chains
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
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
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
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
7Singly Linked Lists and Chains
• To insert GAT between FAT and HAT
8Singly Linked Lists and Chains
• To delete GAT from the list
temp
Representing Chains in CRepresenting Chains in C
10Representing Chains in C
• To make linked representations possible, we need …
– Example 4.2~4.5 [Two-node linked list] …
11Example 4.2 [Two-node linked list]
• To create a linked list with two nodes 10 & 20
12Example 4.3 [List insertion]
• To insert a node with a data field of 50 after some arbitrary node x
13Example 4.4 [List deletion]
• To delete an arbitrary node from a list
14Example 4.5 [Printing out a list]
• To print data fields of the nodes in a list
Linked Stacks and QueuesLinked Stacks and Queues
16Linked Stacks and Queues
17Linked Stacks and Queues
• To insert or delete a node from the top of the stack …
18
• To add a node to the rear of the queue and delete a node at the front …
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
PolynomialsPolynomialsPolynomial Representation
Adding Polynomials Erasing Polynomials
Circular List Representation of PolynomialsSummary
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
22Adding Polynomials
• To add two polynomials
23Erasing Polynomials
• To erase a polynomial
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
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
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
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
Additional List OperationsAdditional List OperationsOperations for Chains
Operations for Circularly Linked Lists
29Operations for Chains
• To invert (or reverse) a chain
30Operations for Chains
• To concatenate two chains
Sparse MatricesSparse MatricesSparse Matrix Representation
Sparse Matrix lnputSparse Matrix Output
Erasing a Sparse Matrix
32Sparse Matrix Representation
33
Doubly Linked ListsDoubly Linked Lists
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
36Doubly Linked Lists
• A doubly linked list may or may not be circular
37Doubly Linked Lists
• To insert a node
• To delete a node
38Doubly Linked Lists
Q&AQ&A