Skip to content

Commit 5b6ff0d

Browse files
committed
新增java实现单向链表栈
1 parent 25f5334 commit 5b6ff0d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: 使用Java实现一个单向链表实现的栈
3+
date: 2025-07-04 14:53
4+
description: java单向链表实现栈
5+
image: "../public/assets/images/arch1.jpg"
6+
category: 记录
7+
tags:
8+
- Java
9+
published: true
10+
sitemap: true
11+
---
12+
13+
```java
14+
15+
public class Node<T> {
16+
public T value;
17+
public Node<T> next;
18+
19+
public Node(T value){
20+
this.value = value;
21+
}
22+
}
23+
24+
public class LinkListStack<T> {
25+
public Node<T> head;
26+
27+
public LinkListStack() {
28+
head = new Node<>(null);
29+
}
30+
31+
public T pop() {
32+
T v = head.value;
33+
head = head.next;
34+
return v;
35+
}
36+
37+
public void push(T value) {
38+
Node<T> node = new Node<T>(value);
39+
node.next = head;
40+
head = node;
41+
}
42+
}
43+
44+
public class Main {
45+
public static void main(String[] args) {
46+
LinkListStack<Integer> stack = new LinkListStack<>();
47+
stack.push(100);
48+
stack.push(1145);
49+
while (stack.head.value != null) {
50+
System.out.println(stack.pop());
51+
}
52+
}
53+
}
54+
```
55+
56+
> 栽跟头在这种题上可以抽自己几嘴巴了

0 commit comments

Comments
 (0)