add linked list stack

This commit is contained in:
ehlxr 2021-04-16 09:05:48 +08:00
parent 6c8b22b204
commit 612feb1e9e

View File

@ -31,10 +31,7 @@ import java.util.Scanner;
* @since 2021-04-15 22:18. * @since 2021-04-15 22:18.
*/ */
public class LinkedListStackDemo { public class LinkedListStackDemo {
public static void main(String[] args) { public static void main(String[] args) {
//测试一下ArrayStack 是否正确
//先创建一个ArrayStack对象->表示栈
LinkedListStack stack = new LinkedListStack(4); LinkedListStack stack = new LinkedListStack(4);
String key; String key;
boolean loop = true; //控制是否退出菜单 boolean loop = true; //控制是否退出菜单
@ -119,9 +116,9 @@ class LinkedListStack {
Node temp = head; Node temp = head;
while (true) { while (true) {
if (temp.next == null) { // if (temp.next == null) {
throw new RuntimeException("栈空"); // throw new RuntimeException("栈空~~~");
} // }
if (temp.next.next == null) { if (temp.next.next == null) {
top--; top--;
int id = temp.next.id; int id = temp.next.id;
@ -152,23 +149,23 @@ class LinkedListStack {
} }
/**
* 反转单向链表
*/
private void revert() { private void revert() {
Node pre = null; Node pre = null;
Node cur = head.next; Node cur = head.next;
while (cur != null) { while (cur != null) {
Node temp = cur; Node next = cur.next;
cur = cur.next; cur.next = pre;
pre = cur;
temp.next = pre; cur = next;
pre = temp;
} }
head.next = pre; head.next = pre;
} }
} }
class Node { class Node {
public int id; public int id;
public Node next; public Node next;