题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
// Node实现stack类
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static Stack <Integer> stack = new Stack<>();
public static final String str1 = "push";
public static final String str2 = "pop";
public static final String str3 = "top";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) {
//第一行为一个正整数 n ,代表操作次数。(1≤n≤100000)(1≤n≤100000)
int n = Integer.parseInt(in.nextLine());
String [] strs = new String[n];
for (int i = 0; i < n; i++) {
// 接下来的 n 行,每行为一个字符串,代表一个操作。保证操作是题目描述中三种中的一种。
strs[i] = in.nextLine();
}
for (String cmd : strs) {
if (cmd.startsWith(str1)) {
// push x 将x入栈 保证 x 为int类型整数
int x = Integer.parseInt(cmd.split(" ")[1]);
stack.push(x);
} else if (cmd.startsWith(str2)) {
// pop 输出栈顶,并让栈顶出栈
int len = stack.length;
if (len == 0) {
System.out.println("error");
} else {
int r = stack.pop();
System.out.println(r);
}
} else if (cmd.startsWith(str3)) {
// top 输出栈顶,栈顶不出栈
int len = stack.length;
if (len == 0) {
System.out.println("error");
} else {
int r = stack.top();
System.out.println(r);
}
}
}
}
}
}
class Stack<T> {
private class Node<T> {
public T val;
private Node next;
public Node(T val, Node next) {
this.val = val;
this.next = next;
}
}
Node head;
int length;
public Stack () {
this.head = new Node(null, null);
this.length = 0;
}
public void push(T val) {
if (length == 0) {
Node newNode = new Node<>(val, null);
head.next = newNode;
} else {
Node a = head.next;
Node newNode = new Node<>(val, a);
head.next = newNode;
}
length++;
}
public T pop() {
if (head.next == null) {
return null;
}
Node a = head.next;
Node b = head.next.next;
head.next = b;
length--;
return (T)a.val;
}
public T top() {
if (head.next == null) {
return null;
}
return (T)head.next.val;
}
}
- 使用LinkedList
import java.util.Scanner;
import java.util.LinkedList;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static LinkedList <Integer> list = new LinkedList<>();
public static final String str1 = "push";
public static final String str2 = "pop";
public static final String str3 = "top";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) {
//第一行为一个正整数 n ,代表操作次数。(1≤n≤100000)(1≤n≤100000)
int n = Integer.parseInt(in.nextLine());
String [] strs = new String[n];
for (int i = 0; i < n; i++) {
// 接下来的 n 行,每行为一个字符串,代表一个操作。保证操作是题目描述中三种中的一种。
strs[i] = in.nextLine();
}
for (String cmd : strs) {
if (cmd.startsWith(str1)) {
// push x 将x入栈 保证 x 为int类型整数
int x = Integer.parseInt(cmd.split(" ")[1]);
push(x);
} else if (cmd.startsWith(str2)) {
// pop 输出栈顶,并让栈顶出栈
pop();
} else if (cmd.startsWith(str3)) {
// top 输出栈顶,栈顶不出栈
top();
}
}
}
}
public static void push(int x) {
list.add(0,x);
}
public static void pop() {
if (list.size() == 0) {
// 为空 返回error
System.out.println("error");
} else {
// 输出栈顶并出栈
int res = list.getFirst();
list.removeFirst();
System.out.println(res);
}
}
public static void top() {
if (list.size() == 0) {
// 为空 返回error
System.out.println("error");
} else {
// 输出栈顶 不出栈
int res = list.getFirst();
System.out.println(res);
}
}
}
`
查看13道真题和解析
