Question: I need a test class for below java code. Both in junit and testing with hardcoded values. public class LinkedList { Node head; Node tail;
I need a test class for below java code. Both in junit and testing with hardcoded values.
public class LinkedList {
Node head;
Node tail;
LinkedList() {
head = null;
tail = null;
}
public void addHead(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
n.next = head;
head = n;
}
}
public void addTail(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
tail.next = n;
tail = n;
}
}
public int deleteTail() {
Node n = tail;
if (head == null) {
return -1;
}else if (head == tail) {
head = null;
tail = null;
} else {
Node cur = head;
while (cur.getNext() != tail)
cur = cur.next;
tail = cur;
tail.next = null;
}
return n.getData();
}
public int deleteHead() {
Node n=head;
if (head == null) {
return -1;
}else if (head == tail) {
head = null;
tail = null;
}else {
head.next=head;
}
return n.getData();
}
public int count() {
int size=0;
Node n = head;
while(n != null){
n=n.getNext();
size++;
}
return size;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
public void printList() {
if (this.head == null) {
return;
}
// print all nodes
Node tempNode = this.head;
while (tempNode != null) {
System.out.print(tempNode.data + "->");
tempNode = tempNode.next;
}
System.out.println("NULL");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
