0% found this document useful (0 votes)
33 views4 pages

Ccl212-18 - Yoursection - Ola-F1 - Yourlastnamefirstnamemi - PDF: Llano, Vergel S. February 4, 2021 2bsemc-1 Ccl212-18

The document describes how to implement a queue using an array in Java. It defines a Queue class with methods to enqueue elements to the rear of the queue, dequeue elements from the front of the queue, and display the queue. These methods use indices front and rear to track the first and last elements in the queue. The main method creates a Queue of size 4, performs enqueue and dequeue operations, and calls display to output the queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Ccl212-18 - Yoursection - Ola-F1 - Yourlastnamefirstnamemi - PDF: Llano, Vergel S. February 4, 2021 2bsemc-1 Ccl212-18

The document describes how to implement a queue using an array in Java. It defines a Queue class with methods to enqueue elements to the rear of the queue, dequeue elements from the front of the queue, and display the queue. These methods use indices front and rear to track the first and last elements in the queue. The main method creates a Queue of size 4, performs enqueue and dequeue operations, and calls display to output the queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Llano, Vergel S.

February 4, 2021
2BSEMC-1 CCL212-18

Filenames: CCL212-18_YourSection_OLA-F1_YourLastnameFirstnameMI.pdf

To implement a queue using array, create an array arr of size n and take two variables front and rear both of


which will be initialized to 0 which means the queue is currently empty. Element rear is the index up to
which the elements are stored in the array and front is the index of the first element of the array.

1. Copy and paste the program code in your Java Editor / Compiler. Provide the missing codes to run the
program.
2. Compile and run the program. Take a screenshot of the output and paste it in the space provided.
3. Trace the program and provide the values for each argument and varables.
Source Code

// Java program to implement a queue using an array


class Queue {
private static int front, rear, capacity;
private static int queue[];

Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}

// function to insert an element


// at the rear of the queue
static void queueEnqueue(int data)
{
// check queue is full or not
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}

// insert element at the rear


else {
queue[rear] = data;
rear++;
}
return;
}

// function to delete an element


// from the front of the queue
static void queueDequeue()
{
// if queue is empty
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}

// shift all the elements from index 2 till rear


// to the right by one
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// store 0 at rear indicating there's no element
if (rear < capacity)
queue[rear] = 0;

// decrement rear
rear--;
}
return;
}

// print queue elements


static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}

// traverse front to rear and print elements


for (i = front; i < rear; i++) {
System.out.printf(" %d <-- ", queue[i]);
}
return;
}

// print front of queue


static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
System.out.printf("\nFront Element is: %d", queue[front]);
return;
}
}

public class StaticQueueinjava {

// Driver code
public static void main(String[] args)
{
// Create a queue of capacity 4
Queue q = new Queue(4);

// print Queue elements


q.queueDisplay();

// inserting elements in the queue


q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);

// print Queue elements


q.queueDisplay();

// insert element in the queue


q.queueEnqueue(60);

// print Queue elements


q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\n\nafter two node deletion\n\n");

// print Queue elements


q.queueDisplay();

// print front of the queue


q.queueFront();
}
}
Screenshot of the Output

OUTPUT OF VARIABLES IN EACH TRACE

You might also like