Question

Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the
public class CircularlyLinkedList { //---- --- nested Node class -------- * Singly linked node, which stores a reference to i
public int first() { // returns (but does not remove) the first element if (isEmpty()) return -1; return / MISSING */ // the
O 78% p 3:34 3G stc KW II. CircularlyLinked List.java */ public int first() { // returns (but does not remove) the first elem
O 78% p 3:34 3G stc KW II. CircularlyLinkedList.java size++; * Adds an element to the end of the list. * @param e the new ele
78% p 3:34 3G stc KW II. € CircularlyLinkedList.java * MISSING *1 * Returns the node with the minimum element in the list. *
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

import java.util.*;

public class CircularyLinkedList {
//---------------------nested Node class--------------------
/**
   *Singly linked node, which stores a reference to its element
   and
   *to the subsequent node in the list.
*/
   private static class Node {
       /** The element stored at this node*/
       private int element; // an element stored at this node
       /** A reference to the subsequent node in the list*/
       private Node next; // a reference to the subsequent node in the list
           /**
       * Creates a node with the given element and next node
       *
       *@param e the element to be stored
       *@param n reference to a node that should follow the new node
       */
       public Node(int e, Node n) {
           element = e;  
           next = n;
       }
   }
   Node tail=null;
   int size=0;
   //Accessor methods
   /**
   *Returns the element stored at the node.
   * @return the element stored at the node
   */
   // public int getElement() { return this.element;}
   /**
   *Returns the node that follows this one (or null if no such node).
   *@return the following node
   */
   public int first(){ // returns (but does not remove) the first element
       if (size==0) return -1;
       return tail.next.element;   // the head is *after* the tail
   }

   /**
   Returns (but does not remove) the last element of the list
   *@return element at the back of the list (or 1 it empty)
   */
   public int last(){ //returns (but does not remove) the last element
       if(size==0) return -1;
       return tail.element;
   }
   // update methods

   /**
   *Rotate the first element to the back of the list.
   */
   public void rotate() { // rotate the first element to the back of the list
       if (tail != null)    // if empty, do nothing
           tail = tail.next;   // the old head becomes the new tail
   }

   /**
   *Adds an element to the front of the list.
   *@param e the new element to add
   */
   public void addFirst(int e) {    //adds element e to the front of the list
       if (size == 0) {
           tail = new Node(e, null);
           tail.next = tail;    // link to itself circularly
       } else {
           Node newest = new Node(e, tail.next);
           tail.next = newest;
       }
       size++;
   }

   /**
   * Adds an element to the end of the list.
   * @param e the new element to add
   */
   public void addLast(int e) { // adds elements to the end of the list
       if(size==0){           // insert new element at front of list
           tail = new Node(e, null);
           tail.next =tail;
       }
       else{
           Node newest = new Node(e,tail.next);
           tail.next = newest;
           tail = newest;       // now new element becomes the tail
       }     
       size++;   
   }

   /**
   * Removes and returns the first element of the list.
   *@return the removed element (or -1 if empty)
   */
   public int removeFirst() { // removes and Returns the first element
       if (size==0) return -1;            // nothing to remove
       Node head = tail.next;
       if (head == tail) tail = null;        // must be the only node left
       else                    // rernoves "head" from the list
           tail.next = head.next;
       size--;
       return head.element;
   }

   /**
   *Removes and returns the Last element of the list.
   * @return the removed element (or -1 if empty)
   */  
   public int removeLast(){
       if(size==0) return -1;
       Node head = tail.next;
       Node ptr=null;
       if(head == tail) tail = null;
       else{
           while(head.next != tail){
               head = head.next;
           }
           head.next = tail.next;
           ptr = tail;
           tail = head;
       }
       size--;
       return ptr.element;
   }

   /**
   *Returns the node with the minimum element in the list.
   * @return the node with minimum element in the list (or null If empty)
   */
   public Node getMin(){
       if(size==0) return null;
       Node ptr = tail;
       Node min = ptr;
       while(ptr.next != tail){
           if(min.element > ptr.element)
               min = ptr;
           ptr = ptr.next;
       }
       if(min.element > ptr.element)
           min = ptr;
       return min;
   }

   /**
   * Returns the node with the maximum element in the list.
   *@return the node with maximum element in the list (or null if empty)
   */
   public Node getMax(){
       if(size==0) return null;
       Node ptr = tail;
       Node max = ptr;
       while(ptr.next != tail){
           if(max.element < ptr.element)
               max = ptr;
           ptr = ptr.next;
       }
       if(max.element < ptr.element)
           max=ptr;
       return max;
   }

   /**
   *Swap two nodes as a whole (without swapping the elements alone), the nodes should not be null
   *@param node1 the first node to be swapped
   * @param node2 the second node to be swapped
   */
   public void swap(Node node1, Node node2){
       int temp = node1.element;
       node1.element = node2.element;
       node2.element = temp;
   }
   /**
   *Returns the sum of the even elements in the list
   */
   public int sumEven(){
       if(size==0) return -1;
       int sum=0;
       Node ptr = tail;
       while(ptr.next != tail){
           if(ptr.element % 2 ==0)
               sum += ptr.element;
           ptr = ptr.next;
       }
       if(ptr.element % 2 ==0)
               sum += ptr.element;
       return sum;
   }
   public void display(){
       Node ptr = tail.next;
       while(ptr != tail){
           System.out.print(ptr.element+"->");
           ptr = ptr.next;
       }
       System.out.print(ptr.element+"\n");
   }
   public static void main(String[] args) {
       System.out.println("Please enter positive integers, -1 to stop.");
       int a=0;
       Scanner sc = new Scanner(System.in);
       CircularyLinkedList c = new CircularyLinkedList();
       while(a != -1){
           a = sc.nextInt();
           if(a != -1)
               c.addLast(a);
       }
       System.out.println("The List is:");
       c.display();
       System.out.println("The list after swapping the minimum with the maximum is:");
       Node max = c.getMax();
       Node min = c.getMin();
       c.swap(max,min);
       c.display();
       System.out.println("The sum of the even numbers is "+c.sumEven());
   }
}

Output:-

​​​​​​

Note:-

Here in this program i not using getElement() and getNext().

I use node.element and node.next.

Add a comment
Know the answer?
Add Answer to:
Q1: You can find a file that defines the CircularlyLinked List class similar to what we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of...

    Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • (1) Implement the countKey(T element) method, which should return a count of the number of times...

    (1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...

  • //implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * *...

    //implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT