You are on page 1of 5

Question 1: Implement ‘Stack’ data structures using integer array

import java.util.Scanner;
class Stack
{
int top;
int maxsize = 10;
int[] arr = new int[maxsize];

boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push (Scanner sc)
{
if(top == maxsize-1)
{
System.out.println("Overflow !!");
return false;
}
else
{
System.out.println("Enter Value");
int val = sc.nextInt();
top++;
arr[top]=val;
System.out.println("Item pushed");
return true;
}
}
boolean pop ()
{
if (top == -1)
{
System.out.println("Underflow !!");
return false;
}
else
{
top --;
System.out.println("Item popped");
return true;
}
}
void display ()
{
System.out.println("Printing stack elements .....");
for(int i = top; i>=0;i--)
{
System.out.println(arr[i]);
}
}
}
public class Stack_Implementation {
public static void main(String[] args) {
int choice=0;
Scanner sc = new Scanner(System.in);
Stack s = new Stack();

while(choice != 4)
{
System.out.println("\nChose one from the below options...\n");
System.out.println("\n1.Push\n2.Pop\n3.Show\n4.Exit");
System.out.println("\n Enter your choice \n");
choice = sc.nextInt();
switch(choice)
{
case 1:
{
s.push(sc);
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
case 4:
{
System.out.println("Exiting....");
System.exit(0);
break;
}
default:
{
System.out.println("Please Enter valid choice ");
}
};
}
}
}

Output:

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit

Enter your choice

1
Enter Value
45
Item pushed

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit

Enter your choice


1
Enter Value
25
Item pushed

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit

Enter your choice

2
Item popped

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit

Enter your choice

3
Printing stack elements .....
45

Chose one from the below options...

1.Push
2.Pop
3.Show
4.Exit

Enter your choice

4
Exiting....

Question 2: Implement ‘Queue’ data structures using integer array

public class QueueUsingArray {

private int capacity;


int queueArr[];
int front;
int rear;
int currentSize = 0;

public QueueUsingArray(int sizeOfQueue) {


this.capacity = sizeOfQueue;
front = 0;
rear = -1;
queueArr = new int[this.capacity];
}

public void enqueue(int data) {


if (isFull()) {
System.out.println("Queue is full!! Can not add more elements");
} else {
rear++;
if (rear == capacity - 1) {
rear = 0;
}
queueArr[rear] = data;
currentSize++;
System.out.println(data + " added to the queue");
}
}

public void dequeue() {


if (isEmpty()) {
System.out.println("Queue is empty!! Can not dequeue element");
} else {
front++;
if (front == capacity - 1) {
System.out.println(queueArr[front - 1] + " removed from the queue");
front = 0;
} else {
System.out.println(queueArr[front - 1] + " removed from the queue");
}
currentSize--;
}
}

public boolean isFull() {


if (currentSize == capacity) {
return true;
}
return false;
}

public boolean isEmpty() {

if (currentSize == 0) {
return true;
}
return false;
}

public static void main(String a[]) {

QueueUsingArray queue = new QueueUsingArray(5);


queue.enqueue(6);
queue.dequeue();
queue.enqueue(5);
queue.enqueue(4785);
queue.enqueue(254);
queue.dequeue();
queue.enqueue(655);
queue.dequeue();
queue.enqueue(257);
queue.enqueue(669);
queue.dequeue();
queue.enqueue(224);
queue.enqueue(784); }
}

Output:

6 added to the queue


6 removed from the queue
5 added to the queue
4785 added to the queue
254 added to the queue
5 removed from the queue
655 added to the queue
4785 removed from the queue
257 added to the queue
669 added to the queue
254 removed from the queue
224 added to the queue
784 added to the queue

You might also like