Thursday, May 14, 2009

Queue Using Stack


Tis post is the continues of my previous post and there will be two complete java program, the java version of my senior post and the revision version that I made in case in my opinion his program still have an error.

Queue as if in a line of waiting ticket for cinema has front and rear, the front is the the first person in that line who will get the ticket the first time, while the rear is the last person in that line that will get the ticket if he/she is lucky because he will get the ticket the last one.

If the first person already got his ticket, he will out from the line, and then the person behind him/her all person in the line move forward, I assume that got ticket person's named Imam, and the person behind him is Baihaqi, so Imam is no longer in the line, while Baihaqi is in the front now.

If we indexing the line by 0, 1, 2, 3, etc. Imam is no longer in the index, he already out for buying coke or chocolate milk for the movie, While Baihaqi is in index 0, before that he was in index 1. So do the last person named Ahmad, he is the rear or the last person in line, his index is decremented, from index number 30 now he is in index 29. And then there is a new person that also want to buy the ticket named Ali, now he is the rear or the last person in line, his index is 30, etc etc.

In queue there are enqueue and dequeue, enqueue is just like Ali when enter the line, he will be as the rear of the queue, his index is rear plus one, I mean in enqueue the rear is incremented while the front remains unchanged. While dequeue is just when Imam out from the line because of he already got the ticket, all the person moving forwards, now the person who is in the front now is change, the front person is Baihaqi not Imam anymore, but still the index of the front is always 0, and the rear is decremented.

In my senior program's there is a mistake about this, specifically in dequeue, I guess he thought that the front is the person, not the index, the first is Imam in front, and then after dequeue, Baihaqi is the front, but I think is a mistake, front and rear is refer to index, front is always 0, and rear is always change when enqueue or dequeue, if enqueue its increment, if dequeue its decrement.

I already capture the output of both program, and sign it when doing dequeue, after dequeueing it also print the index of the front and the rear, so it will more clear about them. Here is the full code of my senior program in java version:

package stack.queue;

import java.util.Scanner;

public class QueueUsingStack {
static final int MAXQUEUE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[] queue = new int[MAXQUEUE];
int front = -1;
int rear = -1;
int choice;

do {
System.out.println("*******************************");
System.out.println("Menu:");
System.out.println("1. ENQUEUE\t2. DEDQUEUE\t3. VIEW\t4. EXIT");
System.out.println("*******************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1://enqueue
if (rear < MAXQUEUE - 1) { //is queue not full yet?
System.out.print("Data enter = ");
queue[rear + 1] = scanner.nextInt();
rear++;
System.out.println();
if(rear == 0) front = 0;
} else {
System.err.println("Queue is Full");
}
break;
case 2: //dequeue
if (front <= rear) {//
System.out.println("Data out: " + queue[front]);
front++;

System.out.println("front: " + front + ", rear: " + rear);
} else {
System.err.println("Queue is Empty");
}
break;
case 3:
for (int i = front; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
break;
case 4:
System.out.println("Exit...");
}
} while (choice != 4);
System.err.println("Program terminated");
}
}


I mark bold for the front declaration and statements when dequeue, the front is a variable that can change, the first initial is -1 and it increment when dequeue, while in dequeue, it only increment the front, and the data of queue is not moving, all the remain data should move forwards.

Below is my program, the diferent is I made the front as constant, because it always 0, and while dequeue, all of the data is moving forwards, and the rear is decremented. And I also comment the checking if rear == 0 then front = 0, I make the font bold. About the initialization of rear as -1, is because I follow my senior, It only initialization, you can initialize rear as 0, or whatever the number is doesn't problem because it will checked inside the program.

package stack.queue;

import java.util.Scanner;

public class MyQueueUsingStack {
static final int MAXQUEUE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int[] queue = new int[5];
final int FRONT = 0;
int rear = -1;
int choice;

do {
System.out.println("*******************************");
System.out.println("Menu:");
System.out.println("1. ENQUEUE\t2. DEDQUEUE\t3. VIEW\t4. EXIT");
System.out.println("*******************************");
System.out.print("Please Choose Menu : ");
choice = scanner.nextInt();
switch (choice) {
case 1://enqueue
if (rear < MAXQUEUE - 1) { //is queue not full yet?
System.out.print("Data enter = ");
queue[rear + 1] = scanner.nextInt();
rear++;
System.out.println();
// if(rear == 0) front = 0;
} else {
System.err.println("Queue is Full");
}
break;
case 2: //dequeue
if (rear >= FRONT) {//is queue not empty yet?
System.out.println("Data out: " + queue[FRONT]);
//queue is moving
for (int i = FRONT; i < rear; i++) {
queue[i] = queue[i+1];
}
rear--;

System.out.println("front: " + FRONT + ", rear: " + rear);
} else {
System.err.println("Queue is Empty");
}
break;
case 3:
for (int i = FRONT; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
break;
case 4:
System.out.println("Exit...");
}
} while (choice != 4);
System.err.println("Program terminated");
}
}

No comments: