一、棧的介紹
- 棧的英文爲(stack)
- 棧是一個先入後出(FILO-First In Last Out)的有序列表。
- 棧(stack)是限制線性表中元素的插入和刪除只能在線性表的同一端進行的一種特殊線性表。允許插入和刪除的一端,爲變化的一端,稱爲棧頂(Top),另一端爲固定的一端,稱爲棧底(Bottom)。
- 根據棧的定義可知,最先放入棧中元素在棧底,最後放入的元素在棧頂,而刪除元素剛好相反,最後放入的元素最先刪除,最先放入的元素最後刪除
二、應用場景
三、代碼實現
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(5);
arrayStack.show();
arrayStack.push(1);
arrayStack.push(2);
arrayStack.push(3);
arrayStack.push(4);
arrayStack.push(5);
arrayStack.push(6);
System.out.println("添加值後的棧信息");
arrayStack.show();
int stackPop = arrayStack.pop();
System.out.println("取出的值爲:"+stackPop);
int stackPop2 = arrayStack.pop();
System.out.println("取出的值爲:"+stackPop2);
System.out.println("取出值後的棧信息");
arrayStack.show();
}
}
class ArrayStack{
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize){
this.maxSize = maxSize;
this.stack = new int[this.maxSize];
}
public boolean isEmpty(){
return this.top == -1;
}
public boolean isFull(){
return top == maxSize - 1;
}
public void push(int value){
if(isFull()){
System.out.println("棧已滿!");
return;
}
top++;
stack[top] = value;
}
public int pop(){
if(isEmpty()){
System.out.println("當前棧空");
throw new RuntimeException("當前棧無數據,不可出棧");
}
int value = stack[top];
top--;
return value;
}
public void show(){
if(isEmpty()){
System.out.println("當前棧空");
return;
}
for (int i = top;i >= 0;i--){
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
}
public class LinkedListStackDemo {
public static void main(String[] args) {
LinkedListStack linkedListStack = new LinkedListStack(5);
linkedListStack.show();
linkedListStack.push(1);
linkedListStack.push(2);
linkedListStack.push(3);
linkedListStack.push(4);
linkedListStack.push(5);
linkedListStack.push(6);
System.out.println("添加值後的棧信息");
linkedListStack.show();
int stackPop = linkedListStack.pop();
System.out.println("取出的值爲:"+stackPop);
int stackPop2 = linkedListStack.pop();
System.out.println("取出的值爲:"+stackPop2);
System.out.println("取出值後的棧信息");
linkedListStack.show();
}
}
class LinkedListStack {
private int maxSize;
private Node first = new Node(-1);
private Node top = first;
public LinkedListStack(int maxSize){
this.maxSize = maxSize;
}
public int size(){
if(first.next == null){
return 0;
}
int size = 0;
Node temp = first.next;
while (true){
size++;
if(temp == top){
break;
}
temp = temp.next;
}
return size;
}
public boolean isEmpty(){
return size() == 0;
}
public boolean isFull(){
return size() == maxSize;
}
public void push(int value){
if(isFull()){
System.out.println("棧已滿!");
return;
}
Node node = new Node(value);
top.next = node;
top = node;
}
public int pop(){
if(isEmpty()){
System.out.println("當前棧空");
throw new RuntimeException("當前棧無數據,不可出棧");
}
int value = top.number;
Node temp = first;
while (true){
if(temp.next == top){
top = temp;
break;
}
temp = temp.next;
}
return value;
}
public void show(){
if(isEmpty()){
System.out.println("當前棧空");
return;
}
Node temp;
for (int i = size(); i >= 0; i--) {
temp = first;
for (int j = 0; j < i; j++) {
temp = temp.next;
if(j == i-1){
System.out.printf("stack[%d]=%d\n", j, temp.number);
}
}
}
}
}
class Node {
int number;
Node next;
public Node(int number) {
this.number = number;
}
@Override
public String toString() {
return "Node{number=" + this.number + "}";
}
}