# 資料結構 20221017_StackByList >撰寫人[name=AmiYaku1049] [首頁--天空路1049號](https://skys-kid-lai.github.io/1004/) >>最後編輯[time=Wed, Jan 18, 2023 7:54 PM] ___ ## 題目敘述 沒有。 就是堆疊實作。 ## 程式碼 ```java= import java.io.*; class ClassWork_1110832059_1017_stackByList { public static void main(String[] args) throws IOException { BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in)); StackByLink stack = new StackByLink(); int choice = 0; while(true) { System.out.print("[0: End, 1: Push, 2: Pop]: "); choice = Integer.parseInt(buf.readLine()); if(choice == 2) { stack.Pop(); System.out.println("The stack after the number poped: "); stack.PrintStack(); } else if(choice == 1) { System.out.print("Please insert number into the stack: "); choice = Integer.parseInt(buf.readLine()); stack.Insert(choice); System.out.println("The stack after the number inserted: "); stack.PrintStack(); } else if(choice == 0) break; else System.out.println("Insert Error!"); } } } class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class StackByLink { public Node front; public Node rea; public boolean isEmpty() { return front == null; } public void PrintStack() { Node current = front; while(current != null) { System.out.print("[" + current.data + "]"); current = current.next; } System.out.println(); } public void Insert(int data) { Node newNode = new Node(data); if(this.isEmpty()) { front = newNode; rea = newNode; } else { rea.next = newNode; rea = newNode; } } public void Pop() { Node newNode; if(this.isEmpty()) { System.out.println("It's an empty stack"); return; } newNode = front; if(newNode == rea) { front = null; rea = null; System.out.println("It's an empty stack"); } else { while(newNode.next != rea) newNode = newNode.next; newNode.next = rea.next; rea = newNode; } } } ``` ## 結果圖示  ## 用書資訊: >書名:圖解資料結構 使用java(第三版) >作者:吳燦銘、胡昭民 >出版社:博碩文化 >出版年份:2018年05月
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up