資料結構-線性結構-棧
阿新 • • 發佈:2021-07-07
一、棧的介紹
二、棧的引入
陣列模擬棧程式碼實現(Java):
import java.util.Scanner; /** * 1.使用陣列來模擬棧 * 2.定義一個top來表示棧,初始化為-1 * 3.入棧的操作,當有資料加入到棧時,top++;stack[top]=data; * 4.出棧的操作,int value=stack[top];top--,return value */ public class ArrayStackDemo { public static void main(String[] args) { //測試ArrayStack是否正確ArrayStack stack = new ArrayStack(4); 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("請輸入你的選擇:"); key = scanner.next(); switch (key) { case "show": stack.list(); break; case "push": System.out.println("請輸入一個數:"); int value = scanner.nextInt(); stack.push(value); break; case "pop": try { int pop = stack.pop(); System.out.printf("出棧的資料是 %d\n", pop); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "exit": scanner.close(); loop = false; break; default: break; } } System.out.println("程式退出~~~"); } } //定義一個ArrayStack 表示棧 class ArrayStack { private int maxSize; //棧的大小 private int[] stack; //陣列,陣列模擬棧,資料就放在該陣列 private int top = -1;//top表示棧頂,初始化為-1 //構造器 public ArrayStack(final int maxSize) { this.maxSize = maxSize; stack = new int[maxSize]; } //棧滿 public Boolean isFull(){ return top == maxSize-1; } //棧空 public Boolean isEmpty(){ return top == -1; } //入棧-push public void push(int value){ if(isFull()){ System.out.println("棧滿"); return; } top++; stack[top] = value; } //出棧-pop,將棧頂的陣列返回 public int pop(){ //先判斷棧是否為空 if(isEmpty()){ throw new RuntimeException("棧空,沒有資料~"); } int value = stack[top]; top--; return value; } //顯示棧的情況[遍歷棧],遍歷時,需要從棧頂開始顯示資料 public void list(){ if (isEmpty()) { System.out.println("棧空,沒有資料~"); return; } for(int i=top; i>=0; i--){ System.out.printf("stack[%d]=%d\n", i, stack[i]); } } }
單向連結串列模擬棧程式碼實現(Java):
public class LinkStackDemo { public static void main(String[] args) { LinkStack stack = new LinkStack(); stack.push(3); stack.push(1); stack.push(2); stack.push(4); System.out.println(stack); System.out.println("訪問棧頂元素:" + stack.peek()); System.out.println("第一次彈出棧頂元素:" + stack.pop()); System.out.println("第二次彈出棧頂元素:" + stack.pop()); System.out.println("兩次pop之後的棧:" + stack); } } class LinkStack { // 定義一個內部類Node,Node例項代表鏈棧的節點 private class Node { // 儲存節點的資料 private int data; // 指向下個節點的引用 private Node next; // 無參構造器 public Node() { } // 初始化全部屬性的構造器 public Node(int data, Node next) { this.data = data; this.next = next; } } // 儲存該鏈棧的棧頂元素 private Node top; // 儲存該鏈棧中已包含的節點數 private int size; // 建立空鏈棧 public LinkStack() { // 空鏈棧,top的值為null top = null; } // 以指定資料元素來建立鏈棧,該鏈棧只有一個元素 public LinkStack(int element) { top = new Node(element, null); size++; } // 返回鏈棧的長度 public int length() { return size; } // 進棧 public void push(int element) { // 讓top指向新建立的元素,新元素的next引用指向原來的棧頂元素 top = new Node(element, top); size++; } // 出棧 public int pop() { Node oldTop = top; // 讓top引用指向原棧頂元素的下一個元素 top = top.next; // 釋放原棧頂元素的next引用 oldTop.next = null; size--; return oldTop.data; } // 訪問棧頂元素,但不刪除棧頂元素 public int peek(){ return top.data; } // 判斷鏈棧是否為空棧 public boolean empty() { return size == 0; } // 清空鏈棧 public void clear() { top = null; size = 0; } public String toString() { // 鏈棧為空棧時 if (empty()) { return "[]"; } else { StringBuilder sb = new StringBuilder("["); for (Node current = top; current != null; current = current.next) { sb.append(current.data + ", "); } int len = sb.length(); return sb.delete(len - 2, len).append("]").toString(); } } }