csc 211 data structures lecture 21

64
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz [email protected] 1

Upload: arty

Post on 15-Feb-2016

56 views

Category:

Documents


2 download

DESCRIPTION

CSC 211 Data Structures Lecture 21. Dr. Iftikhar Azim Niaz [email protected]. 1. Last Lecture Summary. Comparison of Merge Sort and Quick Sort Shell Sort Concept, Examples, Algorithm, Complexity Radix Sort Concept, Examples, Algorithm, Complexity Bucket Sort - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 211 Data Structures Lecture 21

1

CSC 211Data Structures

Lecture 21

Dr. Iftikhar Azim [email protected]

1

Page 2: CSC 211 Data Structures Lecture 21

2

Last Lecture Summary Comparison of Merge Sort and Quick Sort Shell Sort

Concept, Examples, Algorithm, Complexity Radix Sort

Concept, Examples, Algorithm, Complexity Bucket Sort

Concept, Examples, Algorithm, Complexity Comparison of Sorting Techniques

2

Page 3: CSC 211 Data Structures Lecture 21

3

Objectives Overview Doubly Linked List Concept Operations on Doubly Linked List

Insertion Deletion Traversing Search

Implementation Code Doubly Linked List with Two Pointers

Page 4: CSC 211 Data Structures Lecture 21

4

Data Structure Operations Following are the major operations: Traversing: Accessing each record exactly once

so that certain items in the record may be processed. (This accessing and processing is sometimes called "visiting" the record.)

Searching: Finding the location of the record with a given key value, or finding the locations of all records that satisfy one or more conditions

Inserting: Adding a new record to the structure Deleting: Removing a record from the structure

Page 5: CSC 211 Data Structures Lecture 21

5

Data Structure Operations (Cont…) Sometimes two or more of the operations may be used in a given situation; e.g., we may want to delete the record with a given key, which

may mean we first need to search for the location of the record. Following two operations, which are used in special

situations, are also be considered: Sorting: Arranging the records in some logical order

(e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number)

Merging: Combining the records in two different sorted files into a single sorted file

Other operations, e.g., copying and concatenation, are also used

Page 6: CSC 211 Data Structures Lecture 21

6

List Using Linked Memory Various cells of memory are not allocated

consecutively in memory. Not enough to store the elements of the list. With arrays, the second element was right next

to the first element. Now the first element must explicitly tell us

where to look for the second element. Do this by holding the memory address of the

second element

Page 7: CSC 211 Data Structures Lecture 21

7

Linked List Pointer Based Implementation of Linked List

ADT Dynamically allocated data structures can be

linked together to form a chain. A linked list is a series of connected nodes (or

links) where each node is a data structure. A linked list can grow or shrink in size as the

program runs. This is possible because the nodes in a

linked list are dynamically allocated

Page 8: CSC 211 Data Structures Lecture 21

8

Composition of Linked List Each node in the linked list contains –

a) One or more members that represent data (e.g. inventory records, customer names, addresses, telephone numbers, etc).

b) A pointer, that can point to another node.

Data Members Pointer

Page 9: CSC 211 Data Structures Lecture 21

9

Composition of linked List A linked list is called “linked” because each node

in the series (i.e. the chain) has a pointer to the next node in the list, e.g.

Head

NULL

a) The head is a pointer to the first node in the list.

b) Each node in the list points to the next node in the list.

c) The last node points to NULL (the usual way to signify the end).

Note, the nodes in a linked list can be spread out over the memory.

Page 10: CSC 211 Data Structures Lecture 21

10

Linked List Actual picture in memory:

1051

1052

1055

1059

1060

1061

1062

1063

1064

1056

1057

1058

1053

1054 2

6

8

7

1

1051

1063

1057

1060

0

head 1054

1063current

2 6 8 7 1

head

current

1065

Page 11: CSC 211 Data Structures Lecture 21

11

List Declarations How to declare a linked list in C or C++?

Step 1) Declare a data structure for the nodes.

e.g. the following struct could be used to create a list where each node holds a float -

struct ListNode{ int data; ListNode *next;};

Page 12: CSC 211 Data Structures Lecture 21

12

List Declarationsa) The first member of the ListNode struct is a int called data. It is to hold the node’s data.

b) The second member is a pointer called next. It is to hold the address of any object that is a ListNode struct. Hence each ListNode struct can point to the next one in the list.

