L6_print

L6_print - CSE 12 : Basic data structures and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE 12 : Basic data structures and object-oriented design Jacob Whitehill jake@mplab.ucsd.edu 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 tails next pointer points back to the head . If the linked list is doubly-linked, then the heads 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 dont 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 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 weve 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....
View Full Document

This note was uploaded on 11/02/2011 for the course CSE 12 taught by Professor Gary during the Summer '08 term at UCSD.

Page1 / 7

L6_print - CSE 12 : Basic data structures and...

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

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