// // COMP102 // Example X: // // Paul E. Dunne 8/11/99 // public class Records { //****************************************************** // Embed a RecordCell Class as earlier. * //****************************************************** private class RecordCell { private Object Key; private Object Datum; private RecordCell Link; // private RecordCell(Object KeyVal, Object head, RecordCell next_cell) { Key=KeyVal; Datum = head; Link=next_cell; } } //****************************************************** // Records Fields * //****************************************************** private RecordCell Head; private RecordCell Tail; private int CellCount; // The number of list cells in this instance. //****************************************************** // Constructor * //****************************************************** public Records() { Head = null; Tail = null; CellCount=0; // Initiates the empty record set; } //****************************************************** // Instance Methods * //****************************************************** // // Test if this instance is Empty // public boolean IsEmpty() { return (CellCount==0); // `Safer' than testing for null reference. } // //************************************************************ // Add a new RecordCell as the first cell in this * //************************************************************ // public void AddHead ( Object Datum, Object KeyVal ) { if (IsEmpty()) // If it's currently empty; { Head = new RecordCell(KeyVal,Datum,Head); Tail = null; // The link is still `null' CellCount=1; // and this list now contains 1 cell. } else // If it's not empty { Head = new RecordCell(KeyVal,Datum,Head); // Add the new head Datum, Tail = Head.Link; // Reset the `Tail' of the list. CellCount++; // and this list now has 1 extra cell. }; } // //************************************************************************ // Remove the RecordCell at the start of this instance * //************************************************************************ // public void RemoveHead() { if (CellCount<=1) // There is an argument for raising { // an exception if CellCount=0 // however, we adopt the convention // that RemoveHead() applied to // empty leaves empty. Head = null; CellCount=0; } else { Head = Tail; // Tail is never the null reference here. CellCount--; // and this instance has one fewer record. if (CellCount==1) Tail=null; // If there's only a single cell then its Link field // must point to empty (i.e. null reference) else Tail = Head.Link; // Otherwise we can update Tail without any problem. }; } // //********************************************************************** // Obtain the Object in the Datum field of the RecordCell at * // the start of this instance. * //********************************************************************** // public Object GetHeadDatum() { if (!(CellCount==0)) // If there's anything to return return Head.Datum; // then return it. else return null; // otherwise return a null reference. } // //********************************************************************** // Obtain the Object in the Key field of the RecordCell at * // the start of this instance. * //********************************************************************** // public Object GetHeadKey() { if (!(CellCount==0)) // If there's anything to return return Head.Key; // then return it. else return null; // otherwise return a null reference. } // //********************************************************************** // Obtain the (reference) to the Records for the Link field of the * // RecordCell at the start of this instance. // This method will not change the current instantiation //********************************************************************** // public Records GetTail() { Records temporary= new Records(); temporary.Head=Head; temporary.Tail=Tail; temporary.CellCount = CellCount; temporary.RemoveHead(); return temporary; } }