} // end method InsertAtBack // remove first node from List public object RemoveFromFront() { if (IsEmpty()) { throw new EmptyListException(name); } object removeItem = firstNode.Data; // retrieve data // reset firstNode and lastNode references if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.Next; } return(removeItem); // return removed data } // end method RemoveFromFront
} // end method IsEmpty // output List contents public void Display() { if (IsEmpty()) { Console.WriteLine("Empty " + name); } // end if else { Console.Write("The " + name + " is: "); ListNode current = firstNode; // output current node data while not at end of list while (current != null) { Console.Write(current.Data + " "); current = current.Next; } // end while Console.WriteLine("\n"); } // end else } // end method Display
private string name; // string like "list" to display #endregion Fields #region Constructors // construct empty List with specified name public List( string listName ) { name = listName; firstNode = lastNode = null; }
// constructor to create ListNode that refers to dataValue // and refers to next ListNode in List public ListNode( object dataValue, ListNode nextNode ) { Data = dataValue; Next = nextNode; }
// remove first node from List public object RemoveFromFront() { if ( IsEmpty() ) throw new EmptyListException( name ); object removeItem = firstNode.Data; // retrieve data // reset firstNode and lastNode references if ( firstNode == lastNode ) firstNode = lastNode = null; else firstNode = firstNode.Next; return removeItem; // return removed data }
// remove last node from List public object RemoveFromBack() { if ( IsEmpty() ) throw new EmptyListException( name ); object removeItem = lastNode.Data; // retrieve data // reset firstNode and lastNode references if ( firstNode == lastNode ) firstNode = lastNode = null; else { ListNode current = firstNode; // loop while current node is not lastNode while ( current.Next != lastNode ) current = current.Next; // move to next node // current is new lastNode lastNode = current; current.Next = null; } // end else return removeItem; // return removed data }
// Insert object at front of List. If List is empty, // firstNode and lastNode will refer to same object. // Otherwise, firstNode refers to new node. public void InsertAtFront( object insertItem ) { if ( IsEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else firstNode = new ListNode( insertItem, firstNode ); }
// Insert object at end of List. If List is empty, // firstNode and lastNode will refer to same object. // Otherwise, lastNode's Next property refers to new node. public void InsertAtBack( object insertItem ) { if ( IsEmpty() ) firstNode = lastNode = new ListNode( insertItem ); else lastNode = lastNode.Next = new ListNode( insertItem ); }
private string name; // string like "list" to display // construct empty List with specified name public List(string listName) { name = listName; firstNode = lastNode = null; } // end constructor
} // end default constructor // constructor to create ListNode that refers to dataValue // and refers to next ListNode in List public ListNode(object dataValue, ListNode nextNode) { Data = dataValue; Next = nextNode; } // end constructor
// constructor to create ListNode that refers to dataValue // and refers to next ListNode in List public ListNode(double dataValue, ListNode nextNode) { Data = dataValue; Next = nextNode; }