Java Exercise
In this exercise, you will create a Java program that implements the **Stacks** and **Queues** data structures. You will begin by creating a **Stack** class that allows you to stack and pop elements. Then, you will create a **Queue** class that allows you to insert and remove elements in the proper order. Finally, you will implement a program that demonstrates the use of both structures to handle data efficiently and orderly.
Instructions:
- Create a class called
Stack
that allows you to stack and pop elements.
- Create a class called
Queue
that allows you to insert and delete elements in order.
- In the main method, create objects of type
Stack
and Queue
, insert and delete elements, and display the result of each operation.
- Demonstrate how these data structures can be useful in situations where the order of the elements is crucial.
This exercise will help you understand how **Stacks** and **Queues** work in Java and how they can be applied to solve problems in data handling and flow control.
View Example Code
// Stack class
class Stack {
private int maxSize = 10;
private int top = -1;
private int[] stack = new int[maxSize];
public void push(int value) {
if (top < maxSize - 1) {
stack[++top] = value;
System.out.println(value + " pushed.");
} else {
System.out.println("Stack full. Cannot push.");
}
}
public void pop() {
if (top >= 0) {
System.out.println(stack[top--] + " popped.");
} else {
System.out.println("Stack empty. Cannot pop.");
}
}
public boolean isEmpty() {
return top == -1;
}
}
// Queue class
class Queue {
private int maxSize = 10;
private int front = -1;
private int rear = -1;
private int[] queue = new int[maxSize];
public void enqueue(int value) {
if (rear < maxSize - 1) {
if (front == -1) front = 0;
queue[++rear] = value;
System.out.println(value + " enqueued.");
} else {
System.out.println("Queue full. Cannot enqueue.");
}
}
public void dequeue() {
if (front <= rear && front != -1) {
System.out.println(queue[front++] + " dequeued.");
if (front > rear) front = rear = -1; // Queue empty
} else {
System.out.println("Queue empty. Cannot dequeue.");
}
}
public boolean isEmpty() {
return front == -1;
}
}
public class Main {
public static void main(String[] args) {
// Create Stack and Queue objects
Stack stack = new Stack();
Queue queue = new Queue();
// Stack operations
stack.push(10);
stack.push(20);
stack.pop();
stack.pop();
stack.pop(); // Attempt to pop from an empty stack
// Queue operations
queue.enqueue(100);
queue.enqueue(200);
queue.dequeue();
queue.dequeue();
queue.dequeue(); // Attempt to dequeue from an empty queue
}
}
Output:
10 stacked.
20 stacked.
20 popped.
10 popped.
Empty stack. Cannot pop.
100 inserted into queue.
200 inserted into queue.
100 removed from queue.
200 removed from queue.
Empty queue. Cannot remove.
This program demonstrates how the **Stacks** and **Queues** data structures are implemented in Java. The Stack
class allows elements to be stacked and popped, while the Queue
class allows elements to be inserted and removed in order. When you run the program, you can see how these elements are managed in both structures, following their respective rules of behavior.