Question

In: Computer Science

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...

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?

Solutions

Expert Solution

/******************************************************************************

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();
   }
}


Related Solutions

Consider a “Remove” operation from a doubly linked list other than front and end. Explain the...
Consider a “Remove” operation from a doubly linked list other than front and end. Explain the implementation of “Remove” operation using a drawing by showing appropriate links. Using the explanation of (a) write the statements to implement the “Remove” operation. Using (b) show that the complexity of “Remove” operation is O(1).
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
class SLinkedList: """Singly linked list with access to front and end, and with stored size. """...
class SLinkedList: """Singly linked list with access to front and end, and with stored size. """ #-------------------------- nested _Node class -------------------------- class _Node: __slots__ = '_element', '_next' # streamline memory usage def __init__(self, element, next): self._element = element self._next = next #------------------------------- queue methods ------------------------------- def __init__(self): """Create an empty list.""" self._head = None self._tail = None self._size = 0 def __len__(self): """Return the number of elements in the list.""" return self._size def isEmpty(self): """Return True if the list is...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT