publicclassSequentialList { /** * The maximal length of the list. It is a constant. */ privatestaticfinalintMAX_LENGTH=10;
/** * The actual length not exceeding MAX_LENGTH. Attention: length is not only * the member variable of Sequential list, but also the member variable of * Array. In fact, a name can be the member variable of different classes. */ int length;
/** * The data stored in an array. */ int[] data;
/** ********************* * Construct an empty sequential list. ********************* */ publicSequentialList() { length = 0; data = newint[MAX_LENGTH]; }// Of the first constructor
/** ********************* * Construct a sequential list using an array. * * @param paraArray * The given array. Its length should not exceed MAX_LENGTH. For * simplicity now we do not check it. ********************* */ publicSequentialList(int[] paraArray) { this.data = newint[MAX_LENGTH]; this.length = paraArray.length;
// Copy data. for (inti=0; i < paraArray.length; i++) { this.data[i] = paraArray[i]; } // Of for i }// Of the second constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString="";
if (this.length == 0) { return"empty"; } // Of if
for (inti=0; i < this.length - 1; i++) { resultString += data[i] + ", "; } // Of for i
resultString += data[this.length - 1];
return resultString; }// Of toString
/** ********************* * Reset to empty. ********************* */ publicvoidreset() { this.length = 0; }// Of reset
/** ********************* * The entrance of the program. * * @param args * Not used now. ********************* */ publicstaticvoidmain(String args[]) { int[] tempArray = { 1, 4, 6, 9 }; SequentialListtempFirstList=newSequentialList(tempArray); System.out.println("Initialized, the list is: " + tempFirstList.toString()); System.out.println("Again, the list is: " + tempFirstList);
tempFirstList.reset(); System.out.println("After reset, the list is: " + tempFirstList); }// Of main
/** ********************* * Find the index of the given value. If it appears in multiple positions, * simply return the first one. * * @param paraValue The given value. * @return The position. -1 for not found. ********************* */ publicintindexOf(int paraValue) { inttempPosition= -1;
for (inti=0; i < length; i++) { if (data[i] == paraValue) { tempPosition = i; break; } // Of if } // Of for i
/** ********************* * Insert a value to a position. If the list is already full, do nothing. * * @param paraPosition The given position. * @param paraValue The given value. * @return Success or not. ********************* */ publicbooleaninsert(int paraPosition, int paraValue) { if (length == MAX_LENGTH) { System.out.println("List full."); returnfalse; } // Of if
if ((paraPosition < 0) || (paraPosition > length)) { System.out.println("The position " + paraPosition + " is out of bounds."); returnfalse; } // Of if
// From tail to head. The last one is moved to a new position. Because length < // MAX_LENGTH, no exceeding occurs. for (inti= length; i > paraPosition; i--) { data[i] = data[i - 1]; } // Of for i
/** ********************* * Delete a value at a position. * * @param paraPosition The given position. * @return Success or not. ********************* */ publicbooleandelete(int paraPosition) { if ((paraPosition < 0) || (paraPosition >= length)) { System.out.println("The position " + paraPosition + " is out of bounds."); returnfalse; } // Of if
// From head to tail. for (inti= paraPosition; i < length - 1; i++) { data[i] = data[i + 1]; } // Of for i
publicclassSequentialList { /** * The maximal length of the list. It is a constant. */ privatestaticfinalintMAX_LENGTH=10;
/** * The actual length not exceeding MAX_LENGTH. Attention: length is not only the * member variable of Sequential list, but also the member variable of Array. In * fact, a name can be the member variable of different classes. */ int length;
/** * The data stored in an array. */ int[] data;
/** ********************* * Construct an empty sequential list. ********************* */ publicSequentialList() { length = 0; data = newint[MAX_LENGTH]; }// Of the first constructor
/** ********************* * Construct a sequential list using an array. * * @param paraArray The given array. Its length should not exceed MAX_LENGTH. * For simplicity now we do not check it. ********************* */ publicSequentialList(int[] paraArray) { this.data = newint[MAX_LENGTH]; this.length = paraArray.length;
// Copy data. for (inti=0; i < paraArray.length; i++) { this.data[i] = paraArray[i]; } // Of for i }// Of the second constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString="";
if (this.length == 0) { return"empty"; } // Of if
for (inti=0; i < this.length - 1; i++) { resultString += data[i] + ", "; } // Of for i
resultString += data[this.length - 1];
return resultString; }// Of toString
/** ********************* * Reset to empty. ********************* */ publicvoidreset() { this.length = 0; }// Of reset
/** ********************* * Find the index of the given value. If it appears in multiple positions, * simply return the first one. * * @param paraValue The given value. * @return The position. -1 for not found. ********************* */ publicintindexOf(int paraValue) { inttempPosition= -1;
for (inti=0; i < length; i++) { if (data[i] == paraValue) { tempPosition = i; break; } // Of if } // Of for i
return tempPosition; }// Of indexOf
/** ********************* * Insert a value to a position. If the list is already full, do nothing. * * @param paraPosition The given position. * @param paraValue The given value. * @return Success or not. ********************* */ publicbooleaninsert(int paraPosition, int paraValue) { if (length == MAX_LENGTH) { System.out.println("List full."); returnfalse; } // Of if
if ((paraPosition < 0) || (paraPosition > length)) { System.out.println("The position " + paraPosition + " is out of bounds."); returnfalse; } // Of if
// From tail to head. The last one is moved to a new position. Because length < // MAX_LENGTH, no exceeding occurs. for (inti= length; i > paraPosition; i--) { data[i] = data[i - 1]; } // Of for i
data[paraPosition] = paraValue; length++;
returntrue; }// Of insert
/** ********************* * Delete a value at a position. * * @param paraPosition The given position. * @return Success or not. ********************* */ publicbooleandelete(int paraPosition) { if ((paraPosition < 0) || (paraPosition >= length)) { System.out.println("The position " + paraPosition + " is out of bounds."); returnfalse; } // Of if
// From head to tail. for (inti= paraPosition; i < length - 1; i++) { data[i] = data[i + 1]; } // Of for i
length--;
returntrue; }// Of delete
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { int[] tempArray = { 1, 4, 6, 9 }; SequentialListtempFirstList=newSequentialList(tempArray); System.out.println("After initialization, the list is: " + tempFirstList.toString()); System.out.println("Again, the list is: " + tempFirstList);
inttempValue=4; inttempPosition= tempFirstList.indexOf(tempValue); System.out.println("The position of " + tempValue + " is " + tempPosition);
tempValue = 5; tempPosition = tempFirstList.indexOf(tempValue); System.out.println("The position of " + tempValue + " is " + tempPosition);
tempPosition = 2; tempValue = 5; tempFirstList.insert(tempPosition, tempValue); System.out.println( "After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
tempPosition = 8; tempValue = 10; tempFirstList.insert(tempPosition, tempValue); System.out.println( "After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
tempPosition = 3; tempFirstList.delete(tempPosition); System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);
for (inti=0; i < 8; i++) { tempFirstList.insert(i, i); System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList); } // Of for i
tempFirstList.reset(); System.out.println("After reset, the list is: " + tempFirstList);