package csc241.samples.intList; /** ~mohammad/public_html/classes/csc241/intList/intList.java This an ADT for List class that holds integers. This implementation uses a header to reference the start of the list as well as an iterator. It also provides methods that allow us to manipulate and traverse the list. **/ public class intList { node header_; //head of the list node current_; //iterator for the list /** * Construct the list **/ public intList( ) { header_ = null; current_ = null; } /** * Test for empty. * @return true if empty, false otherwise. **/ public boolean isEmpty( ) { return header_ == null; } /** * Make the list logically empty. **/ public void makeEmpty( ) { header_ = null; current_ = null; } /** * Insert after the current position. * current is set to the inserted node on success. * @param x the item to insert. * @exception Exception if the current position is null. */ public void insert( int x ) throws Exception { if( current_ == null ) throw new Exception( "Insertion error; invalid current" ); node newNode = new node( x, current_.next_ ); current_.next_ = newNode; current_ = newNode; } /** * Insert at the beginning of the list. * current is set to the inserted node. * @param x the item to insert. */ public void insertAtBeginning( int x ) { header_= new node( x, header_ ); current_ = header_; } /** * Set the current position to the first node containing an item. * current is unchanged if x is not found. * @param x the item to search for. * @return true if the item is found, false otherwise. */ public boolean find( int x ) { node itr = header_; while( itr != null) { if(itr.element_ == x) { current_ = itr; return true; } itr = itr.next_; } return false; } /** * Remove the first occurrence of an item. * current is set to the node at the beginning of the list on success; * remains unchanged otherwise. * @param x the item to remove. * @exception Exception if the item is not found. */ public void remove( int x ) throws Exception { if( header_ == null ) throw new Exception( "Remove fails; empty list" ); if (header_.element_ == x) { header_ = header_.next_; current_ = header_; return; } node itr = header_; while( itr.next_ != null) { if (itr.next_.element_ == x ) { itr.next_ = itr.next_.next_; current_ = header_; return; } itr = itr.next_; } throw new Exception( "Remove fails; element not in list" ); } /** * Test if the current position references a valid list item. * @return true if the current position is not null */ public boolean validCurrent( ) { return current_ != null; } /** * Return the item stored in the current_ position. * @return the stored item or throw an exception if invalid current * @exception Exception if the current position is null. */ public int retrieve( ) throws Exception { if( current_ == null ) throw new Exception( "Retrieve error; invalid current" ); return current_.element_; } /** * Set the current_ position to the first node in the list. * This operation is valid for empty lists. */ public void first( ) { current_ = header_; } /** * Advance the current_ position to the next node in the list. * If the current_ position is null, then do nothing. * No exceptions are thrown by this routine because in the * most common use (inside a for loop), this would require the * programmer to add an unnecessary try/catch block. */ public void advance( ) { if( current_ != null ) current_ = current_.next_; } /** * return a string representing the list content **/ public String dump () { String text; if (header_ == null) text = new String ("List is Empty"); else { text= new String("head-> "); node temp; int last; for (temp=header_;temp != null;temp=temp.next_) text=text.concat(temp.element_ + "-> "); } return text; } }