The ListNode struct contains a pointer to an object of the same typeas that being declared. It is called a self-referential data structure.

This makes it possible to create nodes that point to other nodes ofthe same type.

Page 13: CSC 211 Data Structures Lecture 21

13

List DeclarationsStep 2) Declare a pointer to serve as the list head, e.g List *head;Before you use the head pointer, make sure it is initialized to NULL,so that it marks the end of the list.

Once you have done these 2 steps (i.e. declared a node data structure, and created a NULL head pointer, you have an empty linked list.

struct ListNode {int data;ListNode *next;

}; ListNode *head; // List head pointer

Page 14: CSC 211 Data Structures Lecture 21

14

Singly Linked List (SLL) - More Terminology

A node’s successor is the next node in the sequence The last node has no successor

A node’s predecessor is the previous node in the sequence The first node has no predecessor

A list’s length is the number of elements in it A list may be empty (contain no elements)

87 34 78 65

Head

Page 15: CSC 211 Data Structures Lecture 21

15

Traversing a SLL (animation)

988767

Head

nodePtr

Page 16: CSC 211 Data Structures Lecture 21

16

Inserting after (animation)

936756

Head

78nodePtr

Find the node you want to insert afterFirst, copy the link from the node that's already in the list

Then, change the link in the node that's already in the list

Page 17: CSC 211 Data Structures Lecture 21

17

Deleting a node from a SLL In order to delete a node from a SLL, you have

to change the link in its predecessor This is slightly tricky, because you can’t follow a

pointer backwards Deleting the first node in a list is a special case,

because the node’s predecessor is the list header

Page 18: CSC 211 Data Structures Lecture 21

18

Deleting an element from a SLL

936756

Head

936756

Head

• To delete the first element, change the link in the header

• To delete some other element, change the link in its predecessor

(predecessor)

Page 19: CSC 211 Data Structures Lecture 21

19

Doubly Linked List (DLL) In a singly linked list (SLL) one can move

beginning from the head node to any node in one direction only (from left to right)

SLL is also termed as one –way list On the other hand, Doubly Linked List (DLL) is

a two-way list One can move in either direction from left to right

and from right to left This is accomplished by maintaining two linked

fields instead of one as in a SLL

Page 20: CSC 211 Data Structures Lecture 21

20

Motivation Doubly linked lists are useful for playing video

and sound files with “rewind” and “instant replay”

They are also useful for other linked data which require “rewind” and “fast forward” of the data

Page 21: CSC 211 Data Structures Lecture 21

21

Each node on a list has two pointers. A pointer to the next element. A pointer to the previous element The beginning and ending nodes' previous

and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list

…………

Doubly Linked List

Page 22: CSC 211 Data Structures Lecture 21

22

Doubly-Linked Lists (DLL)

Here is a doubly-linked list (DLL):

Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any)

The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty)

Head

56 67 93

Page 23: CSC 211 Data Structures Lecture 21

23

Doubly Linked Lists In a Doubly Linked List each item points to both its

predecessor and successor prev points to the predecessor next points to the successor

10 7020 5540

HeadCur Cur->nextCur->prev

Page 24: CSC 211 Data Structures Lecture 21

24

DLLs compared to SLLs Advantages:

Can be traversed in either direction (may be essential for some programs)

Some operations, such as deletion and inserting before a node, become easier

Disadvantages: Requires more space

to store backward pointer

List manipulations are slower because more links

must be changed Greater chance of

having bugs because more links

must be manipulated

Page 25: CSC 211 Data Structures Lecture 21

25

Double Linked List – Definition in Cstruct Node{ int data; Node* next; Node* prev;} *Head, *nodePtr;

Page 26: CSC 211 Data Structures Lecture 21

26

Operations on DLL The two node links allow traversal of the list in

either direction While adding or removing a node in a doubly

linked list requires changing more links than the same operations on a singly linked list

The operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the

previous node during traversal or no need to traverse the list to find the previous

node, so that its link can be modified

Page 27: CSC 211 Data Structures Lecture 21

27

Doubly Linked List Operations

insertNode(Node *Head, int item)//add new node to ordered doubly linked list

deleteNode(Node *Head, int item) //remove a node from doubly linked list

searchNode(Node *Head, int item)

print(Node *Head, int item)

Page 28: CSC 211 Data Structures Lecture 21

28

DLL - Insertion

Page 29: CSC 211 Data Structures Lecture 21

29

