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
  • 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...

  • 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...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

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
ADVERTISEMENT