Question

In: Computer Science

Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template......

Java program to implement circular linked list. NO COPY PASTE ANSWERS plz follow the given template...

public class CircularLinkedList  {

    
    private Node tail;
    private int size;
    
    public CircularLinkedList() {
        tail= null;
        size = 0;

    }



     public int size(){
        return size;
    }

 
    public boolean isEmpty()
    {
        return size==0;
    }
    
        
        //if list is not empty return the first element
     
    public E first() {
        if (isEmpty()) return null;
        //code here
        return 0;
    }
  

    
        
        //if list not empty return last element
     
    public E last() {
        if (isEmpty()) return null;
        return tail.getElement();
    }

    /*
        move tail to the next node
     */
    public void rotate()
    {
    }

    /*
        add element to the first of the linked list
        increase the size
     */
    public void addFirst(E e)
    {
        
    }

    /*
        add element to the end of the linked list
        increase size
     */
    public void addLast(E e)
    {
    }

    /*
        take out the first element
        decrease the size
        return first element or null


     */
    public E removeFirst()
    {
        return null;
    }
public String toString()
{
    String s;
    Node<E> n;
    if ( tail == null )
        return "null";
    s = "[";
    n = tail.getNext();
    if (n==null)
    {
        return s+ "empty list]";
    }
    int iter =0;
//change the while loop below for circular list instead of singly linked list
    while (n!=null&&iter<2*size)
    {
        iter++;
        s = s+ n.getElement();
        if (n.getNext()!=null) s = s + ", ";
        n = n.getNext();
    }
    return s+"]";

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

implements....

public class Node {
    // Instance variables:
    //element = data
    private E element;
    private Node next;
    /** Creates a node with null references to its element and next node. */
    public Node() {
        this(null, null);
    }
    /** Creates a node with the given element and next node. */
    public Node(E e, Node n) {
        element = e;
        next = n;
    }
    // Accessor methods:
    public E getElement() {
        return element;
    }
    public Node getNext() {
        return next;
    }
    // Modifier methods:
    public void setElement(E newElem) {
        element = newElem;
    }
    public void setNext(Node newNext) {
        next = newNext;
    }
}
 

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Node.java

package c15;
public class Node<E> {
// Instance variables:
//element = data
private E element;
private Node next;
/** Creates a node with null references to its element and next node. */
public Node() {
this(null, null);
}
/** Creates a node with the given element and next node. */
public Node(E e, Node n) {
element = e;
next = n;
}
// Accessor methods:
public E getElement() {
return element;
}
public Node getNext() {
return next;
}
// Modifier methods:
public void setElement(E newElem) {
element = newElem;
}
public void setNext(Node newNext) {
next = newNext;
}
}

CircularLinkedList.java

package c15;
public class CircularLinkedList<E> {


   private Node tail;
   private Node head;
   private int size;

   public CircularLinkedList() {
       tail= null;
       size = 0;

   }

   public int size(){
       return size;
   }


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


   //if list is not empty return the first element

           public E first() {
       if (isEmpty()) return null;
       return (E) head.getElement();
           }


           //if list not empty return last element

           public E last() {
               if (isEmpty()) return null;
               return (E) tail.getElement();
           }

           /*
move tail to the next node
           */
           public void rotate(){
               tail = tail.getNext();
               head = head.getNext();
           }

           /*
add element to the first of the linked list
increase the size
           */
           public void addFirst(E e)
           {
               Node<E> newNOde =new Node<>();
               newNOde.setElement(e);

               newNOde.setNext(head);
               head = newNOde;
               if(isEmpty()) {
                   tail = head;
               }
               tail.setNext(head);
               size++;
           }

           /*
add element to the end of the linked list
increase size
           */
           public void addLast(E e){
               Node<E> newNOde =new Node<>();
               newNOde.setElement(e);
               if(isEmpty()) {
                   head = newNOde;
                   tail = head;
                   tail.setNext(head);
               }else {
                   tail.setNext(newNOde);
                   newNOde.setNext(head);
                   tail = newNOde;
               }
              
               size++;
           }

           /*
take out the first element
decrease the size
return first element or null


           */
           public E removeFirst()
           {
               head = head.getNext();
               tail.setNext(head);
               return null;
           }
           public String toString()
           {
               String s;
               Node<E> n;
              
               if ( tail == null )
                   return "null";
               s = "[";
               n = tail.getNext();
               if (n==null)
               {
                   return s+ "empty list]";
               }
               int iter =0;
               //change the while loop below for circular list instead of singly linked list
               while (n!=null)
               {
                   iter++;
                   s = s+ n.getElement();
                   if (n.getNext()!=null) s = s + ", ";
                   n = n.getNext();
                   if(n==head) {
                       break;
                   }
               }
               return s+"]";
           }
          
          
           public static void main(String[] args) {
               CircularLinkedList<Integer> list = new CircularLinkedList<>();
               list.addLast(1);
               list.addLast(2);
               list.addLast(3);
               list.addLast(4);
               list.addLast(5);
               list.addLast(6);
               list.addLast(7);
               list.addLast(8);
                  
               System.out.println(list);
               for(int i=0;i<10;i++) {
                   System.out.println("Rotate "+(i+1));
                   list.rotate();
                   System.out.println(list);
               }
              
              
           }
}

output


Related Solutions

Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
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 C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT