I'm trying to make an array-based queue that inserts and removes the elements...
The default array is {0,0,0,9,10}. after the 0's are filled up it should say that its Full and will return to the loop. I'm having trouble making 3 particular methods which is dequeue, isEmpty, and isFull..

class QueueMethods{
	public int[]array;
	public int front, rear, maxSize, nItems, temp;
	public QueueMethods(int s)
	{
		maxSize = s;
		array = new int[maxSize];
		front = 0;
		rear = -1;
		nItems = 0;
	}

	public int[] display()
	{
		for(int x=0; x<5;x++)
		{
		System.out.print("["+array[x]+"]" + " ");
		}
		return array;
	}

	public boolean isEmpty()
	{
		System.out.println("The queue is Empty, you can either enqueue or exit the program");
            //codes
	}

	public boolean isFull()
	{
		System.out.println("The queue is already full, you can either dequeue or exit the program");
         //codes
	}

	public void enqueue(int o)
	{
		if(rear == maxSize -1)
			rear = -1;
		array[++rear]=o;
		nItems++;
	}

	public int dequeue()
	{
		//codes
}
}

pls help, thx in advance

here's the main method

import java.io.*;
public class QueueProgram{
	public static InputStreamReader reader=new InputStreamReader(System.in);
	public static BufferedReader input=new BufferedReader(reader);
	public static void main (String args[])throws Exception{

		QueueMethods q = new QueueMethods(5);
			q.enqueue(0);
			q.enqueue(0);
			q.enqueue(0);
			q.enqueue(9);
			q.enqueue(10);
			for(int x=0;x<1;x+=0){
				q.display();
				System.out.println("\nChoose: \n1: Enqueue" + "\n2: Dequeue" + "\n3: Exit");
				int choice = Integer.parseInt(input.readLine());

				if(choice==1){
					System.out.println("You have chosen 'Enqueue' \nPlease insert the number you want to enqueue :");
					int eChoice = Integer.parseInt(input.readLine());
					q.enqueue(eChoice);
				}
				else if(choice==2){
					System.out.println("You have chosen 'Dequeue' \nDeleting bottom digit...");
					q.dequeue();
				}
				else if(choice==3){
					System.out.println("Exiting Program...\n.........");
					System.exit(0);
				}
				else
					System.out.println("Invalid input, you can only choose from 1, 2, and 3.");

			}
	}
}
Member Avatar for ztini

How about having a private int pointer = 0; ?

As you enqueue/dequeue, this ++ or --. Dequeue could change the array value to 0 and move the pointer--. Enqueue would check if pointer + 1= = 0, if so, change the value and point++;

You do not need a bufferedreader/inputstream for console input; those are better suited for reading larges chunks of data, such as a file. Just use:

Scanner in = new Scanner(System.in);
int choice = in.nextInt();

Also, try using a do-while loop for your interface.

isEmpty and isFull should be easy. Maintain a variable for the size. Whenever you enqueue, add 1 to that variable. Whenever you dequeue, subtract 1. isEmpty and isFull just depend on the value of that variable. For the actual enqueue operation, maintain the head of the queue as the index 0 and the tail of the queue as the last index. Then check the size variable I previously mentioned.

Example to help you get on the right train of thought for enqueue...

-- starting values --
queue[0] = something, queue size = 1
-- do an enqueue operation for the new element = 2 --
queue[0] = something, queue size = 1, queue[size] = queue[1] = 2

Hopefully that will help you for dequeue as well. Remember that when dequeueing, you need to shift the values in the array one place to the left (if you are using array[0] as the head) and you need to maintain the right queue size.

Thanks for the help, I'll be working on it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.