Question

JAVA 1.         Implement an array-based queue that enqueus 3 items and prints them, dequeues one element...

JAVA

1.         Implement an array-based queue that enqueus 3 items and prints them, dequeues one element and print it, and prints the front and rear items.

Sample output:

1 enqueued

2 enqueued

3 enqueued

1 dequeued

2 is front item

3 is rear item

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

Solution:

class QueueAsArray {
  
int queue[],front,rear;
int size;
  
//constructor to initialize values
QueueAsArray(int n)
{
front=rear= 0;
size=n;
queue= new int[size];
}
  
//function to enqueue an element to the queue
void Enqueue(int data)
{
int temp=0;
  
if (size == rear)
System.out.printf("Queue is full");
  
else {
queue[rear] = data;
temp=queue[rear];
rear++;
}
  
System.out.println(temp+ " enqueued");
}
  
//function to dequeue an element from the queue
void Dequeue()
{
int temp=0;
  
if (front == rear)
System.out.println("Queue is empty");
  
else {
  
temp=queue[0];
for (int i = 0; i < rear-1; i++){
queue[i] = queue[i+1];
}
  
if (rear < size){
queue[rear]=0;
}
rear--;
}
  
System.out.println(temp+ " dequeued");
}
  
//function to print the front element of the queue
void Front()
{
if (front == rear)
System.out.println("Queue is empty");
  
else
System.out.println(queue[front]+ " is front item");
}
  
//function to print the rear element of the queue
void Rear()
{
if (front == rear)
System.out.println("Queue is empty");
  
else
System.out.println(queue[rear]+ " is rear item");
}
}
  
public class Main{
  
public static void main(String[] args)
{
QueueAsArray obj = new QueueAsArray(4);
obj.Enqueue(1);
obj.Enqueue(2);
obj.Enqueue(3);
obj.Dequeue();
obj.Front();
obj.Rear();
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA 1.         Implement an array-based queue that enqueus 3 items and prints them, dequeues one element...
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
  • JAVA based programming assignment that will Implement a linked-based queue, where items 1, 2 and 3...

    JAVA based programming assignment that will Implement a linked-based queue, where items 1, 2 and 3 are equeued, followed by a dequeue operation. Print the dequeued item. Note: item1 51 item2 52 item3 53.

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

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

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • please write them in parts and not the whole code Page 3 of 5 based queue...

    please write them in parts and not the whole code Page 3 of 5 based queue - coding question rray-based fine an array-bag ray-based queue template class ArrQueue that uses a one-dimensional circular array esent the queue. The class consists of member variables: items, front, back, count. Wher functions: enqueue, dequeue, is Empty, peek Front. to represent the queue ement/ write code for enqueue member function ment/ write code for dequeue member function

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers...

    Radix sort C++ Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. USE THIS STUCTURE struct nodeQ{                            node *front;                            node *rear;               };               nodeQ que[10]; enqueue(que[i],data); EXAMPLE: Original, unsorted list: [170, 45, 75, 90, 2, 802, 24, 66] 1ST PASS:...

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