Modify this code to 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.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DataBase {
private DataBaseArray dbArr;
private IndexArray firstIA, lastIA, IDIA;
private DeleteStack deleteStack;
private Scanner scan;
private String nld = "n-----------------------------n";
private String dnl = "-----------------------------n";
//Default Constructor
public DataBase() {
this.dbArr = new DataBaseArray(100);
this.firstIA = new IndexArray(100, false);
this.lastIA = new IndexArray(100, false);
this.IDIA = new IndexArray(100, true);
this.deleteStack = new DeleteStack(100);
readInDataFromFile();
scan = new Scanner(System.in);
}
//Constructor for custom size
public DataBase(int maxSize) {
this.dbArr = new DataBaseArray(maxSize);
this.firstIA = new IndexArray(maxSize, false);
this.lastIA = new IndexArray(maxSize, false);
this.IDIA = new IndexArray(maxSize, true);
this.deleteStack = new DeleteStack(maxSize);
readInDataFromFile();
scan = new Scanner(System.in);
}
//Read in the initial records
public void readInDataFromFile() {
File dbData = new File("dbData.txt");
Scanner scan = new Scanner(System.in); //Have to initialize to something for compilation
try {
scan = new Scanner(dbData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String ID = "";
String fname = "";
String lname = "";
String[] values;
while(scan.hasNextLine()) {
values = scan.nextLine().split(",");
lname = values[0];
fname = values[1];
ID = values[2];
this.insertIt(ID, fname, lname);
}
}
//Ask user to specify ID, delete that record
public void deleteIt() {
String ID = "";
System.out.println("nDELETING" + nld);
System.out.println("Please enter the ID of the record to be deleted: ");
ID = this.scan.nextLine();
if (this.IDIA.searchByKey(ID) == -1) {
System.out.println(dnl + "Record not found, please try again." + nld);
return;
}
this.delete(ID);
System.out.println(dnl + "Record successfully deleted" + nld);
}
//Ask user to specify ID, return data of that record
public void findIt() {
String ID = "";
System.out.println("nFINDING" + nld);
System.out.println("Please enter the ID of the record to be found: ");
ID = this.scan.nextLine();
int iaIndexOfRecord = this.IDIA.searchByKey(ID);
if (iaIndexOfRecord == -1) {
System.out.println(dnl + "Record not found, please try again." + nld);
return.
Modify this code to change the underlying data structure to .pdf
1. Modify this code to 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.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DataBase {
private DataBaseArray dbArr;
private IndexArray firstIA, lastIA, IDIA;
private DeleteStack deleteStack;
private Scanner scan;
private String nld = "n-----------------------------n";
private String dnl = "-----------------------------n";
//Default Constructor
public DataBase() {
this.dbArr = new DataBaseArray(100);
this.firstIA = new IndexArray(100, false);
this.lastIA = new IndexArray(100, false);
this.IDIA = new IndexArray(100, true);
this.deleteStack = new DeleteStack(100);
readInDataFromFile();
scan = new Scanner(System.in);
}
//Constructor for custom size
public DataBase(int maxSize) {
this.dbArr = new DataBaseArray(maxSize);
this.firstIA = new IndexArray(maxSize, false);
this.lastIA = new IndexArray(maxSize, false);
this.IDIA = new IndexArray(maxSize, true);
this.deleteStack = new DeleteStack(maxSize);
readInDataFromFile();
scan = new Scanner(System.in);
}
//Read in the initial records
public void readInDataFromFile() {
File dbData = new File("dbData.txt");
Scanner scan = new Scanner(System.in); //Have to initialize to something for compilation
2. try {
scan = new Scanner(dbData);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String ID = "";
String fname = "";
String lname = "";
String[] values;
while(scan.hasNextLine()) {
values = scan.nextLine().split(",");
lname = values[0];
fname = values[1];
ID = values[2];
this.insertIt(ID, fname, lname);
}
}
//Ask user to specify ID, delete that record
public void deleteIt() {
String ID = "";
System.out.println("nDELETING" + nld);
System.out.println("Please enter the ID of the record to be deleted: ");
ID = this.scan.nextLine();
if (this.IDIA.searchByKey(ID) == -1) {
System.out.println(dnl + "Record not found, please try again." + nld);
return;
}
this.delete(ID);
System.out.println(dnl + "Record successfully deleted" + nld);
}
//Ask user to specify ID, return data of that record
public void findIt() {
String ID = "";
System.out.println("nFINDING" + nld);
System.out.println("Please enter the ID of the record to be found: ");
ID = this.scan.nextLine();
int iaIndexOfRecord = this.IDIA.searchByKey(ID);
if (iaIndexOfRecord == -1) {
System.out.println(dnl + "Record not found, please try again." + nld);
return;
}
System.out.println(this.dbArr.getRecord(this.IDIA.getWhereByKey(ID)));
3. System.out.println(dnl + "Record found" + nld);
}
//Ask user to add a new record
public void addIt() {
String ID = "";
String first = "";
String last = "";
System.out.println("nADDING" + nld);
System.out.println("Please enter the ID of the record: ");
ID = this.scan.nextLine();
if (this.IDIA.searchByKey(ID) != -1) {
System.out.println(dnl + "ID already in use, please try again." + nld);
return;
}
System.out.println("Please enter the first name of the record: ");
first = this.scan.nextLine();
System.out.println("Please enter the last name of the record: ");
last = this.scan.nextLine();
this.insertIt(ID, first, last);
System.out.println(dnl + "Record successfully added" + nld);
}
//Insert record into database
public void insertIt(String ID, String fname, String lname) {
if (IDIA.searchByKey(ID) == -1) {
DataBaseRecord record = new DataBaseRecord(ID, fname, lname);
int where = -1;
if (this.deleteStack.isEmpty()) {
this.dbArr.addRecord(record);
} else {
where = this.deleteStack.pop();
this.dbArr.setRecord(record, where);
}
if (where < 0) {
where = this.dbArr.getSize() - 1;
}
this.firstIA.add(new IndexRecord<String>(fname, where));
this.lastIA.add(new IndexRecord<String>(lname, where));
this.IDIA.add(new IndexRecord<String>(ID, where));
}
}
//Delete a record by passing the key of the record to be deleted
public <K> void delete(K key) {
4. int whereToDelete = IDIA.getWhereByKey(key);
IDIA.deleteIndex(IDIA.searchByKey(key));
firstIA.deleteIndex(firstIA.searchByWhere(whereToDelete));
lastIA.deleteIndex(lastIA.searchByWhere(whereToDelete));
deleteStack.push(whereToDelete);
}
//Debugging method to print entire database
public void dumpDataBase() {
for (int i = 0; i < dbArr.getSize(); i++) {
System.out.println(dbArr.getRecord(i));
}
}
//List the database by passing the desired IndexArray and order to list in
public void list(IndexArray ia, boolean asc) {
System.out.println("nLISTING" + nld);
if (asc) {
ia.iteratorInitFront();
while(ia.hasNext()) {
System.out.println(this.dbArr.getRecord(ia.getNext()));
}
} else {
ia.iteratorInitBack();
while(ia.hasPrevious()) {
System.out.println(this.dbArr.getRecord(ia.getPrevious()));
}
}
System.out.println(dnl + "Records successfully listed" + nld);
}
//Driver methods for listing
public void ListByFirstAscending() {
this.list(firstIA, true);
}
public void ListByFirstDescending() {
this.list(firstIA, false);
}
public void ListByLastAscending() {
this.list(lastIA, true);
}
public void ListByLastDescending() {
this.list(lastIA, false);
}
public void ListByIDAscending() {
5. this.list(IDIA, true);
}
public void ListByIDDescending() {
this.list(IDIA, false);
}
}
public class DataBaseArray {
private DataBaseRecord[] data;
private int maxSize;
private int currSize;
//Constructor to specify size of database
public DataBaseArray(int maxSize) {
this.data = new DataBaseRecord[maxSize];
this.maxSize = maxSize;
this.currSize = 0;
}
//Inserts a record into the desired index
public void setRecord(DataBaseRecord dbr, int index) {
this.data[index] = dbr;
}
//Inserts a record at the end of the array
public void addRecord(DataBaseRecord dbr) {
if (currSize != maxSize) {
this.data[currSize] = dbr;
this.currSize++;
}
}
//Returns the current size of the array
public int getSize() {
return this.currSize;
}
//Returns the record at the specified index
public DataBaseRecord getRecord(int index) {
return data[index];
}
}
public class DataBaseRecord {
private String ID;
private String first;
private String last;
//Constructor to pass the fields to
public DataBaseRecord(String ID, String first, String last) {
6. this.ID = ID;
this.first = first;
this.last = last;
}
//Getter methods if needed in future
public String getID() {
return ID;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
//To string to print out the record
@Override
public String toString() {
return this.ID + " " + this.first + " " + this.last;
}
}
public class DeleteStack {
private int[] stack;
private int currSize;
//Constructor to pass the max size to
public DeleteStack(int maxSize) {
this.stack = new int[maxSize];
this.currSize = 0;
}
//Pushes database index onto array
public void push(int index) {
this.stack[this.currSize++] = index;
}
//Returns the database index at the end of the array and decrements the end-of-array pointer
public int pop() {
return this.stack[--this.currSize];
}
//Returns if the stack is empty
public boolean isEmpty() {
return this.currSize == 0;
}
}
public class IndexRecord<K extends Comparable<K>> {
7. K key;
int where;
//Constructor to pass key and where to
public IndexRecord(K key, int where) {
this.key = key;
this.where = where;
}
//Compares key of this record and another record
public int compareTo(IndexRecord otherRecord) {
return this.key.compareTo((K) otherRecord.key);
}
}
This next part is the driver
public class Driver
{
public static void main(String[] args)
{
/*The following declaration declares a data structure that will change from one assignment to the
next. For example, you will need to implement
* the following as a doubly linked list, as well as a tree.
*/
DataBase d=new DataBase();
int response;
Scanner keyboard=new Scanner(System.in);
/* Read the data into the database from the external disk file here
* IMPORTANT: duplicate ID numbers should not be added. Disregard
* the entire record for duplicate IDs
*/
do
{
System.out.println(" 1 Add a new student");
System.out.println(" 2 Delete a student");
System.out.println(" 3 Find a student by ID");
System.out.println(" 4 List students by ID increasing");
System.out.println(" 5 List students by first name increasing");
System.out.println(" 6 List students by last name increasing");
System.out.println(" 7 List students by ID decreasing");
System.out.println(" 8 List students by first name decreasing");
System.out.println(" 9 List students by last name decreasing");
System.out.println(" ");
System.out.println(" 0 End");
response=keyboard.nextInt();
8. switch (response)
{
case 1: d.addIt(); //Note: if the user enters an ID already in use, issue a warning and return to the
menu
break;
case 2: d.deleteIt(); //Note: output either "Deleted" or "ID not Found" and return to menu
break;
case 3: d.findIt(); //Note: output the entire record or the message "ID not Found" and return to
menu
break;
case 4: d.ListByIDAscending();
break;
case 5: d.ListByFirstAscending();
break;
case 6: d.ListByLastAscending();
break;
case 7: d.ListByIDDescending();
break;
case 8: d.ListByFirstDescending();
break;
case 9: d.ListByLastDescending();
break;
default:
}
} while (response!=0);
}
}