先進(jìn)后出 FILO(First-Input-Last-Out)的有序列表,限制線性表中元素的插入和刪除只能在線性表的同一端進(jìn)行
package com.b0.stack;
import java.util.Scanner;
public class ArrayStackDemo {public static void main(String[] args) {ArrayStack stack = new ArrayStack(5);
String key = "";
boolean loop = true;//控制是否退出菜單
Scanner scanner = new Scanner(System.in);
while (loop){System.out.println("show:表示顯示棧");
System.out.println("exit:退出程序");
System.out.println("push:入棧");
System.out.println("pop:出棧");
System.out.println("請(qǐng)輸入你的選擇");
key = scanner.next();
switch (key){case "show":
stack.list();
break;
case "push":
System.out.println("請(qǐng)輸入一個(gè)數(shù)");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {int res = stack.pop();
System.out.println("出棧數(shù)據(jù)是:"+res);
}catch (Exception e){System.out.println(e.getMessage());
}
break;
case "exit":
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~");
}
}
class ArrayStack{private int maxSize;//棧的大小
private int[] stack;//數(shù)組模擬棧
private int top = -1;//棧頂,初始化為-1
//構(gòu)造器
public ArrayStack(int maxSize) {this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//判斷棧滿
public boolean isFull(){return top == maxSize-1;
}
//??? public boolean isEmpty(){return top == -1;
}
//入棧
public void push(int num){//判斷是否棧滿
if (isFull()){System.out.println("棧滿");
return;
}
top++;
stack[top] = num;
}
//出棧
public int pop(){//判斷是否??? if (isEmpty()){//拋出異常
throw new RuntimeException("棧空,沒有數(shù)據(jù)!");
}
int value = stack[top];
top--;
return value;
}
//遍歷棧
public void list(){if (isEmpty()){System.out.println("???!");
return;
}
while (top != -1){//從棧頂開始遍歷
System.out.println(stack[top]);
top--;
}
}
}
2.2 鏈表模擬棧(頭插法)package com.b0.stack;
public class LinkedStackDemo {public static void main(String[] args) {LinkedStack linkedStack = new LinkedStack();
linkedStack.push(1);
linkedStack.push(2);
linkedStack.push(3);
linkedStack.push(4);
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
}
}
class LinkedStack{//頭節(jié)點(diǎn)
Node head = new Node(0);
//???尾插法
public boolean isEmpty(){return head.next == null;
}
//入棧,頭插法
public void push(int num){Node node = new Node(num);
if (isEmpty()){head.next = node;
}
node.next = head.next;
head.next = node;
}
public int pop(){if (isEmpty()){//拋出異常
throw new RuntimeException("???,沒有數(shù)據(jù)!");
}
//定義零時(shí)變量,不修改head指針指向
Node cur = head;
int value = cur.next.no;
cur.next = cur.next.next;
return value;
}
}
class Node{public int no;
public Node next;
public Node(int no) {this.no = no;
}
}
2.3 鏈表模擬棧(尾插法,不推薦)package com.b0.stack;
public class LinkedStackDemo {public static void main(String[] args) {LinkedStack linkedStack = new LinkedStack();
linkedStack.push(1);
linkedStack.push(2);
linkedStack.push(3);
linkedStack.push(4);
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
System.out.println("出棧值為:"+linkedStack.pop());
}
}
class LinkedStack{//頭節(jié)點(diǎn)
Node head = new Node(0);
//??? public boolean isEmpty(){return head.next == null;
}
//入棧
public void push(int num){Node node = new Node(num);
Node cur = head;
while (true){if (cur.next == null)break;
cur = cur.next;
}
cur.next = node;
}
//出棧
public int pop(){if (isEmpty()){//拋出異常
throw new RuntimeException("???,沒有數(shù)據(jù)!");
}
//定義零時(shí)變量,不修改head指針指向
Node cur = head;
int value;
while (true){if (cur.next.next == null){value = cur.next.no;
cur.next = null;
break;
}
cur = cur.next;
}
return value;
}
}
class Node{public int no;
public Node next;
public Node(int no) {this.no = no;
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