60.8k views
3 votes
Do everything as stated below but change the underlying data structure to a double ended doubly-linked list. Data records should be added to the end of the main array (the database) and three double ended doubly linked lists should be maintained ; one each for the ID, LastName and FirstName. The linked lists should (of course) be maintained in order.

You MUST use the driver program I have provided , and make some additions. Remember, you should NOT be permitted to add a record with a duplicate index number. You MUST implement all methods as I have indicated.
Printing out the data in forward order should use the "next" link in each node, printing out in reverse order should employ the "prev" link.
When printig out records, print out ALL fields (ID, first name and last name).
Create a simple database with three fields : ID, LastName, FirstName all as Strings. You should define a class called DataBaseRecord with three private String elements (the three fields), along with the appropriate methods. Here's what mine looked like:
public class DataBaseRecord {
private String ID;
private String first;
private String second;
DataBaseRecord(String a,String b,String c)
{
ID=new String(a);
first=new String(b);
second=new String(c);
}
public String toString()
{
return ID+" "+first+" "+second;
}
}
You should also declare an object called DataBaseArray which is an array of DatabaseRecords. Records of type DataBaseRecord should be added at the end of the DataBaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexArray. This is an array of IndexRecord and is to be implemented as an OrderedArray class like the one we developed in class. That is, insertions must maintain the order in the array, where order is maintained by the key value. Note that this means you need to define a compareTo method in the IndexRecord object.
Iterators
Your IndexArray must implement an interator. An iterator is simply a variable that maintains a current reference into the data structure. In this instance, since IndexArray is a static array, the iterator is just an integer; a pointer into the array. You should implement the following methods:
void iteratorInitFrot - set the iterator to zero
void iteratorInitBack - set the iterator to the last element in the array
boolean hasNext - returns true if iterator<= current last index in the array, false otherwise.
boolean hasPrevious - returns true if iterator>0 , false otherwise
int getNext - returns the where component of the IndexRecord referenced by iterator and then increments the iterator
int getPrevious - returns the where component of the IndexRecord referenced by iterator and then decrements the iterator
Finally, create an class called DataBase. Here's what mine looked like:
public class DataBase {
private DataBaseArray myDB;
private IndexArray ID , First , Last;
public DataBase()
{
myDB=new DataBaseArray(100);
ID = new IndexArray(100);
First=new IndexArray(100);
Last=new IndexArray(100);
}
//other stuff here
}
To get an idea of how the iterators work, here's the code I have for printing out the database in ascending order by first name:
public void ListByFirstAscending()
{
First.iteratorInitFront();
while(First.hasNext())
{
int temp=First.getNext();
System.out.println(myDB.retrieve(temp));
}
}

1 Answer

5 votes

Final answer:

The student's question involves creating a database system with indexed records where the data is stored in a doubly-linked list and indexes are kept in a sorted order. The system requires an implementation of iterator methods to navigate through the database, and it must prevent duplicate index numbers.

Step-by-step explanation:

The student's question pertains to the construction of a simple database using a doubly-linked list to maintain sorted indexes for ID, LastName, and FirstName. Each entry in the database should be a DataBaseRecord instance, with IndexRecord instances in an IndexArray maintaining ordered references to the database records. The database's operations must support iterating through records in both ascending and descending order using an iterator with specific methods like getNext and getPrevious. The unique requirement in this question is to prevent the addition of records with duplicate index numbers. A DataBase class encapsulates the entire functionality.

Class Details

  • The DataBaseRecord class will have three private String fields: ID, first, and second, with a constructor to initialize these fields.
  • The IndexRecord class will hold a key (String) and a where (int) to reference a record's position in the database array, along with compareTo for ordering.
  • The DataBaseArray is an array of DataBaseRecord elements and should be appended at the end.
  • An IndexArray class will manage the ordered IndexRecord arrays, implementing methods required by the iterator protocol.

Iterator Methods

  • iteratorInitFront: Sets the iterator to point to the first element in the IndexArray.
  • iteratorInitBack: Sets the iterator to the last element.
  • hasNext: Checks if there's a next element in the index.
  • hasPrevious: Checks if there's a previous element.
  • getNext: Retrieves the next 'where' value and increments the iterator.
  • getPrevious: Retrieves the previous 'where' value and decrements the iterator.

User Ricardo Veguilla
by
8.1k points