Assignment 5 Source Class:: Matthew Holtzem && Joseph Ezeh
Assignment 5 Source Class:: Matthew Holtzem && Joseph Ezeh
Assignment 5
Source Class:
public class DoubleLinkedList<T> implements DoubleLinkedListADT<T> {
//Double linked list node class
T info;
DoubleLinkedListNode<T> next;
DoubleLinkedListNode<T> back;
public DoubleLinkedListNode() {
info = null;
next = null;
back = null;
}
public T front() {
return (first.info);
}
public T back() {
return (last.info);
}
if (temp.compareTo(searchItem) >= 0) {
found = true;
} else {
current = current.next;
}
}
if (found) {
found = current.info.equals(searchItem);
}
return found;
}
boolean found;
newNode = new DoubleLinkedListNode();
newNode.info = insertItem;
newNode.next = null;
newNode.back = null;
if (first == null) {
first = newNode;
last = newNode;
count++;
}
else {
found = false;
current = first;
if(temp.compareTo(insertItem) >=0)
found = true;
else{
trailCurrent = current;
current = current.next;
}
}
if (current == first){
first.back = newNode;
newNode.next = first;
first = newNode;
count ++;
}
else{
if (current != null){
trailCurrent.next = newNode;
newNode.back = trailCurrent;
newNode.next = current;
current.back = newNode;
}
else {
trailCurrent.next = newNode;
newNode.back = trailCurrent;
last = newNode;
}
count ++;
}
}
}
public void deleteNode(T deleteItem){
DoubleLinkedListNode<T> current;
DoubleLinkedListNode<T> trailCurrent;
boolean found;
if (first == null)
System.err.println("Cannot delete from an empty list");
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;
if(temp.compareTo(deleteItem) >= 0)
found = true;
else
current = current.next;
}
if (current.next != null)
current.next.back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
}
}
}
public String toString(){
String result = "";
DoubleLinkedListNode<T> current;
current = first;
while (current!=null){
result = result + " - " + current.info;
current = current.next;
}
return result;
}
public String recursiveToString(){
DoubleLinkedList<T> list = new DoubleLinkedList<T>();
list.copy(this);
return toString(list);
}
//Helper
private String toString(DoubleLinkedList<T> list){
if(list.first==null)
return "";
else{
String result = list.first.toString();
list.deleteNode(list.first.info);
return result +" - " + toString(list);
}
}
public String backwardsString(){
String result = "";
DoubleLinkedListNode<T> current;
current = last;
while (current!=null){
result = result + " - " + current.info;
current = current.back;
}
return result;
}
public String recursiveBackwardsString(){
DoubleLinkedList<T> list = new DoubleLinkedList<T>();
list.copy(this);
return backString(list);
}
//Helper
private String backString(DoubleLinkedList<T> list){
if(list.last==null)
return "";
else{
String result = list.last.toString();
list.deleteNode(list.last.info);
return result + " - " + backString(list);
}
}
public boolean equals(DoubleLinkedList<T> list){
if(isEmptyList())
return list.isEmptyList();
else if(count != list.count)
return false;
else{
DoubleLinkedListNode<T> current;
DoubleLinkedListNode<T> listCurrent;
current = first;
listCurrent = list.first;
while(!(current == null)&&!(listCurrent == null)){
if(current.info != listCurrent.info){
return false;
}
else {
current = current.next;
listCurrent = listCurrent.next;
}
}
return true;
}
}
public void copy(DoubleLinkedList<T> otherList) {
initializeList();
DoubleLinkedListNode<T> current;
current = otherList.first;
while (current != null) {
insertNode(current.info);
current = current.next;
}
}
public void reversedCopy(DoubleLinkedList<T> otherList){
initializeList();
DoubleLinkedListNode<T> current;
DoubleLinkedListNode<T> trailCurrent;
current = otherList.last;
while(current!=null){
DoubleLinkedListNode<T> newNode;
newNode = new DoubleLinkedListNode();
newNode.info = current.info;
newNode.next = null;
newNode.back = null;
if (first == null){
first = newNode;
last = newNode;
count ++;
current = current.back;
}
else{
trailCurrent = current.back;
last.next = newNode;
newNode.back = last;
last = newNode;
current = current.back;
}
}
}
}
Output:
Inserted in first list the names: John, Ann, Paul, Joshua, Will, Emma, Peter, Linda, Joan, David, MIriam,
Leah, Jane
(Testing toString) First list sorted is: [head] - Ann - David - Emma - Jane - Joan - John - Joshua - Leah -
Linda - Miriam - Paul - Peter - Will - [tail]
(Testing recursive toString) First list sorted is: [head] - Ann - David - Emma - Jane - Joan - John - Joshua -
Leah - Linda - Miriam - Paul - Peter - Will - [tail]
(Testing backwards) First list reversed (print) is: [tail] - Will - Peter - Paul - Miriam - Linda - Leah - Joshua -
John - Joan - Jane - Emma - David - Ann - [head]
(Testing recursive backwards) First list reversed (print) is: [tail] - Will - Peter - Paul - Miriam - Linda - Leah
- Joshua - John - Joan - Jane - Emma - David - Ann - [head]
Will copy in second list only names that don't start with J. List one destroyed.
Second list should hold names that don't start with J (sorted): - Ann - David - Emma - Leah - Linda -
Miriam - Paul - Peter - Will
First list should be empty. Nothing printed.
(Testing equals) The 2 lists are NOT equals
(Testing copy) First list after copy is: - Ann - David - Emma - Leah - Linda - Miriam - Paul - Peter - Will
(Testing reversed copy) First list as the copy of the second list reversed is: - Will - Peter - Paul - Miriam -
Linda - Leah - Emma - David - Ann