andy6804tw
12/5/2016 - 7:11 AM

LinkList_Stack.java

import java.util.*;

class MyStack {

	private LinkedList<String> linkedList;

	// 建構子初始化字串型態
	public MyStack() {
		linkedList = new LinkedList<String>();
	}

	// 推入堆疊
	public void push(String name) {
		linkedList.addFirst(name);
	}

	// 取得最上層
	public String top() {
		return linkedList.getFirst();
	}

	// 取出並刪除
	public String pop() {
		return linkedList.removeFirst();
	}

	// 判斷是否為空
	public boolean isEmpty() {
		return linkedList.isEmpty();
	}
}

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		MyStack list = new MyStack();
		list.push("This is Stack1");
		list.push("This is Stack2");
		list.push("This is Stack3");
		while (!list.isEmpty())
			System.out.println(list.pop());
	}

}