inital commit

This commit is contained in:
gabriel venberg 2021-03-26 22:39:35 -05:00
commit d1948b0e58
67 changed files with 5280 additions and 0 deletions

View file

@ -0,0 +1,193 @@
import java.util.NoSuchElementException;
/**
*
* @author latimer
* @version 1.00 03/03/16
*
* Example of an Iterator for the LinkedPositionalList class.
*
*/
public class Alphabet {
private LinkedPositionalList<Letter> alphabet = null;
//
// Constructor builds a LinkedPositionalList of Letters.
//
public Alphabet( ){
alphabet = new LinkedPositionalList<>();
String alphabetString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Letter nextLetter;
for ( int i = 0; i < alphabetString.length(); i++ )
{
nextLetter = new Letter( alphabetString.charAt(i) );
alphabet.addLast( nextLetter );
}
}
public boolean isVowel( String s )
{
return ( s.equals("A") || s.equals("E") || s.equals("I") || s.equals("O") || s.equals("U") );
}
//
// This method uses an iterator to traverse the list.
//
// Note that here we are using the nested iterator classes from the
// LinkedPositionalList class and not ones for the Alphabet class.
//
@Override
public String toString( )
{
String returnString = "";
Iterator<Letter> listIterator = alphabet.iterator();
while ( listIterator.hasNext() )
returnString += listIterator.next() + " ";
return returnString;
}
//
// The following classes are the nested Iterator classes from
// Code Fragment 7.14
//
// Only the classes for the Position Itertor have bee included.
//
// These fragements have been modified so that they are specific to the
// Alphabet class.
//
// It is necessary to put the iterator code here since we want to create
// iterators specifically for the Alphabet class which is a concrete class
// based on the generic ADT LinkedPositionalList.
//
// Our code needs to have knowledge of Letter.
//
// Generally the Generic placeholders <E> have been replaced with
// concret references <Letter>
// AND
// Call to LinkedPositionalList methods have be replaced by calls using
// the instance reference alphabet
// e.g.
// private Position<Letter> cursor = first();
// became
// private Position<Letter> cursor = alphabet.first();
//
//----- nested PositionIterator class -----
private class PositionIterator implements Iterator<Position<Letter>>{
private Position<Letter> cursor = alphabet.first(); // position of the next element to report
private Position<Letter> recent = null; // position of last reported element
/** Tests whether the iterator has a next object. */
@Override
public boolean hasNext( ) { return ( cursor != null ); }
/** Returns the next position in the iterator. */
@Override
public Position<Letter> next( ) throws NoSuchElementException {
if ( cursor == null ) throw new NoSuchElementException( "nothing left " );
recent = cursor;
cursor = alphabet.after( cursor );
return recent;
}
/** Removes the element returned by most recent call to next. */
@Override
public void remove( ) throws IllegalStateException {
if ( recent == null ) throw new IllegalStateException( "nothing to remove" );
alphabet.remove( recent ); // remove from outer list
recent = null; // do not allow remove again until next is called
}
} //----- end of nested PositionIterator class -----
//----- nested PositionIterable class -----
private class PositionIterable implements Iterable<Position<Letter>>{
@Override
public Iterator<Position<Letter>> iterator( ) { return new PositionIterator( ); }
} //----- end of nested PositionIterable class -----
/** Returns an iterable representation of the list's positions.
* @return */
public Iterable<Position<Letter>> positions( ) {
return new PositionIterable( ); // create a new instace of the inner class
}
//
// Below are the nested iterator classes modified to iterate over just the
// vowels in a the alphabet list
//
// Aside from taking care of name conflicts the only real modification
// that is necessary is in the next() method betweeen the lines:
// recent = cursor;
// << modification goes here
// return recent;
//
// This is because cursor points to the Position that will be returned
// the "next" time next() is called so:
// after saving the value of cursor to recent
// cursor is advanced to the next desired location
// -- you customize where the cursor is advanced to.
// then the value of recent is returned.
//
// You can create additional custom iterators by making additional copies
// of the nested iterator classes, adjusting the names, and modifying
// how the value of cursor is updated on each call to next().
//
//----- nested PositionIterator class -----
private class VowelPositionIterator implements Iterator<Position<Letter>>{
private Position<Letter> cursor = alphabet.first(); // position of the next element to report
private Position<Letter> recent = null; // position of last reported element
/** Tests whether the iterator has a next object. */
@Override
public boolean hasNext( ) { return ( cursor != null ); }
/** Returns the next position in the iterator. */
@Override
public Position<Letter> next( ) throws NoSuchElementException {
// On the first call to next (i.e. when recent == null) you need to //<<< new code
// advance recent until it is pointing to a vowel element. //<<< new code
if ( recent == null ) //<<< new code
{ //<<< new code
while ( cursor != null && !isVowel( cursor.getElement().letter) ) //<<< new code
cursor = alphabet.after( cursor ); //<<< new code
} //<<< new code
if ( cursor == null ) throw new NoSuchElementException( "nothing left " );
recent = cursor;
cursor = alphabet.after( cursor );
// advance cursor to the next vowel
while ( cursor != null && !isVowel( cursor.getElement().letter) )
cursor = alphabet.after( cursor );
return recent;
}
/** Removes the element returned by most recent call to next. */
@Override
public void remove( ) throws IllegalStateException {
if ( recent == null ) throw new IllegalStateException( "nothing to remove" );
alphabet.remove( recent ); // remove from outer list
recent = null; // do not allow remove again until next is called
}
} //----- end of nested PositionIterator class -----
//----- nested PositionIterable class -----
private class VowelPositionIterable implements Iterable<Position<Letter>>{
@Override
public Iterator<Position<Letter>> iterator( ) { return new VowelPositionIterator( ); }
} //----- end of nested PositionIterable class -----
/** Returns an iterable representation of the list's positions.
* @return */
public Iterable<Position<Letter>> vowelPositions( ) {
return new VowelPositionIterable( ); // create a new instace of the inner class
}
}

