linked list implementation mr. mubashir ali · 3. linked-lists •this organization rids us from...

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

Upload: others

Post on 16-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Linked List Implementation

Mr. Mubashir AliLecturer (Dept. of Software Engineering)

[email protected] Garrison University, Lahore

1

Lecture 04

Page 2: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Previous Lecture

• Abstract List or List ADT

• Arrays

• Passing Arrays to Functions

• Searching Arrays: Linear VS Binary Search

• Lab Work

• 1st Submission

Mubashir Ali - Lecturer (Department of Software Engineering)

2

Page 3: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Outline

1. Disadvantages of Physically Linear DataStructures

2. Treasure Hunt for Kids

3. Linked List

4. Traversal and Implementation

5. Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

3

Page 4: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

1. Disadvantages of Physically Linear Data Structures

• Fixed size

• Not flexible

• Insertion / deletion is difficult

• Always need boundary conditions

• etc

Mubashir Ali - Lecturer (Department of Software Engineering)

4

Page 5: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Continued…

• Given an ordered-list:

– What happens when we add an element to the list?

– What happens when we delete an element from the list?

• In the array implementation, such cases require heavydata movement which is very costly.

• When we deal with linear or ordered-lists, it is in generally notrequired to have a random access to the elements. e.g., weusually don’t need to ask questions like: what is the 8thelement in the list?

• In most cases, the data is processed in a strictly sequentialorder.

• Therefore all that is required is the access to next element.

Mubashir Ali - Lecturer (Department of Software Engineering)

5

Page 6: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Continued…

• In our previous mapping, the next element happensto be the element at the next index in the array.

• If we can some how tell where the next element isstored, we can eliminate this restriction of placingthe next element at the next physical location!

• This gives rise to the notion of logical adjacency asopposed to physical adjacency.

Mubashir Ali - Lecturer (Department of Software Engineering)

6

Page 7: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Continued…

• In that case, in order to access the elements ofthe list, all we need is a starting point and somemechanism to from one element to the next.

• In other words, we need a starting point anda link form one element to the next.

Mubashir Ali - Lecturer (Department of Software Engineering)

7

Page 8: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

2. Treasure Hunt for Kids

In a party, clues are made up and scattered allover the house. Kids are divided into teams of 1to 4, depending on how many are at the party.Each clue leads to the next and at the end of thetrail is a treasure for the team, for example,chocolates, toys, etc.

Mubashir Ali - Lecturer (Department of Software Engineering)

8

Page 9: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Continued…

• In that case, in order to access the elements ofthe list, all we need is a starting point and somemechanism to from one element to the next.

• In other words, we need a starting point and alink from one element to the next.

• We can only travel in the direction of the link.• We have a chain like structure and we can access

the elements in the chain by just following thelinks in the chain.

• Such an organization is known as a linked-list.

Mubashir Ali - Lecturer (Department of Software Engineering)

9

Page 10: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

3. Linked-lists

• This organization rids us from the requirement tomaintaining the logical as well as physical adjacency.

• Now all we need to maintain is the logical sequences andtwo logically adjacent elements need to not bephysically next to each other.

A linked list is a series of connected nodes

Each node contains at least

A piece of data (any type)

Pointer to the next node in the listHead: pointer to the first node

The last node points to NULL

Mubashir Ali - Lecturer (Department of Software Engineering)

10

Page 11: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Key questions

• How do we represent a position in the list ?

• How do we iterate over the elements of a list ?

Mubashir Ali - Lecturer (Department of Software Engineering)

11

Page 12: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Implementation

• We can implement this organization by storing the information and handle to the next element with the other data as follows:-

struct element {

handle next; // handle to next list element

data_type data;

};

handle head; // starting point - handle to first list element

head = // initialize head to - the end marker

Mubashir Ali - Lecturer (Department of Software Engineering)

12

Page 13: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Linked-List Traversal

void traverse (handle head)

{

handle current = head;

while ( current != ) {

process data at current;

current = next field of current element

}

}

Mubashir Ali - Lecturer (Department of Software Engineering)

13

Page 14: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Add an Element after current element

Mubashir Ali - Lecturer (Department of Software Engineering)

14

Page 15: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

15

Page 16: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Fragile, Handle Links With Care

• If one link is broken, you loose access to all subsequent elements.

• What happens if you swap the following two lines in add:

next field of new element = next field of current; //step 2

next field of current = new_element; //step 3

• to

next field of current = new_element; //step 2

next field of new element = next field of current; //step 3

Mubashir Ali - Lecturer (Department of Software Engineering)

16

Page 17: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

17

Page 18: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Delete an Element After Current Element

void delete (handle current)