Inserting a Node Insert a node NewNode before Cur (not at

front or rear)

10 7020 55

40Head

NewNode

Cur

NewNode->next = Cur;

NewNode->prev = Cur->prev;

Cur->prev = NewNode;

(NewNode->prev)->next = Newnode;

Page 30: CSC 211 Data Structures Lecture 21

30

DELETE:

Pointer change for implementation of a Deletion

Doubly – Linked List

Page 31: CSC 211 Data Structures Lecture 21

31

Deleting a node from a DLL Node deletion from a DLL involves changing two links In this example, we will delete node 67

We don’t have to do anything about the links in node 67 Deletion of the first node or the last node is a special case

Head

56 67 93

Page 32: CSC 211 Data Structures Lecture 21

32

Deleting a Node Delete a node Cur (not at front or rear)

(Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;

10 7020 5540

HeadCur

Page 33: CSC 211 Data Structures Lecture 21

33

Other operations on linked lists Most “algorithms” on linked lists such as insertion, deletion, and searching—are

pretty obvious; you just need to be careful

Sorting a linked list is just messy, since you can’t directly access the nth element you have to count your way through a lot of other

elements

Page 34: CSC 211 Data Structures Lecture 21

34

DLL with Dummy Head Node To simplify insertion and deletion by avoiding special cases of deletion and insertion at front and rear, a dummy head node is added at the head of the list

The last node also points to the dummy head node as its successor

Page 35: CSC 211 Data Structures Lecture 21

35

Doubly Linked Lists with Dummy Head

Non-Empty List

Empty List

7020 5540

Head

10Dummy Head Node

Head

Dummy Head Node

Page 36: CSC 211 Data Structures Lecture 21

36

DLL– Creating Dummy Node at Headvoid createHead(Node *Head){Head = new Node;Head->next = Head;Head->prev = Head;

}

Head

Dummy Head Node

Page 37: CSC 211 Data Structures Lecture 21

37

Insert a Node New to Empty List (with Cur pointing to dummy head node)

Head

Dummy Head Node

New

20

New->next = Cur;

New->prev = Cur->prev;

Cur->prev = New;

(New->prev)->next = New;

Cur

Inserting a Node as First Node

Page 38: CSC 211 Data Structures Lecture 21

38

Inserting a Node at Head Insert a Node New after dummy node and

before Cur

Head

Dummy Head Node

Cur

20

New->next = Cur;

New->prev = Cur->prev;

Cur->prev = New;

(New->prev)->next = New;

10

New

Page 39: CSC 211 Data Structures Lecture 21

39

Insert a Node New in the middle and before CurNew->next = Cur;

New->prev = Cur->prev;

Cur->prev = New;

(New->prev)->next = New; // same as insert front!

55

Head

10Dummy Head Node

20

Cur

40

New

Inserting a Node – in the Middle

Page 40: CSC 211 Data Structures Lecture 21

40

Insert a Node New at Rear (with Cur pointing to dummy head)New->next = Cur;

New->prev = Cur->prev;

Cur->prev = New;

(New->prev)->next = New; // same as insert front!

7020 5540

Head

10Dummy Head Node

Cur New

Inserting a Node at Rear

Page 41: CSC 211 Data Structures Lecture 21

41

DLL-Insert – Implementation Codevoid insertNode(Node *Head, int item){ Node *New, *Cur; New = new Node;New->data = item;Cur = Head->next;while(Cur != Head){ //position Cur for insertion

if(Cur->data < item)Cur = Cur->next;

else break;

}New->next = Cur;New->prev = Cur->prev;Cur->prev = New;(New->prev)->next = New;

}

Page 42: CSC 211 Data Structures Lecture 21

42

Deleting a Node – at Head

Delete a node Cur at front

7020 5540

Head

10Dummy Head Node

Cur

(Cur->prev)->next = Cur->next;

(Cur->next)->prev = Cur->prev;

delete Cur;

Page 43: CSC 211 Data Structures Lecture 21

43

Delete a node Cur in the middlevoid deleteNode(Node *Head, int item){

Node *Cur;Cur = searchNode(Head, item);if(Cur != NULL){

Cur->prev->next = Cur->next;Cur->next->prev = Cur->prev;delete Cur;

} // end of if } // end of function

70

Head

10Dummy Head Node

20 5540

Cur

Deleting a Node – in the Middle

Page 44: CSC 211 Data Structures Lecture 21

44

Delete a node Cur at rear(Cur->prev)->next = Cur->next;

(Cur->next)->prev = Cur->prev;

delete Cur; // same as delete front and middle!

7020 5540

Head

10Dummy Head Node

Cur

Deleting a Node – at end

Page 45: CSC 211 Data Structures Lecture 21

45

DLL – Searching a NodeNode* searchNode(Node *Head, int item){Node *Cur = Head->next;while(Cur != Head){

if(Cur->data == item)return Cur;

if(Cur->data < item)Cur = Cur->next;

elsebreak;

}return NULL;

}

Page 46: CSC 211 Data Structures Lecture 21

46

DLL – Printing the Whole Listvoid print(Node *Head){ Node *Cur=Head->next;while(Cur != Head){

cout << Cur->data << " ";Cur = Cur->next;

}cout << endl;

}

Page 47: CSC 211 Data Structures Lecture 21

47

Main Programvoid main(){

Node *Head, *temp; createHead(Head); print(Head);insertNode(Head, 3); print(Head);insertNode(Head, 5); print(Head);insertNode(Head, 2);print(Head);insertNode(Head, 7);insertNode(Head, 1);insertNode(Head, 8);print(Head);deleteNode(Head, 7);deleteNode(Head, 0);print(Head);temp = searchNode(Head, 5);if(temp != NULL)

cout << "Data is contained in the list" << endl;else

cout << "Data is NOT contained in the list" << endl;}

Result is:

33 52 3 51 2 3 5 7 81 2 3 5 8Data is contained in the list

Page 48: CSC 211 Data Structures Lecture 21

48

Doubly Linked List – Worst Case insertion at head or tail is in O(1)

deletion at either end is on O(1) element access is still in O(n)

48

Page 49: CSC 211 Data Structures Lecture 21

49

c

head tail

head = new Node (); tail = new Node (); head->next = tail; tail->prev = head;

Doubly Linked List with Two Pointers With two Pointers

One for HeadAnother for Rear

Page 50: CSC 211 Data Structures Lecture 21

50

newNode = new Node;newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

Inserting into a Doubly Linked List

a c

head tailcurrent

Page 51: CSC 211 Data Structures Lecture 21

51

a c

head tail

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

bcurrent

Inserting into a Doubly Linked List

Page 52: CSC 211 Data Structures Lecture 21

52

a c

head tail

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

bcurrent

Inserting into a Doubly Linked List

Page 53: CSC 211 Data Structures Lecture 21

53

a c

head tail

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

bcurrent

Inserting into a Doubly Linked List

Page 54: CSC 211 Data Structures Lecture 21

54

a c

head tail

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

bcurrent

Inserting into a Doubly Linked List

Page 55: CSC 211 Data Structures Lecture 21

55

a c

head tail

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

bcurrent

Inserting into a Doubly Linked List

Page 56: CSC 211 Data Structures Lecture 21

56

newNode = new Node()newNode->prev = current;newNode->next = current->next;newNode->prev->next = newNode;newNode->next->prev = newNode;current = newNode

a c

head tailbcurrent

Inserting into a Doubly Linked List

Page 57: CSC 211 Data Structures Lecture 21

57

Deleting from a Doubly Linked List

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

a c

head bcurrent

tail

Page 58: CSC 211 Data Structures Lecture 21

58

Deleting from a Doubly Linked List

a c

head bcurrent

oldNode

tail

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

Page 59: CSC 211 Data Structures Lecture 21

59

Deleting from a Doubly Linked List

a c

head bcurrent oldNode

tail

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

Page 60: CSC 211 Data Structures Lecture 21

60

a c

head bcurrent

oldNode

tail

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

Deleting from a Doubly Linked List

Page 61: CSC 211 Data Structures Lecture 21

61

a c

head bcurrent oldNode

tail

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

Deleting from a Doubly Linked List

Page 62: CSC 211 Data Structures Lecture 21

62

a c

headcurrent

tail

oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev;delete oldNode;

Deleting from a Doubly Linked List

Page 63: CSC 211 Data Structures Lecture 21

63

a b c d

head

Circular Linked lists

Insertion and Deletion implementation left as an exercise

Page 64: CSC 211 Data Structures Lecture 21

64

Summary Doubly Linked List Concept Operations on Doubly Linked List

Insertion Deletion Traversing Search

Implementation Code Doubly Linked List with Two Pointers

Insertion and Deletion