Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Stack implementation using singly linked list.
* Supports push, pop, peek, isEmpty, and size operations in O(1) time.
*
* Issue: #6715
* Author: @Kamal1023
*/
public class StackUsingLinkedList<T> {
private Node<T> top;
private int size;

// Inner Node class
private static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}

//Pushes an element onto the stack
public void push(T data) {
Node<T> node = new Node<>(data);
node.next = top;
top = node;
size++;
}

// Removes and returns the top element of the stack
public T pop() {
if (isEmpty()) throw new RuntimeException("Stack is empty");
T data = top.data;
top = top.next;
size--;
return data;
}

/** Returns the top element without removing it */
public T peek() {
if (isEmpty()) throw new RuntimeException("Stack is empty");
return top.data;
}

/** Checks if the stack is empty */
public boolean isEmpty() {
return top == null;
}

//Returns the number of elements in the stack
public int size() {
return size;
}

// Demo/test for StackUsingLinkedList
public static void main(String[] args) {
StackUsingLinkedList<Integer> stack = new StackUsingLinkedList<>();
stack.push(10);
stack.push(20);
stack.push(30);

System.out.println("Top: " + stack.peek()); // 30
System.out.println("Size: " + stack.size()); // 3

while (!stack.isEmpty()) {
System.out.println("Pop: " + stack.pop());
}
}
}
134 changes: 56 additions & 78 deletions src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
package com.thealgorithms.stacks;

import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;

/**
* A class that implements a stack using two queues.
* This approach ensures that the stack's LIFO (Last In, First Out) behavior
* is maintained by utilizing two queues for storage.
* The mainQueue is used to store the elements of the stack, while the tempQueue
* is used to temporarily store elements during the push operation.
* Stack implementation using singly linked list.
* Supports push, pop, peek, isEmpty, and size operations in O(1) time.
*
* Issue: #6715
* Author: @Kamal1023
*/
public class StackUsingTwoQueues {
public class StackUsingLinkedList<T> {
private Node<T> top;
private int size;

private Queue<Integer> mainQueue;
private Queue<Integer> tempQueue;

/**
* Constructs an empty stack using two queues.
*/
public StackUsingTwoQueues() {
mainQueue = new LinkedList<>();
tempQueue = new LinkedList<>();
// Inner Node class
private static class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}

/**
* Pushes an element onto the top of the stack.
* The newly pushed element becomes the top of the stack.
*
* @param item The element to be pushed onto the stack.
*/
public void push(int item) {
tempQueue.add(item);
/** Pushes an element onto the stack */
public void push(T data) {
Node<T> node = new Node<>(data);
node.next = top;
top = node;
size++;
}

// Move all elements from the mainQueue to tempQueue to maintain LIFO order
while (!mainQueue.isEmpty()) {
tempQueue.add(mainQueue.remove());
}
/** Removes and returns the top element of the stack */
public T pop() {
if (isEmpty()) throw new RuntimeException("Stack is empty");
T data = top.data;
top = top.next;
size--;
return data;
}

// Swap the names of the two queues
Queue<Integer> swap = mainQueue;
mainQueue = tempQueue;
tempQueue = swap; // tempQueue is now empty
}
/** Returns the top element without removing it */
public T peek() {
if (isEmpty()) throw new RuntimeException("Stack is empty");
return top.data;
}

/**
* Removes and returns the element at the top of the stack.
* Throws an exception if the stack is empty.
*
* @return The element at the top of the stack.
* @throws NoSuchElementException if the stack is empty.
*/
public int pop() {
if (mainQueue.isEmpty()) {
throw new NoSuchElementException("Stack is empty");
}
return mainQueue.remove();
}
/** Checks if the stack is empty */
public boolean isEmpty() {
return top == null;
}

/**
* Returns the element at the top of the stack without removing it.
* Returns null if the stack is empty.
*
* @return The element at the top of the stack, or null if the stack is empty.
*/
public Integer peek() {
if (mainQueue.isEmpty()) {
return null;
}
return mainQueue.peek();
}
/** Returns the number of elements in the stack */
public int size() {
return size;
}

/**
* Returns true if the stack is empty.
*
* @return true if the stack is empty; false otherwise.
*/
public boolean isEmpty() {
return mainQueue.isEmpty();
}
/** Demo/test for StackUsingLinkedList */
public static void main(String[] args) {
StackUsingLinkedList<Integer> stack = new StackUsingLinkedList<>();
stack.push(10);
stack.push(20);
stack.push(30);

System.out.println("Top: " + stack.peek()); // 30
System.out.println("Size: " + stack.size()); // 3

/**
* Returns the number of elements in the stack.
*
* @return The size of the stack.
*/
public int size() {
return mainQueue.size();
while (!stack.isEmpty()) {
System.out.println("Pop: " + stack.pop());
}
}
}
Loading