In: Computer Science
A circular doubly-linked list
.(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have:
• A private instance variable for the current node
• A getCurrent() method that returns a reference to the current node • A method to insert a node before the current node
• A method to delete the current node
• A method to advance the current node to the next node in the list • A method to check whether the list is empty
• A method to print the list, starting with the current node
.(b) Write tests for these methods. Why did you choose these test cases? (Think about any edge cases as you do this.) Did you get what you expected?
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute
it.
*******************************************************************************/
// class job which is actually a node in cll(circular linked
list)
class job{
public int data;
public job next;
public job prev;
public job(int data){
this.data=data;
this.next=null;
this.prev=null;
}
}
// circular linked list class
class circularlinkedlist{
private job current;
// constructor
public circularlinkedlist(int data){
this.current = new job(data);
}
// return current node function
public job getCurrent(){
return current;
}
// method to insert node into circularlinkedlist before current
node
public void insert(int data){
job node = new job(data);
if(current.prev==null){
node.prev = current;
node.next=current;
current.next=node;
current.prev=node;
}else{
node.next = current;
node.prev = current.prev;
current.prev.next=node;
current.prev=node;
}
}
// deleting the current node and making the current node as current
node next node
public void deletecurrent(){
job temp = current;
current.prev.next=current.next;
current.next=current.prev.next;
current = current.next;
}
// moving current to next node
public void moveforward(){
current=current.next;
}
// returns the true if cll is empty otherwise false
public boolean isempty(){
if(current==null){
return true;
}else{
return false;
}
}
// diaplays the each node data value
public void display(){
if(!this.isempty()){
job temp = current.next;
System.out.println(current.data);
while(temp!=current){
System.out.println(temp.data);
temp=temp.next;
}
}
}
}
public class Main
{
public static void main(String[] args) {
circularlinkedlist cll = new
circularlinkedlist(1);
/*
testcase -1 having 1 node in cll
checking what isempty() returns
*/
System.out.println("testcase-1 : "+cll.isempty());
/*
testcase -2 given node having data =1 as current node
checking what does getCurrent() returns
*/
System.out.println("testcase-2 : "+cll.getCurrent().data);
/*
testcase -3 inserting 2,3,4 into cll and then calling display() to
check
whether they are inserted or not
*/
cll.insert(2);
cll.insert(3);
cll.insert(4);
System.out.println("testcase-3
");
cll.display();
/*
testcase -4 deleting the current node having data=1 and
making next node of current node which is 2 as current node
and display cll to check whether it still has 1
*/
cll.deletecurrent();
System.out.println("testcase-4 :
"+cll.getCurrent().data);
cll.display();
/*
testcase -5 moving the current node which is actually 2 to forward
which
is 3 and checking it
*/
cll.moveforward();
System.out.println("testcase-5 :
"+cll.getCurrent().data);
/*
final testcase checking all the elements present in cll
*/
System.out.println("final testcase");
cll.display();
}
}