栈是一个先入后出(FILO)的有序列表。
栈限制线性表的插入和删除操作只能在一端进行。
允许插入和删除的一端,即变化的一端,称为栈顶;另一段为固定的一端,称为栈底。
要点:
需要一个指针top用来指示栈顶
当top指向0时,栈拥有第一个元素;故栈空时应该设置top = -1
入栈操作,top++;stack[top] = val;
出栈操作, int val = stack[top]; top–; return val;
但是这个栈不能动态扩展
代码:
package com.ravi.structure.arraystack; import java.util.Scanner; public class ArrayStackDemo { public static void main(String[] args) { ArrayStack arrayStack = new ArrayStack(5); String key = ""; Scanner scanner = new Scanner(System.in); boolean loop = true; while (loop) { System.out.println("show:显示栈"); System.out.println("exit:退出程序"); System.out.println("push:入栈"); System.out.println("pop:出栈"); key = scanner.next(); switch (key) { case "show": arrayStack.list(); break; case "exit": scanner.close(); loop = false; break; case "push": System.out.println("输入一个数"); int value = scanner.nextInt(); try { arrayStack.push(value); }catch (Exception e) { System.out.println(e.getMessage()); } break; case "pop": try { int val = arrayStack.pop(); System.out.println(val + "已经出栈."); } catch (Exception exception) { System.out.println(exception.getMessage()); } break; } } } } class ArrayStack { private int maxSize; // 栈的最大大小 private int[] stack;// 栈数组 private int top = -1;// 栈顶指针 public ArrayStack(int maxSize) { this.maxSize = maxSize; stack = new int[maxSize]; } // 判断栈满 public boolean isFull() { return top == maxSize - 1; } // 判断栈空 public boolean isEmpty() { return top == -1; } // 入栈 public void push(int value) { if (isFull()) { throw new RuntimeException("栈满!"); } top++; stack[top] = value; } // 出栈 public int pop() { if (isEmpty()) { throw new RuntimeException("栈空!"); } int value = stack[top]; top--; return value; } // 遍历栈顶=>栈底 public void list() { if (isEmpty()) { for (int i = 0; i < maxSize; i++) { System.out.print("| "); System.out.print(" "); System.out.print(" |"); System.out.println(); } System.out.println("******"); return; } for (int i = maxSize; i > top + 1 ; i--) { System.out.print("| "); System.out.print(" "); System.out.print(" |"); System.out.println(); } for (int i = top; i > -1 ; i--) { System.out.print("| "); System.out.print(stack[i]); System.out.print(" |"); System.out.println(); } System.out.println("******"); } }
测试:
输出结果: show:显示栈 exit:退出程序 push:入栈 pop:出栈 push 输入一个数 2 show:显示栈 exit:退出程序 push:入栈 pop:出栈 push 输入一个数 3 show:显示栈 exit:退出程序 push:入栈 pop:出栈 show | | | | | | | 3 | | 2 | ******
要点:
代码:
package com.ravi.structure.linkedlistStack; public class LinkedListStackDemo { public static void main(String[] args) { LinkedListStack stack = new LinkedListStack(); stack.add(new Node(6)); stack.add(new Node(8)); stack.add(new Node(2)); stack.list(); System.out.println("\n" + stack.pop() + "出栈!\n"); stack.list(); } } class LinkedListStack { private final Node head = new Node(-1); // 头结点 private static int size = 0; // 栈的实际大小 public void add(Node node) { node.next = head.next; head.next = node; size++; } public boolean isEmpty() { return head.next == null; } public int pop() { if (isEmpty()) { throw new RuntimeException("栈空!不可pop!"); } Node temp = head.next; head.next = temp.next; return temp.item; } public void list() { if (isEmpty()) { System.out.print("| "); System.out.print(" "); System.out.print(" |"); System.out.println(); System.out.println("******"); return; } Node p = head.next; while (p != null){ System.out.print("| "); System.out.print(p.item); System.out.print(" |"); System.out.println(); p = p.next; } System.out.println("******"); } } class Node { int item; Node next; public Node(int item) { this.item = item; this.next = null; } }
测试:
| 2 | | 8 | | 6 | ****** 2出栈! | 8 | | 6 | ******