0% found this document useful (0 votes)
46 views

Assignment 5 Source Class:: Matthew Holtzem && Joseph Ezeh

The document describes a DoubleLinkedList class that implements a doubly linked list. It contains methods like initializeList(), isEmptyList(), front(), back(), length(), print(), reversePrint(), search(), insertNode(), deleteNode(), toString(), recursiveToString(), backwardsString(), recursiveBackwardsString(), equals(), copy(), and reversedCopy() to perform common doubly linked list operations like inserting, deleting, searching, printing the list in forward and backward order, comparing lists for equality, and copying lists.

Uploaded by

Joe Ezeh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Assignment 5 Source Class:: Matthew Holtzem && Joseph Ezeh

The document describes a DoubleLinkedList class that implements a doubly linked list. It contains methods like initializeList(), isEmptyList(), front(), back(), length(), print(), reversePrint(), search(), insertNode(), deleteNode(), toString(), recursiveToString(), backwardsString(), recursiveBackwardsString(), equals(), copy(), and reversedCopy() to perform common doubly linked list operations like inserting, deleting, searching, printing the list in forward and backward order, comparing lists for equality, and copying lists.

Uploaded by

Joe Ezeh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Matthew Holtzem && Joseph Ezeh

Assignment 5
Source Class:
public class DoubleLinkedList<T> implements DoubleLinkedListADT<T> {
//Double linked list node class

public class DoubleLinkedListNode<T> {

T info;
DoubleLinkedListNode<T> next;
DoubleLinkedListNode<T> back;

public DoubleLinkedListNode() {
info = null;
next = null;
back = null;
}

public String toString() {


return info.toString();
}
}
protected int count; //number of nodes
protected DoubleLinkedListNode<T> first; //reference to first node
protected DoubleLinkedListNode<T> last; //reference to last node

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<T> current;
current = first;
while (current != null) {
System.out.print(current.info + " ");
current = current.next;
}
}

public void reversePrint() {


DoubleLinkedListNode<T> current;
current = last;
while (current != null) {
System.out.print(current.info + " ");
current = current.back;
}
}

public boolean search(T searchItem) {


boolean found;
found = false;
DoubleLinkedListNode<T> current;
current = first;

while (current != null && !found) {


Comparable<T> temp = (Comparable<T>) current.info;

if (temp.compareTo(searchItem) >= 0) {
found = true;
} else {
current = current.next;
}
}
if (found) {
found = current.info.equals(searchItem);
}

return found;
}

public void insertNode(T insertItem) {


DoubleLinkedListNode<T> current;
DoubleLinkedListNode<T> trailCurrent = null;
DoubleLinkedListNode<T> newNode;

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;

while(current != null && !found){


Comparable <T> temp = (Comparable <T>) current.info;

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;

while (current != null && !found){


Comparable<T> temp = (Comparable<T>) current.info;

if(temp.compareTo(deleteItem) >= 0)
found = true;
else
current = current.next;
}

if ((current == null) || !(current.info.equals(deleteItem)))


System.out.println("The item to be deleted is not in the list");
else{
trailCurrent = current.back;
trailCurrent.next = 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

You might also like