Question

In: Computer Science

We are working with a circular linked list that is referenced by a tail reference, i.e.,...

We are working with a circular linked list that is referenced by a tail reference, i.e., a reference to the last node of the linked list.  There is no head or size information about the linked list.

Node is declared as:

Node {

      int value;

      Node next;

}

There are four parts in this problem. Don't forget to deal with special cases for each part.

  1. Write instructions to insert a node newNode at the beginning of the circular linked list.
  2. Write instructions to insert a node newNode at the end of the circular linked list.
  3. Write instructions to delete the first node of the circular linked list.
  4. Write instructions to delete the last node of the circular linked list.

Solutions

Expert Solution

Please upvote of you like the solution

CircularLL.java


public class CircularLL {

        public class Node {
                int data;
                Node next;

        }

        // Declaring headNode and tailNode Nodes 
        public Node headNode = null;
        public Node tailNode = null;

        // method to add Nodes at end of CLL
        public void addToLastOfCLL(int data) {
                // Creating new node
                Node newNode = new Node();
                newNode.data = data;
                // Checking if CLL is empty or not
                if (headNode == null) {
                        // If CLL is empty, both headNode and tailNode would point to new node.
                        headNode = newNode;
                        tailNode = newNode;
                        newNode.next = headNode;
                } else {
                        // tailNode will point to new node.
                        tailNode.next = newNode;
                        // New node will become new tailNode.
                        tailNode = newNode;
                        // Since, it is circular linked list tailNode will point to headNode.
                        tailNode.next = headNode;
                }
        }

         //This function will add the new node at the end of the list.  
    public void addAtStartOfCLL(int data){  
        //Create new node  
        Node newNode = new Node();
        newNode.data = data;
        //Checks if the list is empty.  
        if(headNode == null) {  
             //If list is empty, both headNode and tailNode would point to new node.  
            headNode = newNode;  
            tailNode = newNode;  
            newNode.next = headNode;  
        }  
        else {  
            //Store data into temporary node  
            Node temp = headNode;  
            //New node will point to temp as next node  
            newNode.next = temp;  
            //New node will be the headNode node  
            headNode = newNode;  
            //Since, it is circular linked list tailNode will point to headNode.  
            tailNode.next = headNode;  
        }  
    }  
        //Deletes node from the beginning of the list  
    public void deleteFromStartOfCLL() {  
        //Checks whether list is empty  
        if(headNode == null) {  
            return;  
        }  
        else {  
            //Checks whether contain only one element  
            //If not, headNode will point to next element in the list and tailNode will point to new headNode.  
            if(headNode != tailNode ) {  
                headNode = headNode.next;  
                tailNode.next = headNode;  
            }  
            //If the list contains only one element   
            //then it will remove it and both headNode and tailNode will point to null  
            else {  
                headNode = tailNode = null;  
            }  
        }  
    }  
      

  //Deletes node from end of the list  
    public void deleteFromEndOfCLL() {  
        //Checks whether list is empty  
        if(headNode == null) {  
            return;  
        }  
        else {  
            //Checks whether contain only one element  
            if(headNode != tailNode ) {  
                Node currentNode = headNode;  
                //Loop will iterate till the second last element as currentNode.next is pointing to tailNode  
                while(currentNode.next != tailNode) {  
                    currentNode = currentNode.next;  
                }  
                //Second last element will be new tailNode  
                tailNode = currentNode;  
                //tailNode will point to headNode as it is a circular linked list  
                tailNode.next = headNode;  
            }  
            //If the list contains only one element  
            //Then it will remove it and both headNode and tailNode will point to null  
            else {  
                headNode = tailNode = null;  
            }  
        }
    }


        // Displays all the nodes in the list
        public void printCLL() {
                
                Node currentNode = headNode;
                
                if (headNode == null) {
                        System.out.println("List is empty");
                } else {
                        System.out.println("Nodes of the circular linked list: ");
                        
                        do {
                                // Prints each node by incrementing pointer.
                                System.out.print(" " + currentNode.data);
                                currentNode = currentNode.next;
                                
                        } while (!currentNode.equals(headNode));
                        System.out.println();
                }
        }

        public static void main(String[] args) {
                CircularLL cl = new CircularLL();
                // Adds data to the list
                cl.addAtStartOfCLL(10);
                cl.addToLastOfCLL(20);
                cl.addToLastOfCLL(30);
                cl.addToLastOfCLL(40);
                cl.addToLastOfCLL(50);
                cl.addToLastOfCLL(60);
                
                
                
                cl.deleteFromStartOfCLL();
                cl.deleteFromEndOfCLL();
                
                cl.printCLL();
        }
}

Output without calling delete mehods

Output after calling calling delete methods

End of Solution


Related Solutions

In Java, What is gained by having a Tail reference/pointer in your Linked List? A. The...
In Java, What is gained by having a Tail reference/pointer in your Linked List? A. The tail is superfluous and offers no benefits B. The tail allows us to speed up a few operations and gives us an end point to look for C.Since we have head and tail we can now do a Binary Search on the list D. It only makes deleting from the end of the list faster
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...
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...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
how to do circular linked list in c++ (inset and delet and display)
how to do circular linked list in c++ (inset and delet and display)
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT