日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【Java】数据结构——栈(图文)

發布時間:2024/9/27 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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】数据结构——栈(图文)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。