View file

@ -0,0 +1,10 @@
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Section 7.4.1
*/
public interface Iterable<E> {
Iterator<E> iterator( ); // Returns an iterator of the elements in the collection
}

View file

@ -0,0 +1,20 @@
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Section 7.4
*/
public interface Iterator<E> {
boolean hasNext( ); // Returns true if there is at least one additional
// element in the sequence, and false otherwise.
E next( ); // Returns the next element in the sequence.
void remove( ) throws IllegalStateException;
// Removes from the collection the element returned by
// the most recent call to next( ). Throws an
// IllegalStateException if next has not yet been called,
// or if remove was already called since the most recent
// call to next.
}

View file

@ -0,0 +1,91 @@
/**
*
* @author latimer
* @version 1.00 03/03/16
*
* Example of an Iterator for the LinkedPositionalList class.
*
*/
public class IteratorExample1Client {
public static void main(String[] args) {
//
// Create an instance of the Alphabet class which is
// just a LinkedPositiionalList of Letters.
//
Alphabet alphabet = new Alphabet();
//
// Look at the toString( ) method in the Alphabet class.
// This method uses a iterator to move across the list
//
System.out.println( "Using toString() alphabet = " + alphabet.toString() );
System.out.print("\n");
//
// Let's create an Iterator for the LinkedPositionalList
//
// Decision: Do you want a PositionInterator or an ElementIterator?
//
// Let's pick the PositionIterator.
//
// Since the PositionIterator class is a private class nested in
// the LinkedPositionalList class we cannot call the class methods
// directly.
//
// The PositionalIterable class is also a private class nested in
// the LinkedPositionalList class that we cannot call the class methods
// directly.
//
// Fortunately (or perhaps by design) the Iterable class is a public
// class nexted in LinkedPositionalList class so we can call its
// class methods directly.
//
// This class has one method "positions()" that generally
// returns an object of type IterablePositions<E>
// and in our specific case
// returns an object of type IterablePostions<Letter>
//
// The object returned by "positions()" gives us access to the
// nested private class PositionIterable class that returns an
// PositionIterator object.
//
// The PositionIterator object is the iterator.
//
Iterator<Position<Letter>> letterListIterator = alphabet.positions().iterator();
System.out.print("Using letterListIterator alphabet = ");
while ( letterListIterator.hasNext() )
System.out.print( letterListIterator.next().getElement().toString() + " " );
System.out.print( "\n\n" );
//
// This seems like a lot of work to traverse a list but provided we have
// access to the Iterator classes we can build an iterator to traverse
// the list without any knowledge of how the list is implemented.
//
// Also we have the opportunity to build customized iterators that
// allow us the specify how we will traverse the list.
//
// For example we may want to build an iterator that moves accross all
// of the vowels in the alphabet.
//
// To do this we create a nested interator class inside of our
// Alphabet class.
//
Iterator<Position<Letter>> vowelListIterator = alphabet.vowelPositions().iterator();
System.out.print("Using VowelListIterator alphabet = ");
while ( vowelListIterator.hasNext() )
System.out.print( vowelListIterator.next().getElement().toString() + " " );
System.out.print( "\n\n" );
}
}

View file

@ -0,0 +1,25 @@
/**
*
* @author latimer
* @version 1.00 03/03/16
*
* A simple Letter class that holds a single char element as a upper case String.
*
*/
public class Letter {
String letter;
public Letter() { }
public Letter( String letter ) { this.letter = letter; }
public Letter( char letter ) {
String newLetter = "" + letter;
this.letter = newLetter.toUpperCase();
}
public String toString( ){
return letter;
}
}

