Linked list

23
PRESENTAION ON LINKED LIST

Transcript of Linked list

Page 1: Linked list

PRESENTAION ON LINKED LIST

Page 2: Linked list

LINKED LIST

DEFINITIONARRAY Vs LINKED LISTTYPESOPERATIONS

Page 3: Linked list

Introduction Linear data structure Dynamic data structure A linked list is a series of

connected nodesEach node contains at least

◦A piece of data (any type)◦Pointer to the next node in the list

Info linkNODE

:

Data item

Pointer

Page 4: Linked list

REPRESENTATION OF A LINKED LIST WITH 3 NODES Start

Data item Data item Data item X

Head: pointer to the first nodeThe last node points to NULL

Page 5: Linked list

REPRESENTATION OF AN EMPTY LINKED LIST

X

START

Page 6: Linked list

VARIATIONS OF LINKED LIST

Header linked listCircular linked listTwo-way list(doubly linked list)

Page 7: Linked list

Variations of Linked Listsheader linked lists

◦A linked list with a special node called header node in the beginning of the list

◦Header node doesn’t contain the actual data.

XA

Head header node

B C

Page 8: Linked list

Variations of Linked Lists

Circular linked lists◦The last node points to the first node of the

list

◦How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)

A

Head

B C

Page 9: Linked list

Variations of Linked Lists

Doubly linked lists◦Each node points to not only successor but the

predecessor◦There are two NULL: at the first and last nodes in

the list◦Advantage: given a node, it is easy to visit its

predecessor. Convenient to traverse lists backwards

A

Head

B C

Back info forw

Tail

Page 10: Linked list

Operations on singly linked listCreationTraversing(visiting)CountingInsertionDeletionSearchingMerging(concatenating two lists)Reversing the listUpdatingCopying

Page 11: Linked list

Algorithm to traverse and count one-way list

Set PTR=HEAD.

• Apply process to INFO[PTR].• Set COUNT=COUNT+1• Set PTR=LINK[PTR].

Set count =0.

Repeat step while PTR!=NULL

Write : COUNT

EXIT

Page 12: Linked list

Algorithm to search an element ITEM in an unsorted one-way list

Set HEAD=NULL, then

• Write: “list is empty”• Set LOC= NULL• And exit

Set PTR=HEAD

• If ITEM=INFO[PTR]• Set LOC=PTR• And exit• Else

• Set PTR=LINK[PTR]

Repeat while PTR=!NULL

Page 13: Linked list

Set LOC= NULL

Write:”search is unsuccessful”

Exit

Page 14: Linked list

Algorithm to search an element ITEM in an unsorted one-way list

Set HEAD=NULL, then

• Write: “list is empty”• Set LOC= NULL• And exit

Set PTR=HEAD

• If ITEM=INFO[PTR]• Set LOC=PTR• Write:”search is successful”• And exit

• Else if ITEM>INFO[PTR]• Set PTR=LINK[PTR]

• Else[ITEM<INFO[PTR]]• Set LOC=NULL• Write:”search is unsuccessful”• And exit

Repeat while PTR=!NULL

Page 15: Linked list

Set LOC= NULL

Write:”search is unsuccessful”

Exit

Page 16: Linked list

Algorithm to insert an element ITEM at the begnning one-way list

If AVAIL=NULL, then

• Write:”overflow”• And exit

Set NEW=AVAIL

• Set AVAIL=LINK[AVAIL]

Set INFO[NEW]=ITEM

• Set HEAD= NEW

Set LINK[NEW]=HEAD

EXIT

Page 17: Linked list

Algorithm to insert an node after a node with location LOC in one-way list

If AVAIL=NULL, then

• Write:”overflow”• And exit

Set NEW=AVAIL

• Set AVAIL=LINK[AVAIL]

Set INFO[NEW]=ITEM

• Set LINK[NEW]=HEAD• Set HEAD =NEW

If LOC =NULL, then

Page 18: Linked list

ELSE

• Set LINK[NEW]=LINK[LOC]• Set LINK[LOC]=NEW

Exit

Page 19: Linked list

Algorithm to delete first node of one-way listIf HEAD=NULL, then

• Write:”underflow”• And exit

Set NEW=HEAD

• Set HEAD=LINK[HEAD]

Set LINK[NEW]=AVAIL

• Set AVAIL=NEW

EXIT

Page 20: Linked list

Algorithm to delete a node with location LOC from one-way list

If HEAD=NULL, then

• Write:”underflow”• And exit

If LOC=NULL, THEN

• Write:”node not found in the list• And exit

If LOCP=NULL, then

• Set HEAD=LINK[HEAD]

Else

• Set LINK[LOCP]=LINK[LOC]

Page 21: Linked list

Set LINK[LOC]=AVAIL

• Set AVAIL=LOC

exit

Page 22: Linked list

ARRAY VERSUS LINKED LISTS

Linked lists are more complex to code and manage than arrays, but they have some distinct advantages.◦ Dynamic: a linked list can easily grow and shrink in

size. We don’t need to know how many nodes will be in the

list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at

compilation time.

◦ Easy and fast insertions and deletions In array, Insertion and deleltion are more time

consuming as large no. of elements need to be shifted to make space for inserting a new element or to cover the space created by deleting an existing element.

With a linked list, no need to move other nodes. Only need to reset some pointers.

Page 23: Linked list

ANY QUERY