• 数据结构与算法【栈】的Java实现


    概念

    是一种线性的数据结构,只能在其一端添加数据和移除数据。习惯来说,这一端称之为栈顶,另一端不能操作数据的称之为栈底。特点是先进后出。

    简单接口

    1. public interface Stack {
    2. /**
    3. * 压入元素
    4. */
    5. boolean push(E value);
    6. /**
    7. * 弹出元素
    8. */
    9. E pop();
    10. /**
    11. * 返回栈顶元素但不移除
    12. */
    13. E peek();
    14. /**
    15. * 判断是否为空
    16. */
    17. boolean isEmpty();
    18. /**
    19. * 判断是否栈满
    20. */
    21. boolean isFull();
    22. }

    基于链表的实现

    1. public class LinkedListStack implements Stack, Iterable {
    2. private Node sentinel = new Node<>(null, null);
    3. private int size = 0;
    4. private int capacity = 8;
    5. public LinkedListStack(int capacity) {
    6. this.capacity = capacity;
    7. }
    8. private static class Node {
    9. Node next;
    10. E value;
    11. public Node(Node next, E value) {
    12. this.next = next;
    13. this.value = value;
    14. }
    15. }
    16. @Override
    17. public boolean push(E value) {
    18. if (isFull()) {
    19. return false;
    20. }
    21. sentinel.next = new Node<>(sentinel.next, value);
    22. size++;
    23. return true;
    24. }
    25. @Override
    26. public E pop() {
    27. if (isEmpty()) {
    28. return null;
    29. }
    30. Node node = sentinel.next;
    31. sentinel.next = node.next;
    32. size--;
    33. return node.value;
    34. }
    35. @Override
    36. public E peek() {
    37. if (isEmpty()) {
    38. return null;
    39. }
    40. return sentinel.next.value;
    41. }
    42. @Override
    43. public boolean isEmpty() {
    44. return size == 0;
    45. }
    46. @Override
    47. public boolean isFull() {
    48. return size == capacity;
    49. }
    50. @Override
    51. public Iterator iterator() {
    52. return new Iterator() {
    53. Node p = sentinel.next;
    54. @Override
    55. public boolean hasNext() {
    56. return p != null;
    57. }
    58. @Override
    59. public E next() {
    60. E value = p.value;
    61. p = p.next;
    62. return value;
    63. }
    64. };
    65. }
    66. }

    基于数组的实现

    1. public class ArrayStack implements Stack, Iterable {
    2. private int capacity = 8;
    3. private E[] array;
    4. private int top = 0;
    5. @SuppressWarnings("all")
    6. public ArrayStack(int capacity) {
    7. this.capacity = capacity;
    8. array = (E[]) new Object[capacity];
    9. }
    10. @Override
    11. public boolean push(E value) {
    12. if (isFull()) {
    13. return false;
    14. }
    15. array[top++] = value;
    16. return true;
    17. }
    18. @Override
    19. public E pop() {
    20. if (isEmpty()) {
    21. return null;
    22. }
    23. return array[--top];
    24. }
    25. @Override
    26. public E peek() {
    27. if (isEmpty()) {
    28. return null;
    29. }
    30. return array[top-1];
    31. }
    32. @Override
    33. public boolean isEmpty() {
    34. return top == 0;
    35. }
    36. @Override
    37. public boolean isFull() {
    38. return top == capacity;
    39. }
    40. @Override
    41. public Iterator iterator() {
    42. return new Iterator() {
    43. int current = top;
    44. @Override
    45. public boolean hasNext() {
    46. return current!=0;
    47. }
    48. @Override
    49. public E next() {
    50. return array[--current];
    51. }
    52. };
    53. }
    54. }
  • 相关阅读:
    【vue3】需要了解哪些【全】
    356页14万字高端商业办公综合楼弱电智能化系统2022版
    构建病毒宿主关系知识图谱
    通信算法之九十六:电力通信系统-HRF多载波通信系统-物理层收发信道开发
    【Linux】进程控制(进程创建、进程终止、进程等待、进程替换)
    Programming Differential Privacy第十二章EXERCISES IN ALGORITHM DESIGN算法设计练习
    javaweb JSP技术(七)
    低时延、高可靠、华为云CDN赋能中小企业
    ES6语法总结--上篇(基础篇)
    Centos7 部署 RocketMQ 高可用集群
  • 原文地址:https://blog.csdn.net/zmbwcx/article/details/134471336