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

  • Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the...

    Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...

  • 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

  • I'm trying to implement a queue using an array within C++. I cannot for the life...

    I'm trying to implement a queue using an array within C++. I cannot for the life of me figure out why some of my methods are not working properly. My exists method does not find a value that's in the queue and my duplicate method is duplicating the rear of my queue instead of the front. The duplicate method should take whatever is at the front of the queue (given it's not empty or full), copy that value, and put...

  • Implement an array-based Linked List in Java. Use double as the item. You need to create...

    Implement an array-based Linked List in Java. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE your language's Library List. You will receive zero points. Write a LinkedList class Write a driver (tester) call LinkListedDriver to show you have...

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