Question

Create a Java code that includes all the methods from the Lecture slides following the ADTs

Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queu

LECTURE SLIDES

The Array List ADT /**Simplified version of java.util. List */ public interface List<E> { /**Returns the number of elements i

The Queue ADT Auxiliary queue operations: The Queue ADT stores arbitrary objects object front(): returns the element at the f

Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm. In the comments additionally explain the advantages of the array-based implementation over the linked- list-based implementation and vice versa. Keep your code nicely formatted and follow good programming practices. Try to make the code robust and try to use generics.
The Array List ADT /**Simplified version of java.util. List */ public interface List { /**Returns the number of elements in this list */ public int size(); /**Returns whether the list is empty. */ public boolean isEmpty() /** Inserts an element e to be at index I, shifting all elements after this. */ public void add(int I, E e) throws IndexOutOfBoundsException; /** Returns the element at index I, without removing it. */ public E get(int i) throws IndexOutOfBoundsException; /** Removes and returns the element at index I, shifting the elements after this. */ public E remove(int i) throws IndexOutOfBounds Exception; /**Replaces the element at index I with e, returning the previous element at i. */ public E set(int I, E e) throws IndexOutOfBoundsException; }
The Queue ADT Auxiliary queue operations: The Queue ADT stores arbitrary objects object front(): returns the element at the front without Insertions and deletions follow removing it the first-in first-out (FIFO) integer size(): returns the number of elements stored scheme Insertions are at the rear of the boolean isEmpty(): indicates whether no elements are stored queue and removals are at the front of the queue Exceptions Main queue operations: Attempting the execution of dequeue or front on an queue throws an EmptyQueueException enqueue(object): inserts an element at the end of the queue empty object dequeue( removes and - returns the element at the front Attempting to enqueue an element on a queue that is full can be signaled with a FullQueueException of the queue
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.Queue ADT using linked list internally:

// Implementation of queue using linked list internally
// Noade class
class QNode
{
   int data;
   QNode nextNode;
  
   // constructor to create a new linked list node
   public QNode(int data) {
       this.data = data;
       this.nextNode = null;
   }
}

// A class to represent a queue
//The queue, front stores the front node and rear stores the last node of LL
class Queue
{
   QNode front, rear;
  
   public Queue() {
       this.front = this.rear = null;
   }
  
   // Method to add an data to the queue.
   void enqueue(int data)
   {
      
       // Create a new LL node
       QNode temp = new QNode(data);
  
       // If queue is empty, then new node is front and rear both
       if (this.rear == null)
       {
       this.front = this.rear = temp;
       return;
       }
  
       // Add the new node at the end of queue and change rear
       this.rear.nextNode = temp;
       this.rear = temp;
   }
  
   // Method to remove an data from queue.
   QNode dequeue()
   {
       // If queue is empty, return NULL.
       if (this.front == null)
       return null;
  
       // Store previous front and move front one node ahead
       QNode temp = this.front;
       this.front = this.front.nextNode;
  
       // If front becomes NULL, then change rear also as NULL
       if (this.front == null)
       this.rear = null;
       return temp;
   }
}

public class Example
{
   public static void main(String[] args)
   {
       Queue q=new Queue();
       q.enqueue(1);
       q.enqueue(2);
       q.dequeue();
       q.dequeue();
       q.enqueue(3);
       q.enqueue(4);
       q.enqueue(5);
      
       System.out.println("Dequeued item is "+ q.dequeue().data);
   }
}

Add a comment
Know the answer?
Add Answer to:
Create a Java code that includes all the methods from the Lecture slides following the ADTs...
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
  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • 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...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • I just need a java mehod that follows the Javadocs Implented using an arraylist. public class...

    I just need a java mehod that follows the Javadocs Implented using an arraylist. public class WorkAheadQueue<T> implements WorkAheadQueueADT<T> {     private LinearNode<T> front;     private LinearNode<T> back;     private int numNodes;     private ArrayList<LinearNode<T>> frontFive; Removes and returns the element that is at place x in the queue. Precondition: x must be less than 5, x must be less than size * Note: indexing from 0: 0-front element, I =-second element, etc. eparam x the passed in index of...

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