// DoubleLinkedListADT
public interface DoubleLinkedListADT
public void initializeList();
public boolean isEmptyList();
public T front();
public T back();
public int length();
public void print();
public void reversePrint();
public boolean search(T searchItem);
public void insertNode(T insertItem);
public void deleteNode(T deleteItem);
public String toString();
public String recursiveToString();
public String backwardsString();
public String recursiveBackwardsString();
public boolean equals(Object o);
public void copy(DoubleLinkedList
public void reversedCopy(DoubleLinkedList
}
//DoubleLinkedList class
import javax.lang.model.util.ElementScanner14;
public class DoubleLinkedList
// Double linked list node class
public class DoubleLinkedListNode
T info;
DoubleLinkedListNode
DoubleLinkedListNode
public DoubleLinkedListNode() {
info = null;
next = null;
back = null;
}
public String toString() {
return info.toString();
}
}
protected int count; // number of nodes
protected DoubleLinkedListNode
protected DoubleLinkedListNode
//Default constructor
public DoubleLinkedList() {
first = null;
last = null;
count = 0;
}
public void initializeList() {
first = null;
last = null;
count = 0;
}
public boolean isEmptyList() {
return (first == null);
}
public T front() {
return first.info;
}
public T back() {
return last.info;
}
public int length() {
return count;
}
public void print() {
DoubleLinkedListNode
current = first;
while (current != null) { //while more data to print
System.out.print(current.info + " ");
current = current.next;
}
}
public void reversePrint() {
DoubleLinkedListNode
current = last;
System.out.print("[tail] - ");
while (current != null) { //while more data to print
System.out.print(current.info + " - ");
current = current.back;
}
System.out.print(" [head]");
}
public boolean search(T searchItem) {
DoubleLinkedListNode
current = first;
while (current != null)
if (current.info == searchItem)
return true;
else
current = current.next;
return false;
}
public void insertNode(T insertItem) {
boolean found;
DoubleLinkedListNode
DoubleLinkedListNode
// Set up node to be inserted
DoubleLinkedListNode
newNode.info = insertItem;
newNode.next = null;
newNode.back = null;
// if the list is empty, newNode is the only node
if (first == null) {
first = newNode;
last = newNode;
count++;
} else {
found = false;
current = first;
// search the list
while (current != null && !found) {
Comparable
if (temp.compareTo(insertItem) >= 0)
found = true;
else {
trailCurrent = current;
current = current.next;
}
}
// insert new node before first
if (current == first) {
first.back = newNode;
newNode.next = first;
first = newNode;
count++;
} else {
// insert newNode between trailCurrent and current
if (current != null) {
trailCurrent.next = newNode;
newNode.back = trailCurrent;
newNode.next = current;
current.back = newNode;
} else {
// insert new node after last
trailCurrent.next = newNode;
newNode.back = trailCurrent;
last = newNode;
}
count++;
}
}
}
public void deleteNode(T deleteItem) {
DoubleLinkedListNode
DoubleLinkedListNode
boolean found;
if (first == null)
System.err.println("Cannot delete from an empty list.");
// if node to be deleted is the first node
else if (first.info.equals(deleteItem)) {
current = first;
first = first.next;
if (first != null)
first.back = null;
else
last = null;
count--;
} else {
found = false;
current = first;
// search the list
while (current != null && !found) {
Comparable
if (temp.compareTo(deleteItem) >= 0)
found = true;
else
current = current.next;
}
if (current == null)
System.out.println("The item to be deleted is not in the list.");
else if (current.info.equals(deleteItem)) {
trailCurrent = current.back;
trailCurrent.next = current.next;
if (current.next != null)
current.next.back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
} else
System.out.println("The item to be deleted is not in list.");
}
}
public String toString() {
String output = "[head] - ";
DoubleLinkedListNode
while(current != null) {
output += current.info + " - ";
current = current.next;
}
return output + "[tail]";
}
public String recursiveToString() {
return "[head] - " + nodeToString(first) + "[tail]";
}
public String nodeToString(DoubleLinkedListNode
if(node == null) return "";
else return node.info + " - " + nodeToString(node.next);
}
public String backwardsString() {
String output = "[tail] - ";
DoubleLinkedListNode
while(current != null) {
output += current.info + " - ";
current = current.back;
}
return output + "[head]";
}
public String recursiveBackwardsString() {
return "[tail] - " + backwardsNodeToString(last) + "[head]";
}
public String backwardsNodeToString(DoubleLinkedListNode
if(node == null) return "";
else return node.info + " - " + backwardsNodeToString(node.back);
}
public boolean equals(Object o) {
if(o instanceof DoubleLinkedList) {
DoubleLinkedList otherList = (DoubleLinkedList) o;
DoubleLinkedListNode
DoubleLinkedListNode
while(current != null && compare != null ) {
if(current.equals(compare)) {
current = current.next;
compare = compare.next;
}
else
return false;
}
return current == compare;
}
else
return false;
}
public void copy(DoubleLinkedList
initializeList();
System.gc();
DoubleLinkedListNode
while(current != null) {
insertNode(current.info);
current = current.next;
}
}
public void reversedCopy(DoubleLinkedList
initializeList(); // Clear destination list contents
System.gc(); // Clean up unlinked data
DoubleLinkedListNode
DoubleLinkedListNode
// Avoiding .insertNode. to circumvent list sorting
while(copyItem != null) {
DoubleLinkedListNode
current.info = copyItem.info; // Copy info into new empty node
if(trailCurrent != null) { // If destination list already has nodes, link between current and previous
trailCurrent.next = current;
current.back = trailCurrent;
}
if(first == null) first = current; // If list is empty, set first to new node
trailCurrent = current; // Move trail up one node
last = current; // Set end of list to most recent node
copyItem = copyItem.back; // Prepare next node for copying (backwards traversal)
}
}
}
//Mainother
public class Mainother {
public static void main(String[] args) {
DoubleLinkedList
DoubleLinkedList
String item;
list_1.insertNode("John");
list_1.insertNode("Ann");
list_1.insertNode("Paul");
list_1.insertNode("Joshua");
list_1.insertNode("Will");
list_1.insertNode("Emma");
list_1.insertNode("Peter");
list_1.insertNode("Linda");
list_1.insertNode("Joan");
list_1.insertNode("David");
list_1.insertNode("Miriam");
list_1.insertNode("Leah");
list_1.insertNode("Jane");
System.out.println("Inserted in first list the names: John, Ann, Paul, Joshua, Will, Emma, Peter, Linda, Joan, David, Miriam, Leah, Jane");
System.out.println("(Testing toString) First list sorted is: " + list_1);
System.out.println("(Testing recursive toString) First list sorted is: " + list_1.recursiveToString());
System.out.println("(Testing backwardsString) First list reversed is: " + list_1.backwardsString());
System.out.println("(Testing recursive backwardsString) First list reversed is: " + list_1.recursiveBackwardsString());
System.out.println("Will copy in second list only names that don't start with J.List one destroyed.");
while (!list_1.isEmptyList()) {
item = list_1.front();
list_1.deleteNode(item);
if(item.charAt(0) != 'J')
list_2.insertNode(item);
}
System.out.println("Second list should hold names that don't start with J (sorted): " + list_2);
System.out.println("First list should be empty. Nothing printed. ");
list_1.print();
System.out.print("(Testing equals) ");
if(list_1.equals(list_2))
System.out.println("The 2 lists are equals");
else
System.out.println("The 2 lists are NOT equals");
System.out.print("(Testing copy) ");
list_1.copy(list_2);
System.out.println("First list after copy is: " + list_1);
System.out.print("(Testing reversed copy) ");
list_1.reversedCopy(list_2);
System.out.println("First list as the copy of the second list reversed is: " + list_1);
}
}
//Integer Client
import java.util.Scanner;
public class IntegerClient {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
DoubleLinkedList
DoubleLinkedList
int item;
list_1.insertNode(2);
list_1.insertNode(4);
list_1.insertNode(23);
list_1.insertNode(4);
list_1.insertNode(21);
list_1.insertNode(46);
list_1.insertNode(13);
list_1.insertNode(13);
list_1.insertNode(6);
list_1.insertNode(12);
list_1.insertNode(6);
list_1.insertNode(4);
list_1.insertNode(15);
System.out.println("Numbers inserted in list 1 are: 2, 4, 23, 4, 21, 46, 13, 13, 6, 12, 6, 13, 15");
System.out.println("List 1 sorted is: " + list_1);
System.out.println();
//Testing toString methods
System.out.println("Testing toString; List 1 is: " + list_1.toString());
System.out.println("Testing recursive toString; List 1 is: " + list_1.recursiveToString());
System.out.println();
//List size test
System.out.println("Testing size; List 1 size is: " + list_1.length());
//testing backwards toString
System.out.println("Testing backwards toString; List 1 backwards is: " + list_1.backwardsString() + "\nTesting recursive backwardstostring; List 1 backwards is; " + list_1.recursiveBackwardsString());
System.out.println();
//Testing delete integer from list
System.out.println("Testing delete and copy into new list: ");
int delete = getInt(sc, "Please enter an integer to delete: ");
while(!list_1.isEmptyList()) {
item = list_1.front();
list_1.deleteNode(item);
if (item != delete) {
list_2.insertNode(item);
}
}
System.out.println("List 1 is: " + list_1);
System.out.println("List 2 without " + delete +" is: " + list_2);
System.out.println();
//Testing equals method
System.out.println("Testing equals ");
if (list_1.equals(list_2))
System.out.println("List 1 and List 2 are equal");
else
System.out.println("List 1 and List 2 are not equal");
System.out.println();
//Testing copy methods
System.out.println("Testing copy: ");
list_1.copy(list_2);
System.out.println("List 1 after copy is: " + list_1);
list_1.reversedCopy(list_2);
System.out.println("List 1 after reversed copy is: " + list_1);
System.out.println();
//Testing reversePrint method
System.out.println("Testing reversePrint: \nList 2: " + list_2 + "\nList 2 reversed: ");
list_2.reversePrint();
System.out.println();
//Testing search method
System.out.println("Testing search in list 2: ");
int searchInt = getInt(sc, "Please enter an integer to search for: ");
boolean found = list_2.search(searchInt);
if (found)
System.out.println(searchInt + " was foudn in list 2.");
else
System.out.println(searchInt + " was not found in list 2.");
//
}
//Get valid integer from user
public static int getInt(Scanner sc, String prompt) {
System.out.println(prompt);
while (!sc.hasNextInt()) {
sc.next();
System.out.println("Invalid integer, please try again.");
}
return sc.nextInt();
}
}