{

handle temp = next field of current;

next field of current = next filed of next element

of current

// same as next field of

temp

discard temp

}

Mubashir Ali - Lecturer (Department of Software Engineering)

18

Page 19: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Delete an Element After Current Element

Mubashir Ali - Lecturer (Department of Software Engineering)

19

Page 20: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Garbage

• When all access paths to a data object are destroyedbut the data object continues to exist, the dataobject is said to be garbage.

• Garbage is a less serious but still troublesomeproblem. A data object that has become garbage tiesup storage that might otherwise be reallocated foranother purpose.

• A buildup of garbage can force the program toterminate prematurely because of lack of availablefree storage.

Mubashir Ali - Lecturer (Department of Software Engineering)

20

Page 21: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Garbage

Mubashir Ali - Lecturer (Department of Software Engineering)

21

Page 22: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Array Based Implementation

• Handle is the index of the next element in the array

struct node {

element_type data;// data

int next;// pointer to the next

} ;

Mubashir Ali - Lecturer (Department of Software Engineering)

22

Page 23: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Array Based Implementation

class List {private:

int head;int available;int size;node * NodeList;

public:List (int s = 10); // constructor - default size =

10~List( ) { delete [ ] NodeList; } // destructorvoid processList();int search (int data);bool add (int data); // add in ascending ordervoid remove (int data); // delete node from the list

}

Mubashir Ali - Lecturer (Department of Software Engineering)

23

Page 24: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Array Based Implementation

List::List(int s)

{

if (s <= 0) size = 10; else size = s;

nodeList = new node [size]; // create array

for (int i = 0; i < size-1; i++) // initialize

nodeList[i].next = i+1; // points to the next

nodeList [size-1].next = -1; // last node – there is no next

available = 0;

head = -1; // -1 is used as

}

Mubashir Ali - Lecturer (Department of Software Engineering)

24

index 0 1 2 3 4 5 6 7 8 9

next 1 2 3 4 5 6 7 8 9 -1 available = 0

data - - - - - - - - - - head = -1

Page 25: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

25

Page 26: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

26

Initial state (available = 0, head = -1)

index 0 1 2 3 4 5 6 7 8 9

next 1 2 3 4 5 6 7 8 9 -1

data - - - - - - - - - -

Add 5 (available = 1, head = 0)

index 0 1 2 3 4 5 6 7 8 9

next -1 2 3 4 5 6 7 8 9 -1

data 5 - - - - - - - - -

Page 27: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

27

Add 7 (available = 2, head = 0)

index 0 1 2 3 4 5 6 7 8 9

next 1 -1 3 4 5 6 7 8 9 -1

data 5 7 - - - - - - - -

Add 8 (available = 3, head = 0)

index 0 1 2 3 4 5 6 7 8 9

next 1 2 -1 4 5 6 7 8 9 -1

data 5 7 8 - - - - - - -

Page 28: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

28

Add 6 (available = 4, head = 0)

index 0 1 2 3 4 5 6 7 8 9

next 3 2 -1 1 5 6 7 8 9 -1

data 5 7 8 6 - - - - - -

Add 2 (available = 5, head = 4)

index 0 1 2 3 4 5 6 7 8 9

next 3 2 -1 1 0 6 7 8 9 -1

data 5 7 8 6 2 - - - - -

Page 29: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

29

Add 7 (available = 1, head = 4)

index 0 1 2 3 4 5 6 7 8 9

next 3 5 -1 2 0 6 7 8 9 -1

data 5 - 8 6 2 - - - - -

Add 4 (available = 5, head = 4)

index 0 1 2 3 4 5 6 7 8 9

next 3 0 -1 2 1 6 7 8 9 -1

data 5 4 8 6 2 - - - - -

Page 30: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

30

Page 31: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Mubashir Ali - Lecturer (Department of Software Engineering)

31

Delete 2 (available = 4, head = 1)

index 0 1 2 3 4 5 6 7 8 9

next 3 0 -1 2 5 6 7 8 9 -1

data 5 4 8 6 - - - - - -

Delete 8 (available = 2, head = 1)

index 0 1 2 3 4 5 6 7 8 9

next 3 0 4 -1 5 6 7 8 9 -1

data 5 4 - 6 - - - - - -

Page 32: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

Summary

Disadvantages of Physically Linear DataStructures

Treasure Hunt for Kids

Linked List

Traversal and Implementation

Array Based Implementation

Mubashir Ali - Lecturer (Department of Software Engineering)

32

Page 33: Linked List Implementation Mr. Mubashir Ali · 3. Linked-lists •This organization rids us from the requirement to maintaining the logical as well as physical adjacency. •Now all

References

you will be able to find course resources at

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

Mubashir Ali - Lecturer (Department of Software Engineering)

33