Question

In: Computer Science

(Implement GenericQueue using inheritance) In Section 24.5, Stacks and Queues, GenericQueue is implemented using composition. Define...

(Implement GenericQueue using inheritance) In Section 24.5, Stacks and Queues, GenericQueue is implemented using composition. Define a new queue class that extends java.util.LinkedList..

Solutions

Expert Solution

GenericQueue.java

import java.util.LinkedList;


public class GenericQueue<E> extends LinkedList<E>
{
/**
   *
   */
   private static final long serialVersionUID = 1L;
   E head;
   E tail;

   public GenericQueue()
   {
       head = null;
       tail = null;
}
  
   public boolean isEmpty()
   {
       if(size() == 0)
           return true;
       return false;
   }


public void enqueue(E c)
{
   add(c);
   // if the list was initially empty point both head and tail to element
   if(head == null && tail == null)
   {
       head = c;
       tail = c;
   }
   else
   {
       //only change tail and point it to recently added node
       tail = c;
   }

}

public E dequeue()
{
   if(isEmpty())
       return null;
  
   E firstElement = getFirst(); // getLast is a linkedList method;
   poll(); // this removes the element at the head
   if(isEmpty()) // if there was a single element in the list, point both head and tail to null
   {
       head = null;
       tail = null;
   }
   else
       head = getFirst(); // point the head to the current first node. No change in tail
  
return firstElement; // return the element we just removed from the queue
}
  
//return the first element of queue without removing it
public E peek()
{
  
   return (E) element(); // this is a linkedlist method which retrieves but does not remove the head of the queue
}
  
public void print()
{
   System.out.print("PrintList: ");
   Object[] ab = toArray(); //toArray is a linkedlist method that returns all elemnts of list in array from head to tail sequence
   for(int i=0; i<size(); i++)
   {
       System.out.print(ab[i] + " ");
   }
   System.out.println();
}
}

GenericQueueDemo.java


public class GenericQueueDemo
{
   public static void main(String args[])
   {
  
       GenericQueue<Integer> g1 = new GenericQueue<Integer>();
       System.out.println("Is queue empty? "+ g1.isEmpty());
       g1.enqueue(5);
       g1.print();
       g1.enqueue(6);
       g1.enqueue(45);
       System.out.println("Peek: "+ g1.peek());
       g1.print();
       System.out.println("Is queue empty? "+ g1.isEmpty());
       g1.dequeue();
       System.out.println("Peek: "+ g1.peek());
       g1.print();
      
       GenericQueue<String> g2 = new GenericQueue<String>();
       System.out.println("Is queue empty? "+ g1.isEmpty());
       g2.enqueue("Hello");
       g2.enqueue("World!");
       g2.enqueue("This");
       g2.enqueue("is");
       g2.enqueue("a");
       g2.enqueue("new");
       g2.enqueue("day");
       g2.print();
       g2.enqueue("Welcome");
       g2.enqueue("back!!");
       System.out.println("Peek: "+ g2.peek());

       g2.print();
       System.out.println("Is queue empty? "+ g1.isEmpty());
       g2.dequeue();
       System.out.println("Peek: "+ g2.peek());

       g2.print();  
       g2.dequeue();
       g2.print();  
       g2.dequeue();
       g2.print();  
   }  

}

Sample I/O and output


Related Solutions

(Implement GenericQueue using inheritance) In section 24.5 Stacks and Queues.GenericQueue is implemented using compositions.Define a new...
(Implement GenericQueue using inheritance) In section 24.5 Stacks and Queues.GenericQueue is implemented using compositions.Define a new queue class that extends java.util.linkedlist
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the...
Describe how stacks and their main operations can be implemented using arrays and stacks. Provide the pros and cons for each approach. Research some applications, that can use stacks as a possible implementation vehicle.
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
With SPM techniques, we can often identify the surface composition of atoms using the cross-section or...
With SPM techniques, we can often identify the surface composition of atoms using the cross-section or size of the atom itself. In XPS, we can also perform such analysis, although typically with greater sensitivity and through a completely different mechanism. Why is it possible to identify individual elements with XPS based on binding energies? (Hint: why are X-rays used to perform photoelectron spectroscopy, and not a lower energy photon source?)
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT