---
# System prepended metadata

title: 資料結構 20221017_StackByArray
tags: [java, Data Structure]

---

# 資料結構 20221017_StackByArray

>撰寫人[name=AmiYaku1049] [首頁--天空路1049號](https://skys-kid-lai.github.io/1004/)
>>最後編輯[time=Wed, Jan 18, 2023 7:45 PM]
___
## 題目敘述
沒有。
就是堆疊實作。
## 程式碼
```java=
import java.io.*;

class ClassWork_1110832059_1017_stackByArray 
{
	public static void main(String[] args) throws IOException 
	{
		BufferedReader buf;
		int val;
		StackByArray stack = new StackByArray(10);
		buf = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please push ten number: ");

		for(int i = 0; i < 10; i++)
		{
			val = Integer.parseInt(buf.readLine());
			stack.Push(val);
		}
		System.out.println("========================");

		while(!stack.Empty())
			System.out.println("The stack in array is: " + stack.Pop());
	}
}

class StackByArray
{
	private int[] stack;
	private int TopNum;
	
	public StackByArray(int StackSize)
	{
		stack = new int [StackSize];
		TopNum =- 1;
	}

	public boolean Push(int data)
	{
		if(TopNum >= stack.length)
		{
			System.out.println("The stack is full, cannot insert anymore");
			return false;
		}
		else
		{
			stack[++TopNum] = data;
			return true;
		}
	}

	public boolean Empty()
	{
		if(TopNum == -1)
			return true;
		else
			return false;
	}

	public int Pop()
	{
		if(Empty())
			return -1;
		else
			return stack[TopNum--];
	}
}
```

## 結果圖示
![](https://i.imgur.com/be0tQOh.png =50%x)

## 用書資訊：
>書名：圖解資料結構 使用java（第三版）
>作者：吳燦銘、胡昭民
>出版社：博碩文化
>出版年份：2018年05月