linked list - dynamic implementation mr. mubashir ali

36
Linked List - Dynamic Implementation Mr. Mubashir Ali Lecturer (Dept. of Software Engineering) [email protected] Lahore Garrison University, Lahore 1 Lecture 06

Upload: others

Post on 22-Apr-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked List - Dynamic Implementation Mr. Mubashir Ali

Linked List - Dynamic Implementation

Mr. Mubashir AliLecturer (Dept. of Software Engineering)

[email protected] Garrison University, Lahore

1

Lecture 06

Page 2: Linked List - Dynamic Implementation Mr. Mubashir Ali

Previous Lecture

Dynamic Link List

Dynamic List Operations

AddToHead(x)

AddToTail(x)

AddToPosition(x)

Link List Implementation

Lab-3

Mubashir Ali - Lecturer (Department of Software Engineering)

2

Page 3: Linked List - Dynamic Implementation Mr. Mubashir Ali

Outline

1. DeleteFromHead(x)2. DeleteFromTail(x)3. Delete(x)4. Implementation

a) Delete a Particular Nodeb) Update Node Valuec) Search Elementd) Display Linked Liste) Sort Link Listf) Reverse Linked List

5. Application of Linked List6. Lab-3

Mubashir Ali - Lecturer (Department of Software Engineering)

3

Page 4: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromHead()

• Two cases must be considered to delete thenode from the head of a linked list.

1. Either a linked list has only one node or

1. A linked list has more than one nodes.

Mubashir Ali - Lecturer (Department of Software Engineering)

4

Page 5: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromHead()

• First Case: Linked List has only one node:

• We follow the following steps to handle this case:

1. Check the values of the pointer variables head andtail.

1. If both points to the same node, means linkedlist has only one node.

Set both head and tail pointers to null and return

back the memory occupied by the node to the system.

Mubashir Ali - Lecturer (Department of Software Engineering)

5

7 /

head tail

Page 6: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromHead()

• Second Case: Linked List has more than one nodes

• We follow the following steps to handle this case:

1. Check the values of the pointer variables head and tail.

If both points to the different nodes, means linked list has more than one nodes.

Mubashir Ali - Lecturer (Department of Software Engineering)

6

9 \7

head tail

8

Page 7: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromHead()

2. Set a temporary pointer variable tmp points to thehead of a linked list.

3. Since we delete a node from the head of a linked list,therefore move the head pointer to the node next tothe head of an existing list.

Mubashir Ali - Lecturer (Department of Software Engineering)

7

9 \7

head tail

8

9 \7

headtail

8

tmp

Page 8: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromHead()

4. Delete a node pointed by the pointer variable tmpfrom a linked list.

Mubashir Ali - Lecturer (Department of Software Engineering)

8

9 \7

headtail

8

tmp

9 \7

headtail

Page 9: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

• Two cases must be considered to delete the node from the end of a linked list.

1. Either a linked list has only one node or

1. A linked list has more than one nodes.

The first case is exactly similar as we discussed in DeleteFromHead() function.

Mubashir Ali - Lecturer (Department of Software Engineering)

9

Page 10: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

• Second Case: Linked List has more than one nodes:

• We follow the following steps to handle this case:

1. Check the values of the pointer variables head and tail.

If both points to the different nodes, means linked list has more than one nodes.

Mubashir Ali - Lecturer (Department of Software Engineering)

10

9 \7

head tail

8 45

Page 11: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

2. After removing a node, tail should refer to thenew tail of the list. i.e. tail has to be movedbackward by one node.

But moving backward is impossible because there isno direct link from the last node to itspredecessor.

Mubashir Ali - Lecturer (Department of Software Engineering)

11

9 \7

head tail

8 45

9 \7

headtail

8 45

Page 12: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

3. Hence, this predecessor has to be found bysearching from the beginning of the list andstopping right before tail.

This is accomplished with a temporary variable tmpused to scan a list within the loop.

4. The variable tmp is initialized to the head of thelist.

Mubashir Ali - Lecturer (Department of Software Engineering)

12

9 \7

head tail

8 45

tmp

Page 13: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

5. In each iteration of the loop, the variable tmp is advanced to the next node of the linked list.

After executing the assignment tmp= tmp->next, tmp

refers to the second node.

Mubashir Ali - Lecturer (Department of Software Engineering)

13

9 \7

head tail

8 45

tmp

9 \7

head tail

8 45

tmp

Page 14: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

After the second iteration and executing the sameassignment, tmp refers to the third node.

After the third iteration and executing the same

assignment, tmp refers to the forth node.

After the third iteration and executing the same

assignment, tmp refers to the forth node.

Mubashir Ali - Lecturer (Department of Software Engineering)

14

9 \7

head tail

8 45

tmp

Page 15: Linked List - Dynamic Implementation Mr. Mubashir Ali

DeleteFromTail()

6. Now delete the last node from the linked list.

7. Because tail is now pointing to non existing node

therefore it is immediately set to point to the node

pointed by tmp.

8. To mark the fact that tail is now pointing to last

node of the linked list, the next member of this node

is set to null.Mubashir Ali - Lecturer (Department of

Software Engineering)15

9 \7

head tail

8 45

tmp

7

head tail

8 4 /5

tmp

Page 16: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

• Two cases must be considered to delete the node of value x from the linked list.

1. Either a linked list has only one node or

1. A linked list has more than one nodes.

The first case is exactly similar as we discussed in DeleteFromHead() function except

We first checks that the value of this single node matches with the value of variable x.

Mubashir Ali - Lecturer (Department of Software Engineering)

16

Page 17: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

