create a class named IntegerQueue
given a singlylinkedqueue of integers, write the following methods:
max(SinglyLinkedQueue<Integer> s) to return the max element in the queu.
min(SinglyLinkedQueue<Integer> s) to return the min element in the queue.
sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu.
median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue.
split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd.
package Stack_and_Queue;
import java.util.Iterator;
import List.SinglyLinkedList;
/**
* Implementation of a FIFO queue as an adaptation of a
SinglyLinkedList.
*/
public class SinglyLinkedQueue<T> implements Queue<T> {
// instance variable
/**
* Creates an empty singly linked list.
*/
private SinglyLinkedList<T> sll = new
SinglyLinkedList<>(); // an empty list
// constructor
/**
* Creates an empty singly linked queue.
*/
public SinglyLinkedQueue() {
}
/**
* Returns the number of elements in the queue.
*
* @return number of elements in the queue
*/
public int size() {
return sll.size();
}
/**
* Tests whether the queue is empty.
*
* @return true if the queue is empty, false
otherwise
*/
public boolean isEmpty() {
return sll.isEmpty();
}
/**
* Returns (but does not remove) the first element of
the queue.
*
* @return the first element of the queue (or null if
empty)
*/
public T first() {
return sll.first();
}
/**
* Inserts an element at the rear of the queue.
*
* @param e the element to be inserted
*/
public void enqueue(T e) {
sll.addLast(e);
}
/**
* Removes and returns the first element of the
queue.
*
* @return element removed (or null if empty)
*/
public T dequeue() {
return sll.removeFirst();
}
/**
* Produces a string representation of the contents of
the queue.
*
* @return textual representation of the queue
*/
public String toString() {
return sll.toString();
}
// Exercise 5.7
/**
* Returns an iterator of the elements stored in the
queue.
*
* @return iterator of the queue's elements
*/
public Iterator<T> iterator() {
return sll.iterator();
}
}
--------------------------------------------
static int max(SinglyLinkedQueue<Integer> s){
int max = Integer.MIN_VALUE;
// Check loop while head not equal to NULL
while (s!= null)
{
// If max is less then head->data then
// assign value of head->data to max
// otherwise node point to next node.
if (max < s.data)
max = s.data;
s= s.next;
}
return max;
}
--------------------------------------
static int min(SinglyLinkedQueue<Integer> s) {
int min = Integer.MAX_VALUE;
// Check loop while head not equal to NULL
while (s!= null)
{
// If min is greater then head->data then
// assign value of head->data to min
// otherwise node point to next node.
if (min > s.data)
min = s.data;
s= s.next;
}
return min;
}
----------------------------------------------------------------
static int sum(SinglyLInkedQueue<Integer> s){
int sum=0;
while (s!= null) {
sum+=s.data;
}
s=s.next;
}
----------------------------------------------------------
static int median(SinglyLinkedQueue<Integer> s){
int size=s.size();
int arr []=new int[size];
int i=0;
while(s!=null){
arr[i]=s.data;
s=s.next;
i++;
}
Arrays.sort(arr);
if(siz2%2!=0)
return arr[(size/2)+1];
else
return arr[size/2];
}
------------------------------------------------------------------------------------
static void split(SinglyLinkedQueue<Integer> s){
// Intializing an deque
int size=s.size();
Deque<Integer> even= new ArrayDeque<Integer>(size);
Deque<Integer> odd= new ArrayDeque<Integer>(size);
while(s!=null){
if(s.data%2==0)
even.add(s.data);
else
odd.add(s.data);
s=s.next;
}
}
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...
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...
Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
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...
//MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { // Problem 1 method // empty linked list, with key=k if (!containsKey(k)) { put(k, new LinkedList<V>()); } // adding v to the linked list associated with key k get(k).add(v); } public V removeFirst(K k)...
Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...
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...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...