Question

In: Computer Science

You must write the correct code for all member methods so that queue functionality can be...

You must write the correct code for all member methods so that queue functionality can be obtained. Changes to the class definition, instance variables, and signatures of the membership method are not allowed. This class has no main method.

package javaclass;

/**

* A queue structure of Movie objects. Only the Movie values contained

* in the queue are visible through the standard queue methods. The Movie

* values are stored in a DoubleLink object provided in class as attribute.

*

*

*

* @version 2013-07-04

*

* @param Movie

* this data structure value type.

*/

public class DoubleQueue {

// First node of the queue

private DoubleLink values = null;

/**

   * Combines the contents of the left and right Queues into the current

   * Queue. Moves nodes only - does not move value or call the high-level

   * methods insert or remove. left and right Queues are empty when done.

   * Nodes are moved alternately from left and right to this Queue.

   *

   * @param source1

   * The front Queue to extract nodes from.

   * @param source2

   * The second Queue to extract nodes from.

   */

public void combine(final DoubleQueue source1,

final DoubleQueue source2) {

// your code here

}

/**

   * Adds value to the rear of the queue.

   *

   * @param value

   * The value to added to the rear of the queue.

   */

public void insert(final Movie value) {

// your code here

}

/**

   * Returns the front value of the queue and removes that value from the

   * queue. The next node in the queue becomes the new front node.

   *

   * @return The value at the front of the queue.

   */

public Movie remove() {

//your code here

return null;//you must change this statement as per your requirement

}

/**

   * Returns the value at the front. Must be copy safe

   *

   * @return the value at the front.

   */

public Movie peekFront() {

// your code here

return null;//you must change this statement as per your requirement

}

/**

   * Returns the value at the rear. Must be copy safe.

   *

   * @return the value at the rear.

   */

public Movie peekRear() {

// your code here

return null;//you must change this statement as per your requirement

}

/**

   * Returns all the data in the queue in the form of an array.

   *

   * @return The array of Movie elements. Must be copy safe

   */

public final Movie [] toArray() {

//your code here

return null;//you must change this statement as per your requirement

}

}

Double link class

package javaclass;

/**

* The class for doubly-linked data structures. Provides attributes

* and implementations for getLength, isEmpty, and toArray methods.

* The head attribute is the first node in any doubly-linked list and

* last is the last node.

*

* @author

* @version 2017-11-01

*

*/

public class DoubleLink {

// First node of double linked list

private DoubleNode head = null;

// Number of elements currently stored in linked list

private int length = 0;

// Last node of double linked list.

private DoubleNode last = null;

/**

   * Adds a new Movie element to the list at the head position

   * before the previous head, if any. Increments the length of the List.

*

   * @param value

   * The value to be added at the head of the list.

*

   * @return true if node is added successfully, else false.

   */

public final boolean addNode(final Movie value) {

//your code here

return false;//you must change this statement as per your requirement

}

/**

   * Removes the value at the front of this List.

   *

   * @return The value at the front of this List.

   */

public Movie removeFront() {

// your code here

return null;//you must change this statement as per your requirement

}

/**

   * Returns the head element in the linked structure. Must be copy safe.

   *

   * @return the head node.

   */

public final DoubleNode getHead() {

//your code here

return null;//you must change this statement as per your requirement

}

/**

   * Returns the current number of elements in the linked structure.

   *

   * @return the value of length.

   */

public final int getLength() {

//your code here

return -1;//you must change this statement as per your requirement

}

/**

   * Returns the last node in the linked structure. Must be copy safe.

   *

   * @return the last node.

   */

public final DoubleNode getLast() {

//your code here

return null;//you must change this statement as per your requirement

}

/**

   * Determines whether the double linked list is empty or not.

   *

   * @return true if list is empty, false otherwise.

   */

public final boolean isEmpty() {

//your code here

return true;//you must change this statement as per your requirement

}

/**

   * Returns all the data in the list in the form of an array.

   *

   * @return The array of Movie elements. Must be copy safe

   */

public final Movie [] toArray() {

//your code here

return null;//you must change this statement as per your requirement

}

}

Solutions

Expert Solution

#include<iostream>

using namespace std;

#define SIZE 10

class Queue
{
    int a[SIZE];
    int rear;   //same as tail
    int front;  //same as head
  
    public:
    Queue()
    {
        rear = front = -1;
    }
    
    //declaring enqueue, dequeue and display functions
    void enqueue(int x);     
    int dequeue();
    void display();
};

// function enqueue - to add data to queue
void Queue :: enqueue(int x)
{
    if(front == -1) {
        front++;
    }
    if( rear == SIZE-1)
    {
        cout << "Queue is full";
    }
    else
    {
        a[++rear] = x;
    }
}

// function dequeue - to remove data from queue
int Queue :: dequeue()
{
    return a[++front];  // following approach [B], explained above
}

// function to display the queue elements
void Queue :: display()
{
    int i;
    for( i = front; i <= rear; i++)
    {
        cout << a[i] << endl;
    }
}

// the main function
int main()
{
    Queue q;
    q.enqueue(10);
    q.enqueue(100);
    q.enqueue(1000);
    q.enqueue(1001);
    q.enqueue(1002);
    q.dequeue();
    q.enqueue(1003);
    q.dequeue();
    q.dequeue();
    q.enqueue(1004);
    
    q.display();
    
    return 0;
}

Related Solutions

You must write an appropriate code for all member methods so that a double-linked list functionality...
You must write an appropriate code for all member methods so that a double-linked list functionality can be obtained. Changes to the class definition, instance variables, and signatures of the membership method are not allowed. This class has no main method. package javaclass; /** * The class for doubly-linked data structures. Provides attributes * and implementations for getLength, isEmpty, and toArray methods. * The head attribute is the first node in any doubly-linked list and * last is the last...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
Submit a document with methods for an automobile class, and pseudo code indicating functionality of each...
Submit a document with methods for an automobile class, and pseudo code indicating functionality of each method. Example: public String RemoveVehicle(String autoMake, String autoModel, String autoColor, int autoYear) If values entered match values stored in private variables remove vehicle information else return message indicating mismatch
[C++] Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that...
[C++] Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first.
YOU MUST SHOW ALL WORK (CALCUATIONS) AND DRAW TIME LINES. WRITE WITH THE CORRECT FORMULA TO...
YOU MUST SHOW ALL WORK (CALCUATIONS) AND DRAW TIME LINES. WRITE WITH THE CORRECT FORMULA TO BE USED 1) Jane is 25 years old and was able to save $25,000.    After doing some research, she identifies a stock called HPG Industries that has a dividend yield of 7% that has been consistent for the past 10 years. Based on this information, she decides to invest in this stock. Considering that HPG Industries pays the dividend consistently, how many years will...
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue <T>{ } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Guidelines for the program: All methods listed below must be public and static. If your code...
Guidelines for the program: All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT