Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

it is one question data structure in java please can you solve it with easy way Q1: You can find a file that defines the

it is one question
data structure in java
please can you solve it with easy way image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/* MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin i. Task: find the node with the minimum element and return it without removing it from the list ii. Return the node with minimum element or null if empty b. public Node getMax0 i. Task: find the node with the maximum element and return it without removing it from the list ii. Return the node with maximum element or null if empty c. public void swap(Node node1, Node node2) i. Task: swap the given nodes (don't swap just the elements of the node) if neither of them is null. Assume the nodes are already in list. ii. Param two nodes: node1 and node2 are the nodes to be swapped. d. public int sumEven i. Task: calculate the sum of the even elements only assuming the type of the element is integer ii. Return the value of the sum of even elements e. public void display i. Task: print the elements of the list starting from the first element and separated by -> 3. Provide a test class. Inside main, create a circularly link list object of integer elements. Then, ask the user to enter positive integer numbers and -1 to stop and add these numbers to the circular list in the order they were entered. Then, display the list. Swap the minimum node with the maximum node and then display the list. Finally, print the sum of the even numbers in the list. Check the sample output. Please enter positive integers, -1 to stop. 10 4 3 972 5168-1 The list is: 10->4->3->9->7->2->5->1->6->8 The list after swapping the minimum with the maximum is: 1->4->3->9->7->2->5->10->6->8 1 of 1 The sum of the even numbers is 30 public class CircularlyLinkedList { //---------------- 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; // Accessor methods * Returns the element stored at the node. * @return the element stored at the node public int getElement() { return element; } * Returns the node that follows this one (or null if no such node). * @return the following node node). * @return the following node public Node getNext() { return next; } // Modifier methods * Sets the node's next reference to point to Node n. * @param n the node that should follow this one public void setNext(Node n) {next = n; } } //----------- end of nested Node class ---- // instance variables of the CircularlyLinkedList /** The designated cursor of the list */ private Node tail = null; // we store tail (but not head) /** Number of nodes in the list */ private int size = 0; // number of nodes in the list /** Constructs an initially empty list. */ public CircularlyLinkedList() {} // constructs an initially empty list // access methods * Returns the number of elements in the linked list. * @return number of elements in the linked list public int size() { return size; } * Tests whether the linked list is empty. * @return true if the linked list is empty, false otherwise public boolean isEmpty() { /* MISSING */} public boolean isEmpty() { /* MISSING */ } * Returns (but does not remove) the first element of the list * @return element at the front of the list (or -1 if empty) public int first() { // returns (but does not remove) the first element if (isEmpty()) return -1; return /* MISSING */ // 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 if empty) public int last() { // returns (but does not remove) the last element if (isEmpty()) return -1; return /* MISSING */ // 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 * MISSING */ // the old head becomes the new tail /** * Adds an element to the front of the list. * @param e the new element to add ll adds elemente to the public void addFirst(int e) { front of the list if (size == 0) { tail = new Node(e, null); tail.setNext(tail); // link to itself circularly } else { Node newest = new Nodele, tail.getNext(); tail.setNext(newest); size++; * * Adds an element to the end of the list. * @param e the new element to add public void addLast(int e) { // adds elemente to the end of the list 1 MISSING */ // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail * 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 (isEmpty()) return -1; // nothing to remove Node head = tail.getNext(); if (head == tail) tail = null; // must be the only node left else /* MISSING */ // removes "head" from the list size--; return head.getElement(); * Removes and returns the last element of the list. * @return the removed element (or - 1 if empty) public int removeLast() public int removeLast() * MISSING / 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() /* MISSING */ * 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() * MISSING / 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) /* MISSING */ @return the node with maximum element in the list (or null if empty) public Node getMax() /* MISSING */ * 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) /* MISSING */ * Returns the sum of the even elements in the list * @return the calculated sum (or O if empty) public int sumEven() /* MISSING */ * Display the elements of the list seperated by -> public void display() /* MISSING */ Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/* MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin i. Task: find the node with the minimum element and return it without removing it from the list ii. Return the node with minimum element or null if empty b. public Node getMax0 i. Task: find the node with the maximum element and return it without removing it from the list ii. Return the node with maximum element or null if empty c. public void swap(Node node1, Node node2) i. Task: swap the given nodes (don't swap just the elements of the node) if neither of them is null. Assume the nodes are already in list. ii. Param two nodes: node1 and node2 are the nodes to be swapped. d. public int sumEven i. Task: calculate the sum of the even elements only assuming the type of the element is integer ii. Return the value of the sum of even elements e. public void display i. Task: print the elements of the list starting from the first element and separated by -> 3. Provide a test class. Inside main, create a circularly link list object of integer elements. Then, ask the user to enter positive integer numbers and -1 to stop and add these numbers to the circular list in the order they were entered. Then, display the list. Swap the minimum node with the maximum node and then display the list. Finally, print the sum of the even numbers in the list. Check the sample output. Please enter positive integers, -1 to stop. 10 4 3 972 5168-1 The list is: 10->4->3->9->7->2->5->1->6->8 The list after swapping the minimum with the maximum is: 1->4->3->9->7->2->5->10->6->8 1 of 1 The sum of the even numbers is 30 public class CircularlyLinkedList { //---------------- 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; // Accessor methods * Returns the element stored at the node. * @return the element stored at the node public int getElement() { return element; } * Returns the node that follows this one (or null if no such node). * @return the following node node). * @return the following node public Node getNext() { return next; } // Modifier methods * Sets the node's next reference to point to Node n. * @param n the node that should follow this one public void setNext(Node n) {next = n; } } //----------- end of nested Node class --- // instance variables of the CircularlyLinkedList /** The designated cursor of the list / private Node tail = null; // we store tail (but not head) /** Number of nodes in the list */ private int size = 0; // number of nodes in the list /** Constructs an initially empty list. */ public CircularlyLinkedList() {} // constructs an initially empty list // access methods * Returns the number of elements in the linked list. * @return number of elements in the linked list public int size() { return size; } * Tests whether the linked list is empty. * @return true if the linked list is empty, false otherwise public boolean isEmpty() { /* MISSING */} public boolean isEmpty() { /* MISSING */ } * Returns (but does not remove) the first element of the list * @return element at the front of the list (or - 1 if empty) public int first() { // returns (but does not remove) the first element if (isEmpty()) return -1; return /* MISSING */ // 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 if empty) public int last() { // returns (but does not remove) the last element if (isEmpty()) return -1; return /* MISSING */ // 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 * MISSING */ // the old head becomes the new tail * Adds an element to the front of the list. * @param e the new element to add l adds elemente to the public void addFirst(int e) { front of the list if (size == 0) { tail = new Nodele, null); tail.setNext(tail); // link to itself circularly } else { Node newest = new Nodele, tail.getNext()); tail.setNext(newest); size++; * * Adds an element to the end of the list. * @param e the new element to add public void addLast(int e) { // adds elemente to the end of the list 1 MISSING */ // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail * 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 (isEmpty() return -1; // nothing to remove Node head = tail.getNext(); if (head == tail) tail = null; // must be the only node left else /* MISSING */ // removes "head" from the list size--; return head.getElement(); * Removes and returns the last element of the list. * @return the removed element (or - 1 if empty) public int removeLast() public int removeLast() * MISSING / 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() /* MISSING */ * 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() * MISSING / 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) /* MISSING */ @return the node with maximum element in the list (or null if empty) public Node getMax() /* MISSING */ * 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) /* MISSING */ * Returns the sum of the even elements in the list * @return the calculated sum (or 0 if empty) public int sumEven() /* MISSING / * Display the elements of the list seperated by -> public void display() /* MISSING */ Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/* MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin i. Task: find the node with the minimum element and return it without removing it from the list ii. Return the node with minimum element or null if empty b. public Node getMax0 i. Task: find the node with the maximum element and return it without removing it from the list ii. Return the node with maximum element or null if empty c. public void swap(Node node1, Node node2) i. Task: swap the given nodes (don't swap just the elements of the node) if neither of them is null. Assume the nodes are already in list. ii. Param two nodes: node1 and node2 are the nodes to be swapped. d. public int sumEven i. Task: calculate the sum of the even elements only assuming the type of the element is integer ii. Return the value of the sum of even elements e. public void display i. Task: print the elements of the list starting from the first element and separated by -> 3. Provide a test class. Inside main, create a circularly link list object of integer elements. Then, ask the user to enter positive integer numbers and -1 to stop and add these numbers to the circular list in the order they were entered. Then, display the list. Swap the minimum node with the maximum node and then display the list. Finally, print the sum of the even numbers in the list. Check the sample output. Please enter positive integers, -1 to stop. 10 4 3 972 5168-1 The list is: 10->4->3->9->7->2->5->1->6->8 The list after swapping the minimum with the maximum is: 1->4->3->9->7->2->5->10->6->8 1 of 1 The sum of the even numbers is 30 public class CircularlyLinkedList { //---------------- 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; // Accessor methods * Returns the element stored at the node. * @return the element stored at the node public int getElement() { return element; } * Returns the node that follows this one (or null if no such node). * @return the following node node). * @return the following node public Node getNext() { return next; } // Modifier methods * Sets the node's next reference to point to Node n. * @param n the node that should follow this one public void setNext(Node n) {next = n; } } //----------- end of nested Node class ---- // instance variables of the CircularlyLinkedList /** The designated cursor of the list */ private Node tail = null; // we store tail (but not head) /** Number of nodes in the list */ private int size = 0; // number of nodes in the list /** Constructs an initially empty list. */ public CircularlyLinkedList() {} // constructs an initially empty list // access methods * Returns the number of elements in the linked list. * @return number of elements in the linked list public int size() { return size; } * Tests whether the linked list is empty. * @return true if the linked list is empty, false otherwise public boolean isEmpty() { /* MISSING */} public boolean isEmpty() { /* MISSING */ } * Returns (but does not remove) the first element of the list * @return element at the front of the list (or -1 if empty) public int first() { // returns (but does not remove) the first element if (isEmpty()) return -1; return /* MISSING */ // 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 if empty) public int last() { // returns (but does not remove) the last element if (isEmpty()) return -1; return /* MISSING */ // 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 * MISSING */ // the old head becomes the new tail /** * Adds an element to the front of the list. * @param e the new element to add ll adds elemente to the public void addFirst(int e) { front of the list if (size == 0) { tail = new Node(e, null); tail.setNext(tail); // link to itself circularly } else { Node newest = new Nodele, tail.getNext(); tail.setNext(newest); size++; * * Adds an element to the end of the list. * @param e the new element to add public void addLast(int e) { // adds elemente to the end of the list 1 MISSING */ // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail * 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 (isEmpty()) return -1; // nothing to remove Node head = tail.getNext(); if (head == tail) tail = null; // must be the only node left else /* MISSING */ // removes "head" from the list size--; return head.getElement(); * Removes and returns the last element of the list. * @return the removed element (or - 1 if empty) public int removeLast() public int removeLast() * MISSING / 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() /* MISSING */ * 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() * MISSING / 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) /* MISSING */ @return the node with maximum element in the list (or null if empty) public Node getMax() /* MISSING */ * 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) /* MISSING */ * Returns the sum of the even elements in the list * @return the calculated sum (or O if empty) public int sumEven() /* MISSING */ * Display the elements of the list seperated by -> public void display() /* MISSING */ Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/* MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin i. Task: find the node with the minimum element and return it without removing it from the list ii. Return the node with minimum element or null if empty b. public Node getMax0 i. Task: find the node with the maximum element and return it without removing it from the list ii. Return the node with maximum element or null if empty c. public void swap(Node node1, Node node2) i. Task: swap the given nodes (don't swap just the elements of the node) if neither of them is null. Assume the nodes are already in list. ii. Param two nodes: node1 and node2 are the nodes to be swapped. d. public int sumEven i. Task: calculate the sum of the even elements only assuming the type of the element is integer ii. Return the value of the sum of even elements e. public void display i. Task: print the elements of the list starting from the first element and separated by -> 3. Provide a test class. Inside main, create a circularly link list object of integer elements. Then, ask the user to enter positive integer numbers and -1 to stop and add these numbers to the circular list in the order they were entered. Then, display the list. Swap the minimum node with the maximum node and then display the list. Finally, print the sum of the even numbers in the list. Check the sample output. Please enter positive integers, -1 to stop. 10 4 3 972 5168-1 The list is: 10->4->3->9->7->2->5->1->6->8 The list after swapping the minimum with the maximum is: 1->4->3->9->7->2->5->10->6->8 1 of 1 The sum of the even numbers is 30 public class CircularlyLinkedList { //---------------- 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; // Accessor methods * Returns the element stored at the node. * @return the element stored at the node public int getElement() { return element; } * Returns the node that follows this one (or null if no such node). * @return the following node node). * @return the following node public Node getNext() { return next; } // Modifier methods * Sets the node's next reference to point to Node n. * @param n the node that should follow this one public void setNext(Node n) {next = n; } } //----------- end of nested Node class --- // instance variables of the CircularlyLinkedList /** The designated cursor of the list / private Node tail = null; // we store tail (but not head) /** Number of nodes in the list */ private int size = 0; // number of nodes in the list /** Constructs an initially empty list. */ public CircularlyLinkedList() {} // constructs an initially empty list // access methods * Returns the number of elements in the linked list. * @return number of elements in the linked list public int size() { return size; } * Tests whether the linked list is empty. * @return true if the linked list is empty, false otherwise public boolean isEmpty() { /* MISSING */} public boolean isEmpty() { /* MISSING */ } * Returns (but does not remove) the first element of the list * @return element at the front of the list (or - 1 if empty) public int first() { // returns (but does not remove) the first element if (isEmpty()) return -1; return /* MISSING */ // 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 if empty) public int last() { // returns (but does not remove) the last element if (isEmpty()) return -1; return /* MISSING */ // 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 * MISSING */ // the old head becomes the new tail * Adds an element to the front of the list. * @param e the new element to add l adds elemente to the public void addFirst(int e) { front of the list if (size == 0) { tail = new Nodele, null); tail.setNext(tail); // link to itself circularly } else { Node newest = new Nodele, tail.getNext()); tail.setNext(newest); size++; * * Adds an element to the end of the list. * @param e the new element to add public void addLast(int e) { // adds elemente to the end of the list 1 MISSING */ // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail * 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 (isEmpty() return -1; // nothing to remove Node head = tail.getNext(); if (head == tail) tail = null; // must be the only node left else /* MISSING */ // removes "head" from the list size--; return head.getElement(); * Removes and returns the last element of the list. * @return the removed element (or - 1 if empty) public int removeLast() public int removeLast() * MISSING / 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() /* MISSING */ * 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() * MISSING / 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) /* MISSING */ @return the node with maximum element in the list (or null if empty) public Node getMax() /* MISSING */ * 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) /* MISSING */ * Returns the sum of the even elements in the list * @return the calculated sum (or 0 if empty) public int sumEven() /* MISSING / * Display the elements of the list seperated by -> public void display() /* MISSING */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions

Question

Demonstrate knowledge of the company/organization and the position.

Answered: 1 week ago

Question

Did you offer hard data that is verifiable? [D]

Answered: 1 week ago