11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 4.1 Lists 101/** Singly linked list node */class Link {priv<strong>at</strong>e E element; // Value for this nodepriv<strong>at</strong>e Link next; // Pointer to next node in list// ConstructorsLink(E it, Link nextval){ element = it; next = nextval; }Link(Link nextval) { next = nextval; }}Link next() { return next; } // Return next fieldLink setNext(Link nextval) // Set next field{ return next = nextval; } // Return element fieldE element() { return element; } // Set element fieldE setElement(E it) { return element = it; }Figure 4.4 A simple singly linked list node implement<strong>at</strong>ion.list node class is th<strong>at</strong> it can be reused by the linked implement<strong>at</strong>ions for the stack<strong>and</strong> queue d<strong>at</strong>a structures presented l<strong>at</strong>er in this chapter. Figure 4.4 shows theimplement<strong>at</strong>ion for list nodes, called the Link class. Objects in the Link classcontain an element field to store the element value, <strong>and</strong> a next field to store apointer to the next node on the list. The list built from such nodes is called a singlylinked list, or a one-way list, because each list node has a single pointer to the nextnode on the list.The Link class is quite simple. There are two forms for its constructor, onewith an initial element value <strong>and</strong> one without. Member functions allow the linkuser to get or set the element <strong>and</strong> link fields.Figure 4.5(a) shows a graphical depiction for a linked list storing four integers.The value stored in a pointer variable is indic<strong>at</strong>ed by an arrow “pointing” to something.Java uses the special symbol null for a pointer value th<strong>at</strong> points nowhere,such as for the last list node’s next field. A null pointer is indic<strong>at</strong>ed graphicallyby a diagonal slash through a pointer variable’s box. The vertical line between thenodes labeled 23 <strong>and</strong> 12 in Figure 4.5(a) indic<strong>at</strong>es the current position (immedi<strong>at</strong>elyto the right of this line).The list’s first node is accessed from a pointer named head. To speed accessto the end of the list, <strong>and</strong> to allow the append method to be performed in constanttime, a pointer named tail is also kept to the last link of the list. The position ofthe current element is indic<strong>at</strong>ed by another pointer, named curr. Finally, becausethere is no simple way to compute the length of the list simply from these threepointers, the list length must be stored explicitly, <strong>and</strong> upd<strong>at</strong>ed by every oper<strong>at</strong>ionth<strong>at</strong> modifies the list size. The value cnt stores the length of the list.Note th<strong>at</strong> LList’s constructor maintains the optional parameter for minimumlist size introduced for Class AList. This is done simply to keep the calls to the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!