Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on implementing a doubly circular linkedlist using DLLNode. I may u

ID: 3853811 • Letter: I

Question

I am working on implementing a doubly circular linkedlist using DLLNode. I may use some help for implementing doubly circular linkedlist. Help lol.

here is DLLNode class

public class DLLNode<T> {

   private DLLNode<T> back;

   private DLLNode<T> forward;

   private T info;

   public DLLNode(T info) {

       this.info = info;

       this.back = null;

       this.forward = null;

   }

   public void setInfo(T info)

   // Sets info of this LLNode.

   {

       this.info = info;

   }

   public T getInfo()

   // Returns info of this LLONode.

   {

       return info;

   }

   public void setBack(DLLNode<T> back)

   // Sets back link of this DLLNode.

   {

       this.back = back;

   }

   public DLLNode<T> getBack()

   // Returns back link of this DLLNode.

   {

       return back;

   }

   public void setForward(DLLNode<T> forward)

   // Sets forward link of this DLLNode;

   {

       this.forward = forward;

   }

   public DLLNode<T> getForward()

   // Returns forward link of this DLLNode.

   {

       return forward;

   }

}

And here are methods to be implemented, name of class doesn't matter. This class implements the

int size();

// Returns the number of elements on this list.

void add(T element);

// Adds element to this list.

boolean remove (T element);

// Removes an element e from this list such that e.equals(element)

// and returns true; if no such element exists, returns false.

  

boolean contains (T element);

// Returns true if this list contains an element e such that

// e.equals(element); otherwise, returns false.

  

T get(T element);

// Returns an element e from this list such that e.equals(element);

// if no such element exists, returns null.

  

String toString();

// Returns a nicely formatted string that represents this list.

  

void reset();

// Initializes current position for an iteration through this list,

// to the first element on this list.

T getNext();

// Preconditions: The list is not empty

// The list has been reset

// The list has not been modified since the most recent reset

//

// Returns the element at the current position on this list.

// If the current position is the last element, then it advances the value

// of the current position to the first element; otherwise, it advances

// the value of the current position to the next element.

/**

T getPrevious();  

* Returns the element at the current position on this list.

   * If the current position is the first element, then it advances the value

   * of the current position to the last element; otherwise, it advances

   * the value of the current position to the previous element.

   *

   * @return the info of type T

   */

Explanation / Answer

/* Sorry didn't had enough time to add comments*/

import java.util.Scanner;

public class DLLImplementation<T> {

   private DLLNode<T> head;
   private int size = 0;
   private DLLNode<T> current;

   public boolean isEmpty() {
       return (size == 0);
   }

   public int size() {
       return size;
   }

   public void add(T val) {
       DLLNode<T> newNode = new DLLNode<T>(val);

       if (head != null) {
           newNode.setForward(head);
           newNode.setBack(head.getBack());
           head.getBack().setForward(newNode);
           head.setBack(newNode);
       } else {
           head = newNode;
           head.setBack(head);
           head.setForward(head);
       }

       size++;
   }

   public boolean remove(T value) {

       if (isEmpty()) {
           throw new IllegalStateException("List is empty");
       }

       DLLNode<T> node = get(value);
       if (node == null)
           return false;

       if (node == head) {
           head = head.getForward();
       }
       node.getForward().setBack(node.getBack());
       node.getBack().setForward(node.getForward());
       node.setForward(null);
       node.setBack(null);
      
       size--;

       if (size == 0) {
           head = null;
       }
       return true;
   }
   public void reset(){
       this.current = head;
   }
   public boolean contains(T value) {
       return get(value) != null;
   }
  
   private DLLNode<T> get(T value) {      
       DLLNode<T> currentNode = head;      
        int i = 0;       
        while (i < size) {           
            if (currentNode.getInfo().equals(value)) {               
                System.out.println(currentNode.getInfo()+ " was found");
                return currentNode;
            }           
            currentNode = currentNode.getForward();
            i++;
        }       
        return null;
    }

   /* Function to display status of list */
   public T getNext(){
       if(null==current)
           System.out.println("Please reset the list or add elements");
       else{      
           current=current.getForward();
           return current.getInfo();
       }
       return null;
   }

   public T getPrevious(){
       if(null==current)
           System.out.println("Please reset the list or add elements");
       else{      
           current=current.getBack();
           return current.getInfo();
       }
       return null;
   }

   public String toString()
   {
       System.out.print(" Circular Doubly Linked List = ");
       DLLNode<T> ptr = head;
       if (size == 0) {
           System.out.print("empty ");
           return"";
       }

       if (head.getForward() == head){
           System.out.print(head.getInfo()+ " <-> "+ptr.getInfo()+ " ");
           return "";
       }

       System.out.print(head.getInfo()+ " <-> ");
       ptr = head.getForward();

       while (ptr.getForward() != head ){
           System.out.print(ptr.getInfo()+ " <-> ");
           ptr = ptr.getForward();
       }

       System.out.print(ptr.getInfo()+ " <-> ");
       ptr = ptr.getForward();
       System.out.print(ptr.getInfo()+ " ");

       return "";

   }
   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       /* Creating object of linkedList */
       DLLImplementation<Integer> list = new DLLImplementation<Integer>();
       System.out.println("Circular Doubly Linked List Test ");
       char ch;
       /* Perform list operations */
       do  
       {
           System.out.println(" Circular Doubly Linked List Operations ");

           System.out.println("1. Insert");
           System.out.println("2. Get Next");
           System.out.println("3. Get Previous");
           System.out.println("4. Delete");
           System.out.println("5. Check empty");
           System.out.println("6. Get Size");
           System.out.println("7. Reset");
           int choice = scan.nextInt();
           switch (choice)
           {
           case 1 :
               System.out.println("Enter integer element to insert");
               list.add(scan.nextInt() );                   
               break;                        
           case 2:
               System.out.println(list.getNext());
               break;
           case 3:
               System.out.println(list.getPrevious());
               break;
           case 4 :
               System.out.println("Enter value");
               int p = scan.nextInt() ;
               list.remove(p);
               break;   
           case 5 :
               System.out.println("Empty status = "+ list.isEmpty());
               break;          
           case 6 :
               System.out.println("Size = "+ list.size() +" ");
               break;
           case 7 :
               list.reset();
               break;
           default :
               System.out.println("Wrong Entry ");
               break;
           }
           /* Display List */

           list.toString();
           System.out.println(" Do you want to continue (Type y or n) ");
           ch = scan.next().charAt(0);    

       } while (ch == 'Y'|| ch == 'y');  
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote