【Java】数据结构——栈(图文)
生活随笔
收集整理的這篇文章主要介紹了
【Java】数据结构——栈(图文)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 簡介
- 一、【數組實現】棧的順序存儲
- 棧的結構
- 1、入棧
- 2、出棧
- 3、判斷棧滿
- 4、判斷棧空
- 5、獲取棧的長度
- 6、遍歷棧
- 完整代碼
- 測試
簡介
棧是一種先進后出的線性結構。
一、【數組實現】棧的順序存儲
棧的結構
public class ArrayStack {/*** max size of the stack*/private int maxSize;/*** a stack object*/private Object[] stack;/*** init the top of the stack* from 0 to maxSize */private int top = -1;public ArrayStack(int maxSize) {this.maxSize = maxSize;stack = new Object[this.maxSize];}------functions------ }1、入棧
思路:
1、首先判斷棧是否為滿,若滿則返回,
2、否則先將top加1,然后給棧數組賦值。
看圖說話:
代碼:
/*** push a element** @param object*/public void push(Object object) {if (this.isFull()) {System.out.println("the stack is full ! can't push !");return;}stack[++top] = object;}2、出棧
思路:
1、首先判斷棧是否為空,若為空,提示并返回。
2.否則,先保存結點,后top減1。
看圖說話:
代碼:
/*** pop a element** @return*/public Object pop() {if (isFull()) {throw new RuntimeException("the stack is empty!");}return stack[top--];}3、判斷棧滿
代碼:
/*** Whether the stack is full** @return*/public boolean isFull() {return top > this.maxSize - 1;}4、判斷棧空
代碼:
/*** Whether the stack is empty** @return*/public boolean isEmpty() {return top == -1;}5、獲取棧的長度
思路:
因為top指針一直指向棧的頂部,所以top的值就是棧的長度,但是由于本案例是從0開始的,所以棧的長度需要加1,即:top+1。
看圖說話:
代碼:
/*** get length of stack* @return*/public int getLength(){return top+1;}6、遍歷棧
代碼:
/*** print a stack*/public void list() {if (isEmpty()) {System.out.println("the stack is empty!");}for (int i = top; i >= 0; i--) {System.out.print("stack[" + i + "]=");System.out.println(stack[i]);}}完整代碼
package com.qingfeng.stack.array;@SuppressWarnings("all") public class ArrayStack {/*** max size of the stack*/private int maxSize;/*** a stack object*/private Object[] stack;/*** init the top of the stack* from 0 to maxSize*/private int top = -1;public ArrayStack(int maxSize) {this.maxSize = maxSize;stack = new Object[this.maxSize];}/*** Whether the stack is empty** @return*/public boolean isEmpty() {return top == -1;}/*** Whether the stack is full** @return*/public boolean isFull() {return top > this.maxSize - 1;}/*** push a element** @param object*/public void push(Object object) {if (this.isFull()) {System.out.println("the stack is full ! can't push !");return;}stack[++top] = object;}/*** pop a element** @return*/public Object pop() {if (isFull()) {throw new RuntimeException("the stack is empty!");}return stack[top--];}/*** print a stack*/public void list() {if (isEmpty()) {System.out.println("the stack is empty!");}for (int i = top; i >= 0; i--) {System.out.print("stack[" + i + "]=");System.out.println(stack[i]);}}/*** get length of stack* @return*/public int getLength(){return top+1;} }測試
代碼:
public class ArrayStackTest {public static void main(String[] args) {ArrayStack stack = new ArrayStack(5);/*------------------------------------------------------------------*/System.out.println("-----testPush-----");stack.push(10);stack.push(20);stack.push(30);stack.push(80);stack.list();/*------------------------------------------------------------------*/System.out.println("-----testPop-----");stack.pop();stack.list();/*------------------------------------------------------------------*/System.out.println("-----testGetLength-----");System.out.println("the length of stack is: "+stack.getLength());} }運行結果:
-----testPush----- stack[3]=80 stack[2]=30 stack[1]=20 stack[0]=10 -----testPop----- stack[2]=30 stack[1]=20 stack[0]=10 -----testGetLength----- the length of stack is: 3Process finished with exit code 0總結
以上是生活随笔為你收集整理的【Java】数据结构——栈(图文)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 在 Mac 环境下的安装
- 下一篇: Java-内置注解