View file

@ -0,0 +1,310 @@
import java.util.NoSuchElementException;
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Code Fragements 7.9, 7.10, 7.11, 7.12 & 7.14
*
* toString method added by Latimer
*/
/** Implementation of a positional list stored as a doubly linked list. */
public class LinkedPositionalList<E> implements PositionalList<E> {
//----- nested Node class -----
private static class Node<E> implements Position<E> {
private E element; // reference to the element stored at this node
private Node<E> prev; // reference to the prevous node in the list
private Node<E> next; // reference to the subsequent node in the list
public Node( E e, Node<E> p, Node<E> n ){
element = e;
prev = p;
next = n;
}
@Override
public E getElement( ) throws IllegalStateException
{
if ( next == null )
throw new IllegalStateException( "Position no longer valid." );
return element;
}
public Node<E> getPrev( )
{
return prev;
}
public Node<E> getNext( )
{
return next;
}
public void setElemetn( E e )
{
element = e;
}
public void setPrev( Node<E> p )
{
prev = p;
}
public void setNext( Node<E> n )
{
next = n;
}
} //----- end of nested Node class -----
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Code Fragement 7.14
*/
//----- nested PositionIterator class -----
private class PositionIterator implements Iterator<Position<E>>{
private Position<E> cursor = first(); // position of the next element to report
private Position<E> recent = null; // position of last reported element
/** Tests whether the iterator has a next object. */
@Override
public boolean hasNext( ) { return ( cursor != null ); }
/** Returns the next position in the iterator. */
@Override
public Position<E> next( ) throws NoSuchElementException {
if ( cursor == null ) throw new NoSuchElementException( "nothing left " );
recent = cursor;
cursor = after( cursor );
return recent;
}
/** Removes the element returned by most recent call to next. */
@Override
public void remove( ) throws IllegalStateException {
if ( recent == null ) throw new IllegalStateException( "nothing to remove" );
LinkedPositionalList.this.remove( recent ); // remove from outer list
recent = null; // do not allow remove again until next is called
}
} //----- end of nested PositionIterator class -----
//----- nested PositionIterable class -----
private class PositionIterable implements Iterable<Position<E>>{
@Override
public Iterator<Position<E>> iterator( ) { return new PositionIterator( ); }
} //----- end of nested PositionIterable class -----
/** Returns an iterable representation of the list's positions.
* @return */
public Iterable<Position<E>> positions( ) {
return new PositionIterable( ); // create a new instance of the inner class
}
//----- nested ElementIterator class -----
/* This class adapts the iteration produced by positions( ) to return elements. */
private class ElementIterator implements Iterator<E> {
Iterator<Position<E>> posIterator = new PositionIterator( );
@Override
public boolean hasNext( ) { return posIterator.hasNext( ); }
@Override
public E next( ) { return posIterator.next( ).getElement( ); } // return element
@Override
public void remove( ) { posIterator.remove( ); }
}
/** Returns an iterator of the elements stored in the list */
public Iterator<E> iterator( ) { return new ElementIterator( ); }
// instance variables of the LinkedPositionalList
private Node<E> header; // header sentinel
private Node<E> trailer; // trailer sentinel
private int size = 0; // number of elements in the list
public LinkedPositionalList( ){
header = new Node<>( null, null, null ); // create header
trailer = new Node<>( null, header, null ); // create trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
// private utilities
/**
* @param p position to validate
* @return node if position is valid
* @throws IllegalArgumentException if p no longer in list or p is not a position
*/
private Node<E> validate( Position<E> p ) throws IllegalArgumentException {
if( !(p instanceof Node )) throw new IllegalArgumentException( "Invalid p" );
Node<E> node = ( Node<E> ) p; // safe cast
if ( node.getNext() == null )
throw new IllegalArgumentException( "p is no longer in the list" );
return node;
}
/**
* @param node to be returned as position if not header or trailer
* @return position of node
*/
private Position<E> position( Node<E> node ){
if ( node == header || node == trailer )
return null;
return node;
}
// public accessor methods
/**
* @return number of elements in linked list
*/
@Override
public int size( ){
return size;
}
/**
* @return true if list is empty, false other wise
*/
@Override
public boolean isEmpty( ){
return ( size == 0 );
}
/**
* @return the first position in linked list (null if empty).
*/
@Override
public Position<E> first( ){
return position( header.getNext( ) );
}
/**
* @return the last position in linked list (null if empty).
*/
@Override
public Position<E> last( ){
return position( trailer.getPrev( ) );
}
/**
* @param p position to get position immediately before
* @return position before p
* @throws IllegalArgumentException if p not valid
*/
@Override
public Position<E> before( Position<E> p ) throws IllegalArgumentException{
Node<E> node = validate( p );
return position( node.getPrev( ) );
}
/**
* @param p position to get immediately after
* @return position after p
* @throws IllegalArgumentException if p not valid
*/
@Override
public Position<E> after( Position<E> p ) throws IllegalArgumentException{
Node<E> node = validate( p );
return position( node.getNext( ) );
}
// private utilities
/**
* @param e element to be added
* @param pred node to add element after
* @param succ node to add element before
* @return position of newly added element
*/
private Position<E> addBetween(E e, Node<E> pred, Node<E> succ ){
Node<E> newest = new Node<>(e, pred, succ); // create and link new node
pred.setNext(newest);
succ.setPrev(newest);
size++;
return newest;
}
// public update methods
/**
* @param e element to be added just after header
* @return position of newly added element
*/
@Override
public Position<E> addFirst(E e) {
return addBetween( e, header, header.getNext() );
}
/**
* @param e element to be added just before trailer
* @return position of newly added element
*/
@Override
public Position<E> addLast( E e ){
return addBetween(e, trailer.getPrev( ), trailer );
}
/**
*
* @param p position to add element before
* @param e element to be added
* @return position of newly added element
* @throws IllegalArgumentException if p is not valid
*/
@Override
public Position<E> addBefore( Position<E> p, E e ) throws IllegalArgumentException {
Node<E> node = validate( p );
return addBetween(e, node.getPrev( ), node );
}
/**
* @param p position to add element after
* @param e element to be added
* @return position of newly added element
* @throws IllegalArgumentException if p is not valid
*/
@Override
public Position<E> addAfter( Position<E> p, E e ) throws IllegalArgumentException {
Node<E> node = validate( p );
return addBetween(e, node, node.getNext( ) );
}
/**
* @param p position of node to update
* @param e new element for node
* @return old element in node before update
* @throws IllegalArgumentException if p not valid
*/
@Override
public E set( Position<E> p, E e ) throws IllegalArgumentException {
Node<E> node = validate( p );
E answer = node.getElement( );
node.setElemetn( e );
return answer;
}
/**
* @param p position to be removed
* @return element that was removed
* @throws IllegalArgumentException if p not valid
*/
public E remove( Position<E> p ) throws IllegalArgumentException {
Node<E> node = validate( p );
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext( successor );
successor.setPrev( predecessor );
size--;
E answer = node.getElement( );
node.setElemetn( null );
node.setNext( null );
node.setPrev( null );
return answer;
}
}

View file

@ -0,0 +1,15 @@
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Code Fragement 7.7
*/
public interface Position<E> {
/**
* Returns the element stored at this position.
*
* @return the stored element
* @thorws IllegalStateExceptoin if position no longer valid
*/
E getElement( ) throws IllegalStateException;
}

View file

@ -0,0 +1,89 @@
/**
* Data Structures & Algorithms 6th Edition
* Goodrick, Tamassia, Goldwasser
* Code Fragement 7.8
*/
/**
* An interface for positional lists.
*/
public interface PositionalList<E> {
/**
* @return the number of elements in the list.
*/
int size( );
/**
* @return true if the list is empty.
*/
boolean isEmpty( );
/**
* @return the first Position in the list ( or null, if empty ).
*/
Position<E> first( );
/**
* @return the last Position in the list ( or null, if empty ).
*/
Position<E> last( );
/**
* @param p a position in the list,
* @return position immediately before p ( or null if p is first ).
* @throws IllegalArgumentException if p is not in list.
*/
Position<E> before( Position<E> p ) throws IllegalArgumentException;
/**
* @param p a position in the list,
* @return position immediately after p ( or null if p is last ).
* @throws IllegalArgumentException if p is not in list.
*/
Position<E> after( Position<E> p ) throws IllegalArgumentException;
/**
* @param e element to be inserted at front of list
* @return position of inserted element
*/
Position<E> addFirst( E e );
/**
* @param e element to be inserted at back of list
* @return position of inserted element
*/
Position<E> addLast( E e );
/**
* @param p position to be inserted before
* @param e element to be inserted before position p
* @return position of e
* @throws IllegalArgumentException if p not in list
*/
Position<E> addBefore( Position<E> p, E e ) throws IllegalArgumentException;
/**
* @param p position to be inserted after
* @param e element to be inserted after position p
* @return position of e
* @throws IllegalArgumentException if p not in list
*/
Position<E> addAfter( Position<E> p, E e ) throws IllegalArgumentException;
/**
* @param p position to store element at
* @param e element to be stored at p
* @return the element that is replaced
* @throws IllegalArgumentException if p is not in list
*/
E set( Position<E> p, E e ) throws IllegalArgumentException;
/**
* @param p position of element to be removed
* @return removed element
* @throws IllegalArgumentException if p not in list
*/
E remove( Position<E> p ) throws IllegalArgumentException;
}