lists - Computer Science 211 Data Structures Mount Holyoke...

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

View Full Document Right Arrow Icon
Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Linked Structures So far, all of our structures for holding collections of items have been very simple. We’ve used only arrays and Vector s. These have some pretty significant limitations. Vector s are resizeable, but it is an expensive operation. It’s also expensive to add or remove objects from the start or the middle of the vector. We can do better. We will begin our study of more advanced data structures with lists . These are structures whose elements are in a linear order. Singly Linked Lists These came up just briefly last time as a motivation for iterators. Most of you have seen the idea of a linked list : List Objects . . . . . . . . . . . . head This structure is made up of a pointer to the first list element and a collection of list elements. The structure that makes up a list element has two fields: 1. value : the Object which is stored at that list element’s position in the list. 2. next : a pointer to the next list element, or null for the last element. So the data for a very basic linked structure could look like this: class SimpleListNode<E> { protected E value; protected SimpleListNode<E> next; } public class SimpleLinkedList<E> { protected SimpleListNode<E> head; }
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
CS 211 Data Structures Fall 2009 As we saw with the VectorIterator , public is not specified in the class definition, since we aren’t allowing regular users to create one of these, only a SimpleLinkedList<E> . So if we want to create one of these, it’s very easy. We just construct a SimpleLinkedList<E> and set its head to null . public SimpleLinkedList() { head = null; } How about adding an element? This involves two steps: 1. construct a new list node for the element 2. insert the new list node into the list Let’s think about what this will mean. We add our first element, say a 1, we want this list to go from just an empty head reference, to a node pointed at by head which has the 1 as its value and null as its next . Now, we add another element, say 2. We have two choices. We can add at the beginning or at the end. Now, we add another element, 3. Now we have three choices. Beginning, middle, or end. In general, we can add at position 0, 1, or 2. Construction of the new list node is easy, once we know what to set its next pointer to. Here’s a constructor: public SimpleListNode(E value, SimpleListNode<E> next) { this.value = value; = next; } We’ll see that we will need to be able to set and retrieve the value and the next pointer. We’ll call the accessors value() and next() , and the mutators setValue() and setNext() . We would like to allow additions to any place in our list, so we will develop a general add method that deals with all three of the cases described above. We’ll need to provide our
Background 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 ]}

Page1 / 12

lists - Computer Science 211 Data Structures Mount Holyoke...

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