Question

create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...

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();
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

--------------------------------------------

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;

}

}

Add a comment
Know the answer?
Add Answer to:
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s)...
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
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