doubly linked lists deleting from the end of the list – have to traverse the entire list to stop...

19
Doubly Linked Lists Deleting from the end of the list Have to traverse the entire list to stop right in front of tail to delete it, so O(n) With head and tail pointer, still O(n), tail has to point to the node in front of the original tail (original tail’s predecessor). To avoid this, redefine the linked list so that each node has 2 pointers, one to the successor and one to the predecessor

Upload: alan-hoover

Post on 23-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Doubly Linked Lists• Deleting from the end of the list

– Have to traverse the entire list to stop right in front of tail to delete it, so O(n)

– With head and tail pointer, still O(n), tail has to point to the node in front of the original tail (original tail’s predecessor).

– To avoid this, redefine the linked list so that each node has 2 pointers, one to the successor and one to the predecessor

Which Operation of DLL is More Efficient than SLL

• ?

Circular Lists

• In some situations, a circular list is needed in which nodes form a ring– E.g., resource sharing among process– The pointer current points to the next process to

which the resource should be assigned

Insertion at the Front and End of a Circular List

Time complexities?

Circular List (cont’d)

• Time complexity of deletion from the end?

• Circular doubly linked list

Define Circular Linked List

Example of Add a Node to the Head of a Circular LinkedList

• Different Cases• Discuss for each case

Self-Organizing Lists

• Linked lists have one serious drawback– Sequential scanning to locate a searched-for element– Time complexity of search is O(n)

• Can improve search efficiency by dynamically organizing the list in a certain manner– Move-to-front method (i.e., Most Recently Used)

• After desired element is located, put it at the beginning– Transpose method

• After desired element is located, swap it with predecessor– Count method (i.e., Most Frequently Used)

• Order the list by number of times elements are accessed– Ordering method

• Order the list using certain criteria

Self-Organizing Lists (cont’d)

• With first 3 methods, try to locate elements most likely to be looked for near the beginning of the list– Move-to-front vs. Transpose

• The ordering method is particularly advantageous when searching for information that is not in the list, because the search can terminate without scanning the entire list

• But, if the desired element is not in the list, the first 3 methods just add a new node at the end of the list O(1), whereas the ordering method must maintain the order

Optimal Static Ordering

• Analysis of the efficiency of these methods usually compares their efficiency to that of optimal static ordering (i.e., all data already ordered by the frequency of their occurrence)

• Optimal static ordering requires two passes through the body of data– Build the list– Use the list for search alone

Optimal Static Ordering (cont’d)

• Example: Find the optimal static ordering of the following stream of data:

A C B C D A D A C A C C E E

• Optimal static ordering used for comparison purposes only

An Application

Sparse Tables

• In many applications, the choice of a table seems to be the most natural one, but space considerations may preclude this choice

• Particularly true if only a small portion of the table is actually used (i.e., a sparse table)

• A sparse table can be replaced by a system of linked lists

Sparse Tables (cont’d)

• Consider the problem of storing one semester grades for 8000 students and 300 classes

• Natural implementation is a 2D array of classes and students

• Assuming, on the average, students take 4 classes per semester, each column has only 4 cells occupied and the rest (296 or 98.7%) are wasted

Sparse Tables (cont’d)

• A better implementation is to use 2 1D arrays of linked lists– Each element of array class is a pointer to a linked

list of students taking a class– Each element of array student is a pointer to a

linked list of the classes taken by a student

• The space needed will be approximately 10% of the space needed for the sparse table implementation

Template DLL

• Make the linked list to store any type of contents