对于栈和链表,数组之间关系的一些探索

news/2024/8/26 11:48:50 标签: 数据结构

先贴脸来个图
这是一个解析图,总体是个栈(stacks)细分有数组和链表【注意这儿的linkedlist可不是Java集合List中的linklist】
在这里插入图片描述
对于栈,如果我们想向栈中添加元素,或者想从中删除元素,都必须从一个地方开始:栈的顶部(Top)。这种从同一位置插入和删除的行为也叫后进后先(Last In, First Out,LIFO
图示
在这里插入图片描述
LIFO图例备份

栈的实现方式

一句话,栈用链表实现就是上面提到的linkedList。
链表有一个显著的特征,任何操作都在一个头节点上,这样一来复杂的就是O(1),这样得益于LIFO。
到这如果感觉啰嗦,可以跳到最后的实现,有完整简易Java代码的实现,现在gpt这么好用,其他语言的也可以拿到Java代码转置一下,变成自己喜欢的高级语言debugger
当然, 栈也开业用数组实现,但是数组数组是静态数据结构,比如我们在Java中生命一个数组甚至要声明出其可存储的数据类型

//int [一个数字预设存储空间] = ****
int[8] = ***;

虽然使用链表不用这样设置和声明,这样会让链表以后他可以一直装,这种时候就会遇到栈溢出 的问题,但是只要不是你的这个类型的数据彻底占用了你的电脑内存就不会发生,而且人家链表里面是多个数组根本不担心你过来的是什么,来了就往口袋里装(回到图一⬆️)。

栈操作

以增删为例
在这里插入图片描述
我们只能从栈的一端(顶部)添加和删除元素,无论使用哪种语言实现栈,几乎都会实现以下几个基本的操作:

push:用于将元素添加到栈顶
pop:用于从栈顶删除元素
top ( peek ):返回栈顶部的元素,但不会将其删除
isEmpty:检查栈是否为空
size:返回栈中的元素数量

栈的实现

伪代码示例及代码实现
一个函数(function_one),它定义了一些局部变量,然后将这些变量作为参数调用了函数function_two,function_two中做了类似的事情:定义了一些局部变量,然后将这些变量作为参数传递给另一个函数(function_three)。
在这里插入图片描述

使用Java

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

//用链表实现栈
class LinkedStack {
    //始终指向栈顶节点(链表头结点)
    private Node top;
    private int size;

    public LinkedStack() {
        this.top = null;
        this.size = 0;
    }

    // 压入栈顶
    public void push(int data) {
        Node newNode = new Node(data);
        newNode.next = top;
        top = newNode;
        size++;
    }

    // 弹出栈顶
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty. Cannot pop.");
        }
        int data = top.data;
        top = top.next;
        size--;
        return data;
    }

    // 获取栈顶元素
    public int top() {
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty. Cannot retrieve top element.");
        }
        return top.data;
    }

    // 检查栈是否为空
    public boolean isEmpty() {
        return top == null;
    }

    // 获取栈的大小
    public int size() {
        return size;
    }

    // 打印栈
    public void printStack() {
        Node current = top;
        while (current != null) {
            System.out.print(current.data + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }
}

public class StackExample {
    public static void main(String[] args) {
        LinkedStack stack = new LinkedStack();

        // 压入栈顶
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.printStack(); // 输出: 3 -> 2 -> 1 -> null

        // 获取栈顶元素
        System.out.println("Top element is: " + stack.top()); // 输出: 3

        // 弹出栈顶
        System.out.println("Popped element is: " + stack.pop()); // 输出: 3
        stack.printStack(); // 输出: 2 -> 1 -> null

        // 获取栈大小
        System.out.println("Stack size is: " + stack.size()); // 输出: 2

        // 检查栈是否为空
        System.out.println("Is stack empty? " + stack.isEmpty()); // 输出: false

        // 弹出所有元素
        stack.pop();
        stack.pop();
        stack.printStack(); // 输出: null

        // 检查栈是否为空
        System.out.println("Is stack empty? " + stack.isEmpty()); // 输出: true
    }
}


http://www.niftyadmin.cn/n/5558735.html

相关文章

vue自制表格

一、有时候element-ui的表格不满足需求&#xff0c;需要自定义表格&#xff0c;例如图下 二、上代码 <table class"tablenew table1" cellpadding"0" cellspacing"0"><tr><td>身份证号码</td><td>111111111</…

LabVIEW人工模拟肺控制系统开发

开发了一种创新的主被动一体式人工模拟肺模型&#xff0c;通过LabVIEW开发的上位机软件&#xff0c;实现了步进电机驱动系统的精确控制和多种呼吸模式的模拟。该系统不仅能够在主动呼吸模式下精确模拟快速呼吸、平静呼吸和深度呼吸&#xff0c;还能在被动模式下通过PID控制实现…

uniapp使用 web-view 与网页互相通信

在APP中使用 this.$scope.$getAppWebview() 获取webview对象实例 通过evalJS可以为这个webview注入一段js&#xff0c;从而去调用网页中的对象 <template> <view> <web-view ref"webview" src"http://192.168.1.79:6446/demo.html" mess…

第59期|GPTSecurity周报

GPTSecurity是一个涵盖了前沿学术研究和实践经验分享的社区&#xff0c;集成了生成预训练Transformer&#xff08;GPT&#xff09;、人工智能生成内容&#xff08;AIGC&#xff09;以及大语言模型&#xff08;LLM&#xff09;等安全领域应用的知识。在这里&#xff0c;您可以找…

Spring Boot app: Failed to determine a suitable driver class

目录 问题描述解决方案 问题描述 我尝试连接springboot&#xff1b;当我运行时&#xff0c;出现错误“Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured."” application.yml 应用程序.yml dat…

Spring Boot集成Activity7实现简单的审批流

由于客户对于系统里的一些新增数据&#xff0c;例如照片墙、照片等&#xff0c;想实现上级逐级审批通过才可见的效果&#xff0c;于是引入了Acitivity7工作流技术来实现&#xff0c;本文是对实现过程的介绍讲解&#xff0c;由于我是中途交接前同事的这块需求&#xff0c;所以具…

Django REST Framework(九)GenericAPIView视图子类

GenericAPIView 是 Django REST Framework (DRF) 中一个非常重要的类&#xff0c;它提供了常用的通用视图功能。通过继承 GenericAPIView&#xff0c;可以轻松地构建 RESTful API。 用法 导入所需模块&#xff1a; from rest_framework import generics from .models import …

深度学习 | CNN 基本原理

目录 1 什么是 CNN2 输入层3 卷积层3.1 卷积操作3.2 Padding 零填充3.3 处理彩色图像 4 池化层4.1 池化操作4.2 池化的平移不变性 5 全连接层6 输出层 前言 这篇博客不够详细&#xff0c;因为没有介绍卷积操作的具体计算&#xff1b;但是它介绍了 CNN 各层次的功能…