Linked Lists CMSC 202 Version 402 Types of

  • Slides: 18
Download presentation
Linked Lists CMSC 202, Version 4/02

Linked Lists CMSC 202, Version 4/02

Types of Linked Lists • • Singly-linked list Doubly-linked list Singly, circularly-linked list Doubly,

Types of Linked Lists • • Singly-linked list Doubly-linked list Singly, circularly-linked list Doubly, circularly-linked list CMSC 202, Version 4/02 2

Singly-linked List data head pointer head node tail* *Some refer to only the last

Singly-linked List data head pointer head node tail* *Some refer to only the last node as the tail CMSC 202, Version 4/02 3

Doubly-linked List data head pointer CMSC 202, Version 4/02 4

Doubly-linked List data head pointer CMSC 202, Version 4/02 4

Singly, Circularly-linked List data head pointer CMSC 202, Version 4/02 5

Singly, Circularly-linked List data head pointer CMSC 202, Version 4/02 5

Doubly, Circularly-linked List data head pointer CMSC 202, Version 4/02 6

Doubly, Circularly-linked List data head pointer CMSC 202, Version 4/02 6

Singly-linked List in OO other private data members* list object data object node object

Singly-linked List in OO other private data members* list object data object node object *May include maximum number of nodes, current number of nodes, or other items. CMSC 202, Version 4/02 7

A Look at Common Operations on a Singly-linked List • Locating an item •

A Look at Common Operations on a Singly-linked List • Locating an item • Inserting an item • Removing an item CMSC 202, Version 4/02 8

Locating an Item Other Private Data members 10 42 63 head next CMSC 202,

Locating an Item Other Private Data members 10 42 63 head next CMSC 202, Version 4/02 99 9

Locating an Item (con’t) • Design Decision: – What do we do if the

Locating an Item (con’t) • Design Decision: – What do we do if the item is not in the list? • Performance: – What is the best case performance? – What is the worst case performance? – What is the average performance? – How is the performance different in a sorted linked list (best, worst, average cases)? – Sorted or unsorted, would the performance be better if the list were doubly-linked? CMSC 202, Version 4/02 10

Inserting an Item Insert 17 at the head Other Private Data members 10 42

Inserting an Item Insert 17 at the head Other Private Data members 10 42 63 head next CMSC 202, Version 4/02 99 11

Inserting an Item (con’t) Insert 17 in sorted order Other Private Data members 10

Inserting an Item (con’t) Insert 17 in sorted order Other Private Data members 10 42 63 head next CMSC 202, Version 4/02 99 12

Inserting an Item (con’t) In general, with a sorted list, there are three cases:

Inserting an Item (con’t) In general, with a sorted list, there are three cases: - insert as first node - insert as last node - insert somewhere in between Other Private Data members 10 42 63 head next CMSC 202, Version 4/02 99 13

Inserting an Item (con’t) Design Decision: • What do we do if duplicates are

Inserting an Item (con’t) Design Decision: • What do we do if duplicates are not allowed? Performance: • If the list is not maintained in sorted order, it is most efficient to insert at the head. • What is the asymptotic performance of this kind of insert? – If we choose to insert at the end of the list, what is the asymptotic performance? CMSC 202, Version 4/02 14

Inserting an Item (con’t) • If the list is maintained in sorted order, we

Inserting an Item (con’t) • If the list is maintained in sorted order, we must search to find the proper place in the list, then insert. – What is the asymptotic performance of this insert? • Would performance be better if the list were doubly-linked? CMSC 202, Version 4/02 15

Removing an Item Other Private Data members 10 17 42 63 head next 99

Removing an Item Other Private Data members 10 17 42 63 head next 99 Three cases: - remove first item - remove last item - remove an item in between CMSC 202, Version 4/02 16

Removing an Item (con’t) How does the algorithm change if we use a doubly-linked

Removing an Item (con’t) How does the algorithm change if we use a doubly-linked list? Other Private Data members 10 17 42 63 99 head next prev CMSC 202, Version 4/02 17

Removing an Item (con’t) Design decisions: • What do we do if the item

Removing an Item (con’t) Design decisions: • What do we do if the item we are asked to delete is not in the list? • What do we do if duplicates are allowed in the list? Performance: • Would performance be better if the list were doubly-linked? CMSC 202, Version 4/02 18