MyLinkedList - import java.util.Stack; import...

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

View Full Document Right Arrow Icon
import java.util.Stack; import java.util.LinkedList; import java.util.List; /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType extends Comparable<? super AnyType>> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { clear( ); } /** * Change the size of this collection to zero. */ public void clear( ) { beginMarker = new Node<AnyType>( null, null, null ); endMarker = new Node<AnyType>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; } /** * Returns the number of items in this collection. * @return the number of items in this collection. */ public int size( ) { return theSize; } public boolean isEmpty( ) { return size( ) == 0; } /** * Adds an item to this collection, at the end. * @param x any object. * @return true. */ public boolean add( AnyType x ) { add( size( ), x ); return true; } /** * Adds an item to this collection, at specified position. * Items at or after that position are slid one position higher. * @param x any object. * @param idx position to add at.
Background image of page 1

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

View Full DocumentRight Arrow Icon
* @throws IndexOutOfBoundsException if idx is not between 0 and size(), inclusive. */ public void add( int idx, AnyType x ) { addBefore( getNode( idx, 0, size( ) ), x ); } /** * Adds an item to this collection, at specified position p. * Items at or after that position are slid one position higher. * @param p Node to add before. * @param x any object. * @throws IndexOutOfBoundsException if idx is not between 0 and size(), inclusive. */ private void addBefore( Node<AnyType> p, AnyType x ) { Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p ); newNode.prev.next = newNode; p.prev = newNode; theSize++; } /** * Returns the item at position idx. * @param idx the index to search in. * @throws IndexOutOfBoundsException if index is out of range. */ public AnyType get( int idx ) { return getNode( idx ).data; } /** * Changes the item at position idx. * @param idx the index to change. * @param newVal the new value. * @return the old value. * @throws IndexOutOfBoundsException if index is out of range. */ public AnyType set( int idx, AnyType newVal ) { Node<AnyType> p = getNode( idx ); AnyType oldVal = p.data; p.data = newVal; return oldVal; } /** * Gets the Node at position idx, which must range from 0 to size( ) - 1. * @param idx index to search at.
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.

This note was uploaded on 09/16/2009 for the course CS 3345 taught by Professor Ozbirn during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 9

MyLinkedList - import java.util.Stack; import...

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