0% found this document useful (0 votes)
30 views8 pages

Slot5 Stack Queu

The document provides implementations of a stack and queue of integer values using Java generics and collection classes. It defines common operations like push, pop, peek, and traverse for the stack, and enqueue, dequeue, peek, and traverse for the queue. The implementations also include methods to convert decimal numbers to their binary representations using the stack and queue structures.

Uploaded by

Đức Trung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

Slot5 Stack Queu

The document provides implementations of a stack and queue of integer values using Java generics and collection classes. It defines common operations like push, pop, peek, and traverse for the stack, and enqueue, dequeue, peek, and traverse for the queue. The implementations also include methods to convert decimal numbers to their binary representations using the stack and queue structures.

Uploaded by

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

Question 1.

Write a Java program to implement a stack of integer values with the following
operations:
import java.util.EmptyStackException;

import java.util.Stack;

public class IntStack {

private Stack<Integer> stack;

public IntStack() {

stack = new Stack<>();

public boolean isEmpty() {

return stack.isEmpty();

public void clear() {

stack.clear();

public void push(int x) {

stack.push(x);

}
public int pop() {

if (isEmpty()) {

throw new EmptyStackException();

return stack.pop();

public int top() {

if (isEmpty()) {

throw new EmptyStackException();

return stack.peek();

public void traverse() {

for (int i = stack.size() - 1; i >= 0; i--) {

System.out.print(stack.get(i) + " ");

System.out.println();

}
public void convertToBinary(int decimalNumber) {

IntStack binaryStack = new IntStack();

while (decimalNumber > 0) {

int remainder = decimalNumber % 2;

binaryStack.push(remainder);

decimalNumber /= 2;

System.out.print("Binary representation: ");

binaryStack.traverse();

public static void main(String[] args) {

IntStack stack = new IntStack();

stack.push(5);

stack.push(10);

stack.push(7);

System.out.println("Stack is empty: " + stack.isEmpty());

stack.traverse();

System.out.println("Top element: " + stack.top());

System.out.println("Popped element: " + stack.pop());


stack.traverse();

stack.clear();

System.out.println("Stack is empty after clearing: " + stack.isEmpty());

stack.convertToBinary(13);

Question 2. Write a Java program to implement a queue of integer values with the following
operations:
import java.util.LinkedList;

import java.util.Queue;

import java.util.NoSuchElementException;

public class IntQueue {

private Queue<Integer> queue;

public IntQueue() {

queue = new LinkedList<>();

public boolean isEmpty() {

return queue.isEmpty();
}

public void clear() {

queue.clear();

public void enqueue(int x) {

queue.offer(x);

public int dequeue() {

if (isEmpty()) {

throw new NoSuchElementException("Queue is empty");

return queue.poll();

public int first() {

if (isEmpty()) {

throw new NoSuchElementException("Queue is empty");

return queue.peek();
}

public void traverse() {

for (int item : queue) {

System.out.print(item + " ");

System.out.println();

public void convertToBinary(double decimalNumber) {

if (decimalNumber >= 1 || decimalNumber <= 0) {

System.out.println("Invalid input. Please enter a real number between 0 and 1.");

return;

IntStack binaryStack = new IntStack();

while (decimalNumber > 0) {

decimalNumber *= 2;

if (decimalNumber >= 1) {

binaryStack.push(1);

decimalNumber -= 1;

} else {
binaryStack.push(0);

System.out.print("Binary representation: ");

binaryStack.traverse();

public static void main(String[] args) {

IntQueue queue = new IntQueue();

queue.enqueue(5);

queue.enqueue(10);

queue.enqueue(7);

System.out.println("Queue is empty: " + queue.isEmpty());

queue.traverse();

System.out.println("First element: " + queue.first());

System.out.println("Dequeued element: " + queue.dequeue());

queue.traverse();

queue.clear();

System.out.println("Queue is empty after clearing: " + queue.isEmpty());


queue.convertToBinary(0.625);

You might also like