Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 2 February 2019

FIRST WEEK TERM WORK Program 1

1) Write a Java Program that implements a Queue using OOP concepts.

package Program1;

import java.util.*;

class Queue {
public int Q[];
public int top;
public int rear = 0;

Queue(int size) {
Q = new int[size];
top = -1;
rear = 0;
}

public void EnQueue(int item) {
if (top == Q.length - 1) {
System.out.println("Queue overflow");
} else {
Q[++top] = item;
System.out.println("EnQueue");
}
}

public int DeQueue() {
if (top < 0) {
System.out.println("Queue underflow");
return 0;
} else {
int a = Q[rear];
for (int i = 0; i < Q.length - 1; i++) {
Q[i] = Q[i + 1];
}
top--;
return a;
}
}

public void display() {
if (top < 0) {
System.out.println("Queue underflow");
}

for (int i = 0; i <= top; i++) {
System.out.println(Q[i]);
}
}

}

public class QueueOperation {

public static void main(String args[]) {
int m = 1;
Queue obj = new Queue(5);
Scanner input = new Scanner(System.in);
do {
System.out.println("QUEUE MENU");
System.out.println("Press 1 For EnQueue");
System.out.println("Press 2 For DeQueue");
System.out.println("Press 3 For Display");
System.out.println("Press 4 For Exit");
System.out.println("Enter the operation");
int Op = input.nextInt();
switch (Op) {
case 1:
System.out.print("Enter the Element u want insert");
int n = input.nextInt();
obj.EnQueue(n);
break;
case 2:
n = obj.DeQueue();
System.out.println("Element DeQueue:" + n);
break;
case 3:
obj.display();
break;
case 4:
System.out.println("Exit");
m = 0;
break;
default:
System.out.println("Enter Proper Input");
}
} while (m == 1);
}
}


0 comments:

Post a Comment