🌐 AI搜索 & 代理 主页
Skip to content

Commit c71a95e

Browse files
author
Lord-of-Algorithms
committed
Add stack implementations
1 parent 1eece86 commit c71a95e

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package lineards.stack;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* A dynamic array implementation of a stack that dynamically resizes as elements are added or removed.
7+
*/
8+
public class DynamicArrayStack implements Stack {
9+
10+
private char[] data;
11+
private int top;
12+
13+
/**
14+
* Constructor to initialize the stack with an initial capacity.
15+
*/
16+
public DynamicArrayStack(int capacity) {
17+
if (capacity < 1) {
18+
throw new IllegalArgumentException("Initial capacity must be at least 1");
19+
}
20+
data = new char[capacity];
21+
top = -1;
22+
}
23+
24+
@Override
25+
public boolean isEmpty() {
26+
return top == -1;
27+
}
28+
29+
@Override
30+
public void push(char value) {
31+
if (top + 1 == data.length) {
32+
resize(data.length * 2); // Double the size of the array if it is full
33+
}
34+
data[++top] = value;
35+
}
36+
37+
@Override
38+
public char pop() {
39+
if (isEmpty())
40+
throw new NoSuchElementException("Stack is empty");
41+
42+
char value = data[top--];
43+
44+
// Reduce the size of the array if it is less than 1/4 full
45+
if (top + 1 > 0 && top + 1 == data.length / 4) { // The top + 1 > 0 ensures that there’s at least one element in the stack. The number of elements in the stack is actually top + 1.
46+
resize(data.length / 2);
47+
}
48+
49+
return value;
50+
}
51+
52+
@Override
53+
public char peek() {
54+
if (isEmpty()) {
55+
throw new NoSuchElementException("Stack is empty");
56+
}
57+
return data[top];
58+
}
59+
60+
/**
61+
* Resizes the internal array to the specified new capacity.
62+
*
63+
* @param newCapacity The new capacity of the array
64+
*/
65+
private void resize(int newCapacity) {
66+
char[] newArray = new char[newCapacity];
67+
System.arraycopy(data, 0, newArray, 0, top + 1);
68+
data = newArray;
69+
}
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package lineards.stack;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Implements the Stack interface using a singly linked list.
7+
* This implementation uses a custom Node class to represent each element in the stack,
8+
* allowing dynamic memory allocation as elements are added or removed.
9+
*/
10+
public class LinkedListStack implements Stack {
11+
12+
/**
13+
* Node represents an element in the stack,
14+
* containing a value and reference to the next Node.
15+
*/
16+
private static class Node {
17+
final char data;
18+
Node next;
19+
20+
/**
21+
* Constructs a new Node with the given data value.
22+
*/
23+
Node(char data) {
24+
this.data = data;
25+
}
26+
}
27+
28+
// Top element of the stack
29+
private Node top;
30+
31+
@Override
32+
public boolean isEmpty() {
33+
return top == null;
34+
}
35+
36+
@Override
37+
public void push(char value) {
38+
// Create a node
39+
Node node = new Node(value);
40+
// Make the new node the top element
41+
node.next = top;
42+
top = node;
43+
}
44+
45+
@Override
46+
public char pop() {
47+
if (isEmpty())
48+
throw new NoSuchElementException("Stack is empty");
49+
char value = top.data;
50+
// Remove the top element
51+
top = top.next;
52+
return value;
53+
}
54+
55+
@Override
56+
public char peek() {
57+
if (isEmpty()) {
58+
throw new NoSuchElementException("Stack is empty");
59+
}
60+
return top.data;
61+
}
62+
}

src/lineards/stack/Stack.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lineards.stack;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Defines the interface for a stack.
7+
*/
8+
interface Stack {
9+
10+
/**
11+
* Checks if the stack is empty.
12+
*
13+
* @return true if there are no elements in the stack, false otherwise.
14+
*/
15+
boolean isEmpty();
16+
17+
/**
18+
* Adds a character to the top of the stack.
19+
*
20+
* @param value The character to be pushed onto the stack.
21+
*/
22+
void push(char value);
23+
24+
/**
25+
* Removes and returns the character at the top of the stack.
26+
*
27+
* @return The character at the top of the stack.
28+
* @throws NoSuchElementException if the stack is empty.
29+
*/
30+
char pop();
31+
32+
/**
33+
* Retrieves, but does not remove, the character at the top of the stack.
34+
*
35+
* @return The character currently at the top of the stack.
36+
* @throws NoSuchElementException if the stack is empty.
37+
*/
38+
char peek();
39+
}

src/lineards/stack/StackMain.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package lineards.stack;
2+
3+
/**
4+
* Demonstrates the functionalities of different stack implementations.
5+
*/
6+
public class StackMain {
7+
8+
public static void main(String[] args) {
9+
// Demo for StaticArrayStack
10+
demoStaticArrayStack();
11+
12+
// Demo for DynamicArrayStack
13+
demoDynamicArrayStack();
14+
15+
// Demo for LinkedListStack
16+
demoLinkedListStack();
17+
}
18+
19+
private static void demoStaticArrayStack() {
20+
System.out.println("Demo: StaticArrayStack");
21+
StaticArrayStack staticStack = new StaticArrayStack(3);
22+
System.out.println("Push A, B, C");
23+
staticStack.push('A');
24+
staticStack.push('B');
25+
staticStack.push('C');
26+
27+
try {
28+
System.out.println("Pushing D...");
29+
staticStack.push('D'); // This should fail because the stack is full
30+
} catch (IllegalStateException e) {
31+
System.out.println("Caught exception: " + e.getMessage());
32+
}
33+
34+
System.out.println("Pop from stack: " + staticStack.pop()); // Should be C
35+
System.out.println("Peek from stack: " + staticStack.peek()); // Should be B
36+
System.out.println("Is empty: " + staticStack.isEmpty()); // Should be false
37+
staticStack.pop();
38+
staticStack.pop();
39+
System.out.println("Is empty after popping all elements: " + staticStack.isEmpty()); // Should be true
40+
System.out.println();
41+
}
42+
43+
private static void demoDynamicArrayStack() {
44+
System.out.println("Demo: DynamicArrayStack");
45+
DynamicArrayStack dynamicStack = new DynamicArrayStack(2);
46+
System.out.println("Push W, X, Y, Z");
47+
dynamicStack.push('W');
48+
dynamicStack.push('X');
49+
dynamicStack.push('Y'); // Should resize and allow pushing another element
50+
dynamicStack.push('Z');
51+
52+
System.out.println("Pop from stack: " + dynamicStack.pop()); // Should be Z
53+
System.out.println("Peek from stack: " + dynamicStack.peek()); // Should be Y
54+
System.out.println("Is empty: " + dynamicStack.isEmpty()); // Should be false
55+
dynamicStack.pop();
56+
dynamicStack.pop();
57+
dynamicStack.pop();
58+
System.out.println("Is empty after popping all elements: " + dynamicStack.isEmpty()); // Should be true
59+
System.out.println();
60+
}
61+
62+
private static void demoLinkedListStack() {
63+
System.out.println("Demo: LinkedListStack");
64+
LinkedListStack linkedListStack = new LinkedListStack();
65+
System.out.println("Push 1, 2, 3");
66+
linkedListStack.push('1');
67+
linkedListStack.push('2');
68+
linkedListStack.push('3');
69+
70+
System.out.println("Pop from stack: " + linkedListStack.pop()); // Should be 3
71+
System.out.println("Peek from stack: " + linkedListStack.peek()); // Should be 2
72+
System.out.println("Is empty: " + linkedListStack.isEmpty()); // Should be false
73+
linkedListStack.pop();
74+
linkedListStack.pop();
75+
System.out.println("Is empty after popping all elements: " + linkedListStack.isEmpty()); // Should be true
76+
System.out.println();
77+
}
78+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package lineards.stack;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Implementation of the {@link Stack} interface using a static-sized array.
7+
*/
8+
public class StaticArrayStack implements Stack {
9+
10+
// Array to store the elements
11+
private final char[] data;
12+
// Index of the top element
13+
private int top;
14+
15+
/**
16+
* Constructor to initialize the stack with a given size.
17+
*/
18+
public StaticArrayStack(int size) {
19+
if (size < 1) {
20+
throw new IllegalArgumentException("Size must be at least 1");
21+
}
22+
data = new char[size];
23+
top = -1;
24+
}
25+
26+
@Override
27+
public boolean isEmpty() {
28+
return top == -1;
29+
}
30+
31+
/**
32+
* Adds a character to the top of the stack.
33+
*
34+
* @param value The character to be pushed onto the stack.
35+
* @throws IllegalStateException if the stack is full.
36+
*/
37+
@Override
38+
public void push(char value) {
39+
if (isFull()) {
40+
throw new IllegalStateException("Stack is full");
41+
}
42+
// Increment top and add element
43+
data[++top] = value;
44+
}
45+
46+
@Override
47+
public char pop() {
48+
if (isEmpty())
49+
throw new NoSuchElementException("Stack is empty");
50+
// Decrement top which causes the
51+
// element to be removed
52+
return data[top--];
53+
}
54+
55+
@Override
56+
public char peek() {
57+
if (isEmpty()) {
58+
throw new NoSuchElementException("Stack is empty");
59+
}
60+
return data[top];
61+
}
62+
63+
/**
64+
* Checks if the stack is full.
65+
*/
66+
private boolean isFull() {
67+
return top == data.length - 1;
68+
}
69+
}

0 commit comments

Comments
 (0)