Saturday, May 16, 2009

Queue and Stack With Linked List


This post is based on my senior post, his post is a C program about queue using Linked List, I made the java version and I extend it not only implementing queue with Linked List but also implementing stack.

I also improve my coding style, in which Im not write all of the stuff in main method, but I divide them into their own methods, so I have enqueue and dequeue method for queue, push and pop method for stack, also view and clear linked list method. So I only need call them in main method, it more clear and readable.

package stack.queue;

import java.util.Scanner;

class Node {
int info;
Node next;

public Node(int d) {
info = d;
}
}

public class LinkedListImpl {
static Node newNode;
static Node front;
static Node rear;
static Node tempNode;
static Scanner scanner = new Scanner(System.in);

public static void enqueue() {
System.out.print("Data enter: ");
newNode = new Node(scanner.nextInt());
if (front == null) {
front = newNode;
rear = newNode; //rear is never null, because if front is null,
//the new Node is also the rear
} else {
rear.next = newNode;
rear = newNode; //nothing happen with the front
}
System.out.println();
}
public static void dequeue() {
if (front != null) {
tempNode = front;
System.out.println("Data out = " + tempNode.info);
front = tempNode.next;
} else {
System.err.println("Queue is empty!");
}
System.out.println();
}
public static void push() { //push as front not as rear
System.out.print("Data enter: ");
newNode = new Node(scanner.nextInt());
if (front == null) {
front = newNode;
rear = newNode; //rear is never null, because if front is null,
//the new Node is also the rear
} else {
tempNode = front;
front = newNode;
front.next = tempNode; //nothing happen with the rear
}
System.out.println();
}
public static void pop() {
if (front != null) {
tempNode = front;
System.out.println("Data out = " + front.info);
front = tempNode.next;
} else {
System.err.println("Queue is empty!");
}
System.out.println();
}
public static void viewFromFront() {
tempNode = front;
while (tempNode != null) {
System.out.print(tempNode.info + " ");
tempNode = tempNode.next;
}
System.out.println("\n");
}
public static void clearList() {
front = null;
rear = null;
}
public static void main(String[] args) {
int choiceMain = 0;
do {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.println("Menu Implementation:");
System.out.println("1. QUEUE\t2. STACK\t3. EXIT FROM APPLICATION");
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.print("Please Choose Menu : ");
choiceMain = scanner.nextInt();
switch (choiceMain) {
case 1:
System.out.println("************************************************************");
System.out.println("Queue Menu:");
System.out.println("1. ENQUEUE\t2. DEDQUEUE\t3. VIEW\t4. EXIT FROM QUEUE");
System.out.println("************************************************************");
int choiceQueue = 0;
do {
System.out.print("Please Choose Menu : ");
choiceQueue = scanner.nextInt();
switch (choiceQueue) {
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: viewFromFront();
break;
case 4:
clearList();
System.err.println("Exiting From Queue...");
break;
}
} while (choiceQueue != 4);
break;
case 2:
System.out.println("************************************************************");
System.out.println("Stack Menu:");
System.out.println("1. PUSH\t2. POP\t3. VIEW\t4. EXIT FROM STACK");
System.out.println("************************************************************");
int choiceStack = 0;
do {
System.out.print("Please Choose Menu : ");
choiceStack = scanner.nextInt();
switch (choiceStack) {
case 1: push();
break;
case 2: pop();
break;
case 3: viewFromFront();
break;
case 4:
clearList();
System.err.println("Exiting From Stack...");
break;
}
} while (choiceStack != 4);
break;
case 3:
System.err.println("Exiting From Application...");
break;
}
} while (choiceMain != 3);
System.err.println("Application terminated.");
}
}

No comments: