gpt4 book ai didi

java数据结构基础:栈

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 39 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java数据结构基础:栈由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

准备工作

工具:idea+jdk8 。

技术要求:java基础语法 。

编码环节

首先,我们得先确定下来,用什么数据来模拟栈的操作。由于是一个一个的元素放入栈里面,我们可以考虑用数组来实现.

java数据结构基础:栈

以上是Java官方文档中的栈定义,我们也只需要实现三个方法:判断是否为空、移除栈顶对象、添加元素到栈的尾部 。

所以我们事先得定义一个数组:

?
1
Objects[] arr;

数组定义好了之后呢,想想,我们怎么去获取到栈尾部或者栈首的元素呢?还记得数组的索引吗?可以用索引来假设为栈的指针。所以,我们还得定义好栈的元素个数和栈的默认长度以及默认的指针:

?
1
2
3
private int stackLength = 4 ; // 数组的默认长度
private int size; // 记住栈容器的元素个数
private int index = - 1 ; // 操作数组下标位置的指针

为什么这儿指向的是-1呢?我们知道,数组的第一个元素是索引为0,那么-1的意思就是不指向任何元素。待会儿我们在用的时候再去指向他.

然后,我们还得定义出数组的初始化。以及初始化的长度。参考官方文档的写法,当栈的长度满了之后我们就对栈长度进行1.5倍的扩容。我们就单独提取出一个方法来放置; 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
  * 数组初始化或者以1.5倍容量对数组扩容
  */
private void capacity() {
     // 数组初始化
     if ( this .arr == null ) {
         this .arr = new Object[ this .stackLength];
     }
     // 以1.5倍对数组扩容
     if ( this .size - ( this .stackLength - 1 ) >= 0 ) { // 如果当前数组的元素个数大于了当前数组的最后一个索引值
         this .stackLength = this .stackLength + ( this .stackLength >> 1 ); // 位运算,让长度变成原来的1/2
         this .arr = Arrays.copyOf( this .arr, this .stackLength); // 复制一个新的数组,用新开辟的长度
     }
}

push方法

如何给栈添加元素?我们要考虑的地方:指针向右移动一位,也就是说指针要+1。其次,添加完元素之后,栈元素的长度发生了变化,size+1 .

?
1
2
3
4
5
6
7
8
9
public E push(E item){
     // 先初始化数组
     this .capacity();
     // 添加元素
     this .arr[++index] = item;
     // 记录元素个数加一
     this .size++;
     return item;
}

pop方法

pop方法主要是用来移除栈顶的元素.

先分析一下思路:我们要用index去指向栈顶的元素,该怎么去指定?

删除之后,对应的size长度该怎么去改变?

我们知道,当元素添加了之后,index会跟着改变,那么就好比我们添加了三个元素,此时的index应该就是指向的2。那就好办了.

当移除的时候,我们只需要让index–来操作就能解决问题;看代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
  * 获取栈顶元素
  *
  * @return
  */
public E pop() {
     // 如果栈容器中没有元素则抛出异常
     if ( this .index == - 1 ) {
         throw new EmptyStackException();
     }
     // 记录元素个数
     this .size--;
     // 返回栈顶元素
     System.out.println( "删除元素之前的当前下标:" +index);
     return (E) this .arr[index--];
}

empty方法

判断栈是否为空,这很简单。直接判断当前的size是不是0就能解决:

?
1
2
3
public boolean empty(){
     return this .index== 0 ? true : false ;
}

全部代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.zxy;
import java.util.Arrays;
import java.util.EmptyStackException;
/**
  * @Author Zxy
  * @Date 2021/2/2 20:24
  * @Version 1.0
  * 演示栈容器的使用
  */
public class MyStack<E> {
     private Object[] arr; // 存放元素的物理结构
     private int stackLength = 4 ; // 数组的默认长度
     private int size; // 记住栈容器的元素个数
     private int index = - 1 ; // 操作数组下标位置的指针
     /**
      * 判断栈容器是否为空
      */
     public boolean empty() {
         return this .size == 0 ? true : false ;
     }
     /**
      * 获取栈顶元素
      *
      * @return
      */
     public E pop() {
         // 如果栈容器中没有元素则抛出异常
         if ( this .index == - 1 ) {
             throw new EmptyStackException();
         }
         // 记录元素个数
         this .size--;
         // 返回栈顶元素
         System.out.println( "删除元素之前的当前下标:" +index);
         return (E) this .arr[index--];
     }
 
     /**
      * 向栈顶添加元素
      *
      * @param item
      * @return
      */
     public E push(E item) {
         // 初始化数组
         this .capacity();
         // 向数组中添加元素
         System.out.println( "添加元素之前的下标:" +index);
         this .arr[++index] = item;
         System.out.println( "添加元素之后的下标:" +index);
         // 记录元素个数
         this .size++;
         return item;
     }
     /**
      * 数组初始化或者以1.5倍容量对数组扩容
      */
     private void capacity() {
         // 数组初始化
         if ( this .arr == null ) {
             this .arr = new Object[ this .stackLength];
         }
         // 以1.5倍对数组扩容
         if ( this .size - ( this .stackLength - 1 ) >= 0 ) { // 如果当前数组的元素个数大于了当前数组的最后一个索引值
             this .stackLength = this .stackLength + ( this .stackLength >> 1 ); // 位运算,让长度变成原来的1/2
             this .arr = Arrays.copyOf( this .arr, this .stackLength); // 复制一个新的数组,用新开辟的长度
         }
     }
     public static void main(String[] args) {
         MyStack<String> stack = new MyStack<>();
         stack.push( "a" );
         stack.push( "b" );
         stack.push( "c" );
         System.out.println(stack.size);
         System.out.println( "当前栈顶元素:" +stack.pop());
         /*System.out.println(stack.pop());
         System.out.println(stack.pop());*/
     }
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望能够您能够关注我的更多内容! 。

原文链接:https://blog.csdn.net/weixin_43581288/article/details/113588790 。

最后此篇关于java数据结构基础:栈的文章就讲到这里了,如果你想了解更多关于java数据结构基础:栈的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com