Second Case: Linked List has more than one nodes:

We follow the following steps to handle this case:

1. First matches the value of variable x with the value of firstnode of the linked list.

If the value matches, it means we want to delete first nodeof a linked list, therefore call the operationDeleteFromHead().

2. Otherwise, match the value of variable x with the value oflast node of the linked list.

If the value matches, it means we want to delete last nodeof a linked list, therefore call the operation DeleteFromTail().

Mubashir Ali - Lecturer (Department of Software Engineering)

17

Page 18: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

3. If first two cases fails then locate the node whose valuematches with the value of variable x.

Such a node has to be found by searching from thebeginning of the list and stopping at the node whose valuematches with the value of variable x.

This is accomplished with a temporary variable tmp used toscan a list within the loop.

The variable tmp is initialized to the head of the linked list.

Mubashir Ali - Lecturer (Department of Software Engineering)

18

9 \7

head tail

8 45

tmp

Page 19: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

In each iteration of the loop, the value of the nodepointed by variable tmp will be compared with thevalue of variable x.

If the value matches then we exited from loop

Otherwise we advance the variable tmp tocompare the value of the next node of linked list.

Consider the following linked list.

Mubashir Ali - Lecturer (Department of Software Engineering)

19

9 \7

head tail

8 45

tmp

Page 20: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

Suppose the value of the variable x is 4.

When we exited from the loop the variable tmp is pointing to thenode containing 4.

We can now remove a node pointed by variable tmp by linking

its predecessor to its successor.

But because the list has only forward links the predecessor of a

node is not reachable from the node.

To locate the predecessor we again scan the list from the

beginning.

Mubashir Ali - Lecturer (Department of Software Engineering)

20

9 \7

head tail

8 45

tmp

Page 21: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

4. This is accomplished with a temporary variable pred used toscan a list within the loop.

The variable pred is initialized to the head of the linked list.

After executing the assignment pred=pred->next, pred refers to

the second node.

After executing the assignment pred=pred->next, pred refers to

the second node.

Mubashir Ali - Lecturer (Department of Software Engineering)

21

Page 22: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

Because next to this node is a node pointed by the variable tmp,i.e. this node is a predecessor of the node pointed byvariable tmp, therefore the loop is exited.

5. Now join the node pointed by variable pred to the node next to

the node pointed by the variable tmp.

Mubashir Ali - Lecturer (Department of Software Engineering)

22

Page 23: Linked List - Dynamic Implementation Mr. Mubashir Ali

Delete(x)

6. Now delete the node pointed by the variable tmp from linkedlist.

Mubashir Ali - Lecturer (Department of Software Engineering)

23

Page 24: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Delete a Particular Node

Mubashir Ali - Lecturer (Department of Software Engineering)

24

Page 25: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Delete a Particular Node

Mubashir Ali - Lecturer (Department of Software Engineering)

25

Page 26: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Update Node

Mubashir Ali - Lecturer (Department of Software Engineering)

26

Page 27: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Update Node

Mubashir Ali - Lecturer (Department of Software Engineering)

27

Page 28: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Search an Object

Mubashir Ali - Lecturer (Department of Software Engineering)

28

Page 29: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Print List

Mubashir Ali - Lecturer (Department of Software Engineering)

29

Page 30: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Sort Link List

Mubashir Ali - Lecturer (Department of Software Engineering)

30

Page 31: Linked List - Dynamic Implementation Mr. Mubashir Ali

Implementation – Reverse Link List

Mubashir Ali - Lecturer (Department of Software Engineering)

31

Page 32: Linked List - Dynamic Implementation Mr. Mubashir Ali

Applications of Linked List

• Implementation of stacks and queues• Implementation of graphs : Adjacency list

representation of graphs is most popular which is useslinked list to store adjacent vertices.

• Dynamic memory allocation : We use linked list of freeblocks.

• Maintaining directory of names• Performing arithmetic operations on long integers• Manipulation of polynomials by storing constants in

the node of linked list• Representing sparse matrices

Mubashir Ali - Lecturer (Department of Software Engineering)

32

Page 33: Linked List - Dynamic Implementation Mr. Mubashir Ali

Applications of Linked List

Real World Examples1. Image viewer – Previous and next images are

linked, hence can be accessed by next and previous button.

2. Previous and next page in web browser – We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.

3. Music Player – Songs in music player are linked to previous and next song. you can play songs either from starting or ending of the list.

Mubashir Ali - Lecturer (Department of Software Engineering)

33

Page 34: Linked List - Dynamic Implementation Mr. Mubashir Ali

Lab-3

Write C++ program to perform dynamicimplementation of Linked List. Write functionto delete data from head, tail, specificposition, add on specific position, update,sort, reverse, search and print entire list.

Mubashir Ali - Lecturer (Department of Software Engineering)

34

Page 35: Linked List - Dynamic Implementation Mr. Mubashir Ali

Summary

DeleteFromHead(x) DeleteFromTail(x) Delete(x) Implementation Delete a Particular Node Update Node Value Search Element Display Linked List Sort Link List Reverse Linked List

Application of Linked List Lab-3

Mubashir Ali - Lecturer (Department of Software Engineering)

35

Page 36: Linked List - Dynamic Implementation Mr. Mubashir Ali

References

you will be able to find course resources at

http://www.mubashirali.com/data-structures-algorithms/

https://www.quora.com/Where-are-linked-lists-used-in-real-life

http://www.cs.cmu.edu/~ab/15-111N09/Lectures/Lecture%2008%20Application%20of%20Linked%20Lists.pdf

Mubashir Ali - Lecturer (Department of Software Engineering)

36