L6 - CSE 12 Basic data structures and object-oriented...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 12 : Basic data structures and object-oriented design Jacob Whitehill [email protected] Lecture Six 9 Aug 2011 Tuesday, August 9, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Circular linked lists. Tuesday, August 9, 2011
Background image of page 2
Circular linked lists Before moving on to other data structures, we will discuss one more variant of the basic “linked list” concept. A circular linked list is a list where the tail’s “next” pointer points back to the head . If the linked list is doubly-linked, then the head’s “previous” pointer also points back to the tail . Tuesday, August 9, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Circular linked lists _prev _next _data Node _prev _next _data Node _head _tail _prev _next _data: o Node CircularDoublyLinkedList _prev _next _data: o2 Node Tuesday, August 9, 2011
Background image of page 4
Circular linked lists _prev _next _data Node _prev _next _data Node _head _tail _prev _next _data: o Node CircularDoublyLinkedList _prev _next _data: o2 Node With a circular linked list, we don’t even really need a tail. Instead, all we really care about is whether we add to the front of the list (to the “right” of _head), or to the back of the list (to the “left” of _head). Tuesday, August 9, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Circular linked lists The utility of circular linked lists is perhaps most clearly illustrated when there are no dummy nodes. Empty list: _head = null. List of size 1: _prev _next _data Node _head CircularDoublyLinkedList Tuesday, August 9, 2011
Background image of page 6
Circular linked lists List of size 2: _prev _next _data Node _head CircularDoublyLinkedList _prev _next _data Node Tuesday, August 9, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Iterating through a circular linked list As long as a circular linked list is non-empty, an Iterator can iterate forever . Just keep following the current Node ’s _next pointer. class CircularListIterator { Node _current; ... boolean hasNext () { return _listSize > 0; } Object next () { _current = _current._next; return _current._data; } } Tuesday, August 9, 2011
Background image of page 8
Simulating a circular linked list Using DoublyLinkedList12 (with dummy nodes, but without pointers to “loop back around”), we can easily simulate a circular linked list. In Iterator.next() , if we’ve iterated to the tail, then just start back over at the head ... Object next () { if (_current == _tail) { // Loop back _current = _head; } _current = _current._next; ... } Tuesday, August 9, 2011
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Circular linked lists Circular linked lists are most useful for storing a collection of objects in which “looping forever” is an intuitive and useful operation. Examples: Looping around vertices of a polygon. Tuesday, August 9, 2011
Background image of page 10
Circular linked lists CPU scheduling: One CPU can only execute one computer program at any given time. On a single-core machine, to simulate
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 55

L6 - CSE 12 Basic data structures and object-oriented...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online