- #1
ishika17
- 10
- 3
New poster has been reminded to post schoolwork problems in the Homework Help forums
Summary:: I have recently started with data structures in java and I tried doing this Program .But I have few confusions.
1.How do I write the main() of the program?
2.What are we supposed to do with the value returned by function pop()?
If anyone could point out if there are any errors and please if you could help me with the above questions.
Thank You.
1.How do I write the main() of the program?
2.What are we supposed to do with the value returned by function pop()?
If anyone could point out if there are any errors and please if you could help me with the above questions.
Thank You.
Java:
import java.io.*;
class CirQueue {
int cirque[]; //to store the elements of array in circular queue
int cap; //to store maximum capacity of array
int front, rear; //to store front and rear indices
CirQueue(int max) //constructor to initailize cap with the mximum value entered by the console
{
cap = max;
front = 0;
rear = 0;
}
void push(int n) //to add integer in the queue from rear end
{
if (front == 0 && rear == cap - 1 || front == rear + 1)
System.out.println("QUEUE OVERFLOWS");
else {
if (front == -1 && rear == -1) {
front = 0;
rear = 0;
} else
if (rear == cap - 1)
rear = 0;
else
rear = rear + 1;
cirque[rear] = n;
}
}
int pop() {
if (front != rear) {
int n = cirque[front];
front = (front + 1) % cap;
return (n);
} else
return (-9999);
}
void show() {
if (front != rear) {
for (int i = front; i <= rear; i = (i + 1) % cap) {
System.out.print(cirque[i] + "\t");
System.out.println();
} else
System.out.println("QUEUE IS EMPTY");
}
}