/** * A class containing list of records with insert/delete * and search funcionality. * * @author Vivek Rane. TA for 22C:21, Spring 2006 * Modified by Sriram Pemmaraju, Jan 17th 2007 */ public class RecordDB { private static int maxRecords = 20; private int numRecords = 0; private Record recordList[] = new Record[maxRecords]; /** * Inserts a record into the ordered array in a sorted fashion. * @param rec - The record to be inserted. */ public void insert (Record rec) { // Check for overflow if (numRecords == maxRecords-1) { return; // Increase the size of the array maybe } // Find the position to insert in int i; for (i=0; i rec.ssn) break; } // Shift all records one space down for (int j=numRecords-1; j>=i; j--) { recordList[j+1] = recordList[j]; } // Insert the new record recordList[i] = rec; // Increment current number of employees in the list numRecords++; printAll(); } /** * Deletes a record from the list based on the key (ssn). * Performs a linear scan of the array of records until * a record is found with the given ssn * @param ssn */ public void delete (long ssn) { int i; for (i=0; i ssn) { System.out.println("Record not found: " + ssn); return; } // SSN found in slot i if (recordList[i].ssn == ssn) { break; } } // Shift all records that are beyond slot i, // to the left by one slot for (int j=i; j ssn) { last = current-1; continue; } if (recordList[current].ssn < ssn) { first = current+1; continue; } } } /** * Prints the list of records to the standard output; */ private void printAll() { System.out.println("---------------------------"); for (int i=0; i