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(); } /*...
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...
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.
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...
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.
**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...
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...
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...
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
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:"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT