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

In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
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.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
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.
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
**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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT