C SC 205 Lecture 11: Doubly-Linked Lists
major resources: Data Structures and the Java Collections Framework Second Edition,
William Collins, McGraw-Hill, 2005
Introduction to Programming and OO Design
using Java, Niño and Hosch, Wiley & Sons, 2002
[ previous
| schedule
| next ]
Wednesday October 24 (week 7)
Doubly-Linked Lists
- In general, each list entry has references to both the entry that follows it on the list and
the one that precedes it.
- This introduces additional space overhead, two references per entry.
- This should result in some time savings -- what can be done faster?
- There are multiple implementation strategies
- Appreciate the separation of specification and implementation
The LinkedList class
- Member of the Java Collection Framework
- Extends AbstractSequentialList and implements several interfaces
- Specification includes two kinds of iterators:
- Iterator, which we've already studied. One-way traversal
- ListIterator, which provides two-way traversals and thus additional methods
such as previous() and hasPrevious()
- Implemented using doubly-linked list. More on this later.
Data Structures for Doubly-Linked Lists
- Simple two-way structure
- Each entry has three references: element, previous entry, next entry
- Head is reference to first entry
- First entry has null for previous
- Last entry has null for next
- Consider code to add a new entry in front of an arbitrary entry: what are the special cases?
- Consider code to remove an arbitrary entry: what are the special cases?
- Simple two-way circular structure
- Each entry has three references: element, previous entry, next entry
- Head is reference to first entry
- First entry links to last entry as its previous
- Last entry links to first entry as its next
- Consider code to add a new entry in front of an arbitrary entry: what are the special cases?
- Consider code to remove an arbitrary entry: what are the special cases?
- Any advantages over the simple two-way structure?
- Two-way circular structure with dummy header entry
- Each entry has three references: element, previous entry, next entry
- Head is reference to a dummy entry
- dummy entry has null element and is not a member of the list per se
- dummy entry links to last entry as its previous
- dummy entry links to first entry as its next
- empty list consists of dummy entry linking to itself as both previous and next
- First entry links to dummy entry as its previous
- Last entry links to dummy entry as its next
- Consider code to add a new entry in front of an arbitrary entry: any special cases?
- Consider code to remove an arbitrary entry: any special cases?
- Any advantages over the other two structures? Oh, yeah!
Implementation of LinkedList class
- Implemented in java.util package
- Data structure is two-way circular with dummy header entry
- Even though implementation is circular, specification is linear (there is a first and a last entry)
- Maintains a size variable for constant time size()
- has iterator() for one-way traversal
- has listIterator() for two-way traversal
[ C
SC 205 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)