0% found this document useful (0 votes)
10 views3 pages

Implementing Queue with Stacks in Java

The document defines a CustomStack class that implements a stack data structure with methods for pushing, popping, peeking, and checking size and emptiness. It also defines a QueueUsingStacks class that utilizes two CustomStack instances to implement queue operations like enqueue, dequeue, and peek. The main method demonstrates the functionality of the queue by enqueuing elements, checking the size, and dequeuing an element.

Uploaded by

jahekaf484
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)
10 views3 pages

Implementing Queue with Stacks in Java

The document defines a CustomStack class that implements a stack data structure with methods for pushing, popping, peeking, and checking size and emptiness. It also defines a QueueUsingStacks class that utilizes two CustomStack instances to implement queue operations like enqueue, dequeue, and peek. The main method demonstrates the functionality of the queue by enqueuing elements, checking the size, and dequeuing an element.

Uploaded by

jahekaf484
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

class CustomStack {

int maxSize;
int[] stackArray;
int top;

public CustomStack(int size) {


maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

public boolean isFull() {


return top == maxSize - 1;
}

public boolean isEmpty() {


return top == -1;
}

public void push(int value) {


if (top == maxSize - 1) {
[Link]("Stack is full. Cannot push element: " + value);
} else {
top++;
stackArray[top] = value;
}
}

public int pop() {


if (top == -1) {
[Link]("Stack is empty. Cannot pop element.");
return -1; // Return a sentinel value to indicate an error
} else {
int poppedItem = stackArray[top];
top--;
return poppedItem;

}
}

public int peek() {


if (isEmpty()) {
[Link]("Stack is empty. Cannot peek element.");
return -1; // Return a sentinel value to indicate an error
} else {
return stackArray[top];
}
}

public int size() {


return top + 1;
}
}

public class QueueUsingStacks {


CustomStack stack1; // For enqueue operations
CustomStack stack2; // For dequeue operations

public QueueUsingStacks(int size) {


stack1 = new CustomStack(size);
stack2 = new CustomStack(size);
}

public void enqueue(int value) {


if ([Link]()) {
[Link]("Queue is full. Cannot enqueue element: " + value);
} else {
[Link](value);
}

public int dequeue() {

if ([Link]() && [Link]()) {


[Link]("Queue is empty. Cannot dequeue.");
return -1; // Return a sentinel value to indicate an error
}

if ([Link]()) {
while (![Link]()) {
int a = [Link]();
[Link](a);
}
}

return [Link]();
}

public int peek() {


if ([Link]() && [Link]()) {
[Link]("Queue is empty. Cannot peek.");
return -1; // Return a sentinel value to indicate an error
}

if ([Link]()) {
while (![Link]()) {
[Link]([Link]());
}
}

return [Link]();
}

public boolean isEmpty() {


return [Link]() && [Link]();
}

public int size() {


return [Link]() + [Link]();
}

public static void main(String[] args) {


QueueUsingStacks queue = new QueueUsingStacks(5);

[Link](10);
[Link](20);
[Link](30);
[Link]("Queue size: " + [Link]());
[Link]("Front element: " + [Link]());

int dequeuedItem = [Link]();


[Link]("Dequeued item: " + dequeuedItem);

[Link]("Queue size after dequeue: " + [Link]());


}
}

You might also like