0% found this document useful (0 votes)
25 views4 pages

STACKS and QEUE

The document defines classes for a Stack and Queue data structure. The Stack class implements push(), pop(), isEmpty(), and display() methods. The pop() method has additional logic to remove all instances of the number 5 from the stack. The Queue class implements enqueue(), dequeue(), isEmpty(), and display() methods to add and remove items from the queue. The main method provides a menu driver to test the stack and queue operations.

Uploaded by

Clark james Hibz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

STACKS and QEUE

The document defines classes for a Stack and Queue data structure. The Stack class implements push(), pop(), isEmpty(), and display() methods. The pop() method has additional logic to remove all instances of the number 5 from the stack. The Queue class implements enqueue(), dequeue(), isEmpty(), and display() methods to add and remove items from the queue. The main method provides a menu driver to test the stack and queue operations.

Uploaded by

Clark james Hibz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

class Stack {
private int[] stack;
private int top;

public Stack(int size) {


stack = new int[size];
top = -1;
}

public void push(int item) {


if (top >= stack.length - 1) {
System.out.println("Stack is full");
return;
}
stack[++top] = item;
System.out.println("Pushed " + item + " into stack");
display();
}

public void pop() {


if (top < 0) {
System.out.println("Stack is empty");
return;
}
if (stack[top] == 5) {
int indexToRemove = -1;
for (int i = top; i >= 0; i--) {
if (stack[i] == 5) {
indexToRemove = i;
break;
}
}
if (indexToRemove != -1) {
System.out.println("Popped " + stack[indexToRemove] + " from
stack");
for (int i = indexToRemove; i < top; i++) {
stack[i] = stack[i + 1];
}
top--;
} else {
System.out.println("5 not found in stack");
}
} else {
System.out.println("Popped " + stack[top--] + " from stack");
}
display();
}

public boolean isEmpty() {


return top < 0;
}

public void display() {


System.out.print("Stack: ");
for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}

class Queue {
private int[] queue;
private int front;
private int rear;

public Queue(int size) {


queue = new int[size];
front = -1;
rear = -1;
}

public void enqueue(int item) {


if (rear >= queue.length - 1) {
System.out.println("Queue is full");
return;
}
if (rear == -1) {
front = 0;
}
queue[++rear] = item;
System.out.println("Enqueued " + item + " into queue");
display();
}

public void dequeue() {


if (front == -1 || front > rear) {
System.out.println("Queue is empty");
return;
}
System.out.println("Dequeued " + queue[front++] + " from queue");
display();
}

public boolean isEmpty() {


return front == -1 || front > rear;
}

public void display() {


System.out.print("Queue: ");
for (int i = front; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Stack stack = new Stack(10);
Queue queue = new Queue(10);
int choice;
String item;

// STACK Operations
while (true) {
System.out.println("\nSTACK Operations:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display Stack");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter item to push: ");
item = scanner.next();
stack.push(Integer.parseInt(item));
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
System.out.println("Exiting STACK operations...");
break;
default:
System.out.println("Invalid choice");
}

if (choice == 4) {
break;
}
}

// QUEUE Operations
while (true) {
System.out.println("\nQUEUE Operations:");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Display Queue");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter item to enqueue: ");
item = scanner.next();
queue.enqueue(Integer.parseInt(item));
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
System.out.println("Exiting QUEUE operations...");
break;
default:
System.out.println("Invalid choice");
}

if (choice == 4) {
break;
}
}

scanner.close();
}
}

You might also like