Question

In: Computer Science

Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...

Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure.

A queue is a specialised form of list in which you can only get and remove the first element in the queue.

The class should be able to work with the following code:

SimpleQueue queue = new MyQueue();

NB: You cannot import anything from the standard library for this task. The data structure must be of your own creation, not a modified form of a pre-existing class.

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

public interface SimpleQueue {
  

   /**
   * Returns true if the queue is empty, else false.
   *
   * @return   whether the queue is empty
   */
   public boolean isEmpty();

   /**
   * Removes all elements from the queue.
   */
   public void clear();
  

   /**
   * Returns the first element, null if empty.
   *
   * @return   the first element
   */
   public T peek();
  
   /**
   * Returns and removes the first element, null if empty.
   *
   * @return   the first element
   */
   public T dequeue();
  
   /**
   * Appends the element to the end of the queue. Does nothing if the element is null.
   *
   * @param elem   the element to be added
   */
   public void enqueue(T elem);
  

   /**
   * Returns the size of the queue
   *
   * @return   the size of the queue
   */
   public int size();
  

   /**
   * Returns true if the queue contains the given element.
   *
   * @return   whether the element is present
   */
   public boolean contains(T elem);
}

Solutions

Expert Solution

//SimpleQueue.java


public class MyQueue<T> implements SimpleQueue<T> {
   Object arr[];
   int size, count;

   public MyQueue() {
       arr = new Object[1];
       size = 1;
       count = 0;
   }

   public void double_size() {
       Object temp[] = new Object[count];
       for (int i = 0; i < count; i++) {
           temp[i] = arr[i];
       }
       size = size * 2;
       for (int i = 0; i < count; i++) {
           arr[i] = temp[i];
       }
   }

   @Override
   public boolean isEmpty() {
       if (count == 0)
           return true;
       return false;
   }

   @Override
   public void clear() {
       count = 0;
       size = 1;
       arr = new Object[1];
   }

   @Override
   public T peek() {
       if (count == 0)
           return null;
       return (T) arr[count - 1];
   }

   @Override
   public T dequeue() {
       if (count == 0)
           return null;
       else {
           count--;
           T temp = (T) arr[count];
           arr[count] = null;
           return temp;
       }
   }

   @Override
   public void enqueue(T elem) {
       if (count == size)
           double_size();
       arr[count] = elem;
       count++;
   }

   @Override
   public int size() {
       return count;
   }

   @Override
   public boolean contains(T elem) {
       for (int i = 0; i < count; i++) {
           if (arr[i].equals(elem))
               return true;
       }
       return false;
   }

}


============================================================

//MyQueue.java


public class MyQueue<T> implements SimpleQueue<T> {
   Object arr[];
   int size, count;

   public MyQueue() {
       arr = new Object[1];
       size = 1;
       count = 0;
   }

   public void double_size() {
       Object temp[] = new Object[count];
       for (int i = 0; i < count; i++) {
           temp[i] = arr[i];
       }
       size = size * 2;
       for (int i = 0; i < count; i++) {
           arr[i] = temp[i];
       }
   }

   @Override
   public boolean isEmpty() {
       if (count == 0)
           return true;
       return false;
   }

   @Override
   public void clear() {
       count = 0;
       size = 1;
       arr = new Object[1];
   }

   @Override
   public T peek() {
       if (count == 0)
           return null;
       return (T) arr[count - 1];
   }

   @Override
   public T dequeue() {
       if (count == 0)
           return null;
       else {
           count--;
           T temp = (T) arr[count];
           arr[count] = null;
           return temp;
       }
   }

   @Override
   public void enqueue(T elem) {
       if (count == size)
           double_size();
       arr[count] = elem;
       count++;
   }

   @Override
   public int size() {
       return count;
   }

   @Override
   public boolean contains(T elem) {
       for (int i = 0; i < count; i++) {
           if (arr[i].equals(elem))
               return true;
       }
       return false;
   }

}


===================================

// Code Snippet


Related Solutions

Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
In this homework, you will implement a single linked list to store a list of employees...
In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below: Employee Type Attribute int ID string name string department int salary Employee* next Return Type Function (constructor) Employee(int ID, string name, string department, int salary) EmployeeList class should...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
Could you check/edit my code? Design and implement your own linked list class to hold a...
Could you check/edit my code? Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT