954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I need help with an Array-based Queue

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

miku003
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.");

			}
	}
}
miku003
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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.

ztini
Posting Whiz in Training
299 posts since Jan 2011
Reputation Points: 54
Solved Threads: 52
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

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

miku003
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: