Transcript
Page 1: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Linked Lists

• Linked Lists Representation

• Traversing a Linked List

• Searching in a Linked List

• Insertion and Deletion in a Linked List

• Header Linked Lists

• Two – Way Lists

Page 2: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Introduction• It consists of a sequence of nodes, each containing

arbitrary data field and one or two reference (links) pointing to the next and/or previous nodes.

• Linked List consist of nodes . A node, the building block of a linked list, contains two parts:– A data element representing the information in the current position of the list.– A pointer/link/address to the next node in the list

• The START pointer points to the first node of the list.• The last node of Linked List has its Link portion null

indicating end of Linked List• No node in Linked List has its data portion empty

Page 3: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Introduction Cont….

Start Data Link

1 4 8 10

Representation in Memory• It requires two linear arrays as INFO and LINK

Start

Info Link1

2

3

4

5

6

5

2

4

0

A

H

G

Example 5.3, 5.4,5.5

Page 4: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Arrays and Linked Lists

• An array is a list store in contiguous memory.– Any element of an array can be accessed quickly.– Insertion and deletion in the middle of an array requires the

movement of many elements.– The size of an array is fixed.• A linked list is a list scattered throughout memory and

connected with pointers/Links of next element.– The elements of a linked list must be accessed in order.

Linear Access Mechanism– Insertion and deletion only requires re-assignment of a few

pointers.– The length of the list can change at any time, making it a

dynamic data structure.

Page 5: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Traversing a Linked List

Start Data Link

1 4 8 10

PTR

PTR:= LINK [PTR]

1. Set PTR := START2. Repeat Steps 3-4 while PTR ≠ NULL3. Apply process to INFO[PTR]4. Set PTR:= LINK[PTR] [end of loop]5. Exit

Page 6: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

• LIST => Link List, LOC => ITEM Location or LOC => NULL

Searching in a Linked List

Search (LINK, START, ITEM, LOC)1. Set PTR := START, LOC:= NULL2. Repeat Steps 3-4 while PTR ≠ NULL3. If INFO[PTR] = ITEM

Set LOC:=PTR [end of If Structure]4. Set PTR:= LINK[PTR]5. [end of loop]6. Return LOC

Page 7: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Insertion in a Linked List

1. Create a new Node and store data in that node.2. Find the correct position in the list.3. Assign the pointers to include the new node.

Algorithm: InsertFirst(START, ITEM)

1.Set New:= Create Node()2.Set INFO [New] := ITEM and LINK [New] := NULL3.Set START := New4.Exit

Page 8: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Insertion in a Linked List1. Create a new Node and store data in that node.2. Find the correct position in the list.3. Assign the pointers to include the new node.

Algorithm: InsertAtLoc( START, LOC, ITEM)1. If LOC=NULL then2. InsertFirst (START, ITEM)3.Else4.Set New:= Create Node()5.Set INFO [New] := ITEM and LINK [New] := NULL6.Set LINK [NEW] := LINK [LOC]7.Set LINK [LOC] := New

[End of if Structure]8.Exit

Page 9: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Insertion In Sorted Linked List• Steps:

1. Find the Location of the node2. Use Insertion method to insert into the Linked List

1. Find the Location of the node:1. Search the Item after which insertion needs to be

made.2. As the Linked List is sorted searching will result in the

location of element => Given ITEM 3. As Insertion can not be made before a Node therefore

another pointer will keep track of the previous node i.e. before moving the PTR , Save := PTR

4. PTR points to current node Element which is => ITEM while SAVE points to Element before PTR.

5. Insertion will be made after SAVE

Page 10: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Algorithm: FindLoc (START,ITEM,LOC)1. If START = NULL, then

– Set LOC := NULL and Return

2. Else if ITEM < INFO [START] [ITEM is not in List]– Set LOC := NULL and Return

[ End of if ]

3. Set SAVE := START and PTR := LINK [ START ]

4. Repeat Steps 3 - 4 while PTR ≠ NULL

5. If ITEM < INFO [ PTR ] then– Set LOC := SAVE and Return

6. Else– Set SAVE := PTR and PTR := LINK [ PTR ]

[ End of if ]

[ End of Step 4 loop ]

7. Set LOC := PTR and Return

“Find Location in a Sorted Linked List”

Page 11: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Algorithm: FindAndInsert (START, ITEM)

1. LOC:= FindLoc (START,ITEM,LOC)

2. Call InsetAtLoc (Start, ITEM,LOC)

3. Exit

Page 12: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Header Linked Lists

• A Header Linked List always Contains a Special Node called Header Node• It has Two Types:

a) Grounded Header List Last Node Contains the NULL Pointer

b) Circular Header List Last Node Points Back to the Header Node

Page 13: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Graphical Representations

Start Data Link

4 8 10

Grounded Header Link ListHeaderNode

Start Data Link

4 8 10

Circular Header Link ListHeaderNode

LINK [START] = NULL

LINK [START] = START

Page 14: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Header Linked Lists

• One way to simplify insertion and deletion is never to insert an item before the first or after the last item and never to delete the first node

• You can set a header node at the beginning of the list containing a value smaller than the smallest value in the data set

• You can set a trailer node at the end of the list containing a value larger than the largest value in the data set.

• These two nodes, header and trailer, serve merely to simplify the insertion and deletion algorithms and are not part of the actual list.

• The actual list is between these two nodes.

Page 15: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Doubly Linked Lists• A Doubly Linked List is List in which every

Node has a Next Pointer and a Back Pointer

• Every Node (Except the Last Node) Contains the Address of the Next Node, and Every Node (Except the First Node) Contains the Address of the Previous Node.

• A Doubly Linked List can be Traversed in Either Direction

Page 16: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Definition and Graphical Representations

Start

4

The Node is Divided into 3 parts1) Information Field2) Forward Link which Points to the Next Node3) Backward Link which Points to the Previous Node

The starting Address or the Address of First Node is Stored in START / FIRST Pointer Another Pointer can be used to traverse Doubly LL from End. This Pointer is Called END or LAST

X 2 10

Last

INFO Field

BACK Pointer

FORE Pointer

Page 17: Linked Lists Linked Lists Representation Traversing a Linked List Searching in a Linked List Insertion and Deletion in a Linked List Header Linked Lists

Reading Assignments

• Complexity in Traversing a Linked List • Complexity in Searching a Linked List• Complexity in Sorted Linked List• Garbage Collection in Linked List• Overflow and Underflow in Linked Lists• Complexities of Insertion in a Linked List


Recommended