Question: please create a program that tests the linkedlist I put below: public class LinkedList { private class Node { private T data; private Node link;

please create a program that tests the linkedlist I put below:

public class LinkedList

{

private class Node

{

private T data;

private Node link;

public Node( )

{

data = null;

link = null;

}

public Node(T newData, Node linkValue)

{

data = newData;

link = linkValue;

}

} //End of Node inner class

private Node head, tail;

private int size;

public LinkedList( )

{

head = tail = null;

}

// Adds a node at the start of the list with the specified data.

// The added node will be the first node in the list.

public void addFront(T itemData)

{

head = new Node(itemData, head);

if (tail == null)

tail = head;

size++;

}

// Removes the head node and returns true if the

// list contains at least one node. Returns false if the list is empty.

public boolean deleteFront( )

{

if (isEmpty( ))

return false;

head = head.link;

size--;

if (head == null)

tail = null;

return true;

}

// Analogous to addFront, but add at back.

public void addBack(T itemData)

{

if (isEmpty( ))

addFront(itemData);

else

{

tail.link = new Node(itemData, null);

tail = tail.link;

size++;

}

}

// Analogous to deleteFront, but delete at back.

public boolean deleteBack( )

{

if (isEmpty( ))

return false;

if (head.link == null)

return deleteFront( );

else

{

Node current = head;

while (current.link != tail)

current = current.link;

current.link = null;

tail = current;

size--;

return true;

}

}

// Returns the number of nodes in the list.

public int size( )

{

return size;

}

// Finds the first node containing the target item, and returns a

// reference to that node. If target is not in the list,

// null is returned.

private Node find(T target)

{

Node position = head;

T itemAtPosition;

while (position != null) {

itemAtPosition = position.data;

if (itemAtPosition.equals(target))

return position;

position = position.link;

}

return null;

}

// Checks whether list contains item. Must call the private find method.

public boolean contains(T item) {

return find(item) != null;

}

public void outputList() {

Node position = head;

while (position != null) {

System.out.println(position.data);

position = position.link;

}

}

public boolean isEmpty() {

return head == null;

}

public void makeEmpty() {

head = tail = null;

}

// For two lists to be equal they must contain the same data items in

//the same order. The equals method of T is used to compare data items.

public boolean equals(Object otherObject) {

if (otherObject == null) {

return false;

} else if (getClass() != otherObject.getClass()) {

return false;

} else {

LinkedList otherList = (LinkedList) otherObject;

if (size() != otherList.size()) {

return false;

}

Node position = head;

Node otherPosition = otherList.head;

while (position != null) {

if (!(position.data.equals(otherPosition.data))) {

return false;

}

position = position.link;

otherPosition = otherPosition.link;

}

return true; //no mismatch was not found

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!