Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a JUnit test class that tests all the functionality of the linked list and iterators. Here is the LinkedList and Iterator class: public class

Write a JUnit test class that tests all the functionality of the linked list and iterators. Here is the LinkedList and Iterator class:

public class LinkedList { Node itsFirstNode; Node itsLastNode; private int size;

public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; }

public Iterator getIterator() { return new Iterator(this); } public void add(T element) {

Node node = new Node(element);

if (itsFirstNode == null) { itsFirstNode = node; itsLastNode = node; } else { itsLastNode.setNextNode(node); node.setPriorNode(itsLastNode);//prior node set from new node to last node itsLastNode = node; } size++; } public void add(T element, int index) {

int counter = 0; Node newNode = new Node(element); Node current = itsFirstNode;

while (current.getNextNode() != null ) {

if (counter == index - 1 ) break;

current = current.getNextNode(); counter++; } newNode.setNextNode(current.getNextNode()); Node temp=current.getNextNode();//temp node is next node of current node temp.setPriorNode(newNode); current.setNextNode(newNode); newNode.setPriorNode(current); size++; } public T get(int index) {

int counter = 0; Node current = itsFirstNode;

while (current.getNextNode() != null ) {

if (counter == index) break;

current = current.getNextNode(); counter++; } return current.getData(); }

/** * A doubly-linked list * @param element * @return if element is in the list result is true, false if not */ public boolean contains(T element) {

Node head = itsFirstNode; boolean res = false;

if (itsFirstNode == null) return res;

else {

while (head != null) {

if(head.getData() == element){ res = true; break; } head.setNextNode(head.getNextNode()); } } return res; }

/** * @return the index of the element if it is in the list, -1 if not found */ public int indexOf(T element) {

Node head = itsFirstNode; int index = 0;

if(itsFirstNode == null) return -1;

else {

while (head != null) {

if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); index++; } } return index; }

/** * We cannot get Java iterator to an user defined object unless implementing * iterable interface, therefore the return type is Node in order to * return reference to current node * @return an Iterator at the location of the element if it is in the list * and returns the null reference if the element is not found * @param element */ public Node iteratorAt(T element) {

Node head = itsFirstNode;

if (itsFirstNode == null) return null;

else {

while (head != null) {

if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); } } return head; }

public String toString() {

String returnVal = ""; Node current = itsFirstNode;

if (size != 0 ) {

while (current.getNextNode() != null ) { returnVal += current.toString(); returnVal += " "; current = current.getNextNode(); } returnVal += current.toString(); } return returnVal; } // end toString

class Node { private T data; private Node itsNext; private Node itsPrior;

public Node(T data) { itsNext = null; itsPrior = null; this.data = data; }

public T getData() { return this.data; }

public Node getNextNode() { return itsNext; }

public Node getPriorNode() { return itsPrior; }

public void setNextNode(Node next) { itsNext = next; }

public void setPriorNode(Node prior) { itsPrior=prior; }

public String toString() { return data.toString(); }

} // end of Node

} // end LinkedList class

----------------------------------------------------------------------------------------------------------------

public class Iterator {

private LinkedList myList; private LinkedList.Node myCurrentNode;

public Iterator(LinkedList list) { myList = list; myCurrentNode = myList.itsFirstNode; }

/** * @return true if there is a "next" element, otherwise returns false */ public boolean hasNext() { if (myCurrentNode != null) return true; return false; }

/** * @return true if there is a "prior" element, otherwise returns false */ public boolean hasPrior() { if (myCurrentNode.getPriorNode() != null) return true; return false; }

/** * @return the "next" node (really the current one) * and moves the Iterator forward by one node */ public T next() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getNextNode(); return data; }

/** * @return the "prior" node (really the current one) * and moves the Iterator backward by one node */ public T prior() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getPriorNode(); return data; }

/** * Sets this iterator to point to the last Node in the list */ public void setToEnd() { while (myCurrentNode.getNextNode() != null) myCurrentNode = myCurrentNode.getNextNode(); }

} //end Iterator class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

Would you accept the position? Why or why not?

Answered: 1 week ago