{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–3. 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 Circular linked lists. Tuesday, August 9, 2011 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 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 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 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 Circular linked lists List of size 2: _prev _next _data Node _head CircularDoublyLinkedList _prev _next _data Node Tuesday, August 9, 2011 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
Image of page 1

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

View Full Document Right Arrow Icon
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 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 Circular linked lists CPU scheduling: One CPU can only execute one computer program at any given time. On a single-core machine, to simulate “multitasking”, each program is given a small “timeslice” (few milliseconds) to run on the CPU. After the timeslice expires, the next program in the list of processes is selected, and so on.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}