简体   繁体   English

理解栈(数据结构)、栈类、链表——java?

[英]Understanding stack(data structure), stack class, linked list - java?

I'm trying to understand about data structure and ADT.我正在尝试了解数据结构和 ADT。 So far I've read numerous resources, yet I don't have a clear understanding what the difference is.到目前为止,我已经阅读了大量资源,但我并没有清楚地了解其中的区别。

So my understanding is stack, linkedlist and queue are data structures, and there are classes named Stack, Linkedlist as ADT to use those data structures in Java.所以我的理解是堆栈,链表和队列是数据结构,Java中有名为Stack,Linkedlist作为ADT的类来使用这些数据结构。 But some books or resources say that stack is a collection, and the other resources say stack is data structure.但是有些书或资源说堆栈是一个集合,而其他资源则说堆栈是数据结构。 I'm very confused all the differences, and am lost at how stack can be implemented with linkedlist.我对所有差异感到非常困惑,并且对如何使用链表实现堆栈感到迷茫。

Could anybody explain to make it more clear to me?有人可以解释一下让我更清楚吗? I know it would be very basic question, but I'm really in need to understand!我知道这将是非常基本的问题,但我真的需要了解!

Thank you so much!非常感谢!

Welcome to the programming world.欢迎来到编程世界。 A new term everyday.每天一个新名词。 ;-) ;-)

When you study from a programming-language-agnostic view, the Stack is a logical structure that has operations such as push and pop.当您从与编程语言无关的角度学习时,堆栈是一种逻辑结构,具有推送和弹出等操作。 It's an abstraction, a mathematical concept, it's a DATA STRUCTURE.它是一个抽象,一个数学概念,它是一个数据结构。

Now, when we're talking about a specific programming language such as Java, Sun developers put several common-use DATA STRUCTURES implementations into a group which they call Collections.现在,当我们谈论特定的编程语言(例如 Java)时,Sun 开发人员会将几个常用的 DATA STRUCTURES 实现放入他们称为集合的组中。 In the Java world, Collections is a concept that also has Java class ( http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html ) where all these structures inherits from, called AbstractCollection, that also implements an interface called Collection ( http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html ).在 Java 世界中,集合是一个概念,它也有 Java 类( http://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html ),所有这些结构都继承自,称为 AbstractCollection ,它还实现了一个名为 Collection ( http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html ) 的接口。

So a Java implementation of the concept Stack is also called Stack, but as you can see from the documentation, the CLASS Stack inherits from an AbstractCollection (see http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html ), and so, it's a Java Collection.因此,概念堆栈的 Java 实现也称为堆栈,但正如您从文档中看到的,CLASS 堆栈继承自 AbstractCollection(请参阅http://docs.oracle.com/javase/7/docs/api/java /util/Stack.html ),因此,它是一个 Java 集合。

Maybe you will find the explanations here useful.也许你会发现这里的解释很有用。 They come with a nice animations (as java applets).它们带有漂亮的动画(作为 java 小程序)。

A stack is a special type of collection.堆栈是一种特殊类型的集合。 It is a data structure.它是一种数据结构。

A stack is a linear data structure where you have one item after another.堆栈是一种线性数据结构,其中您拥有一个又一个项目。

A stack can only do 3 things:一个堆栈只能做三件事:

  1. Push - tack on the data to the end of the collection.推送 - 将数据粘贴到收集的末尾。
  2. Peek - look at only the very last item of the collection.窥视 - 仅查看该系列的最后一个项目。
  3. Pop - remove the last item of the collection.弹出 - 删除集合的最后一个项目。

You can't insert in the middle or at the beginning.您不能在中间或开头插入。 You can't look at any item (the first and any middle) besides the last item.除了最后一项之外,您无法查看任何项目(第一项和任何中间项)。

A stack is also referred to as "Last In First Out"堆栈也称为“后进先出”

Why such a "limitation"?为什么会有这样的“限制”? There are a few scenarios in real life where this happens.在现实生活中有一些场景会发生这种情况。 At a buffet, employees put plates one on top of each other, and the top-most plates (last in) are the ones taken out by customers (first out).在自助餐中,员工将一个盘子叠在一起,最上面的盘子(后进)是顾客拿出的盘子(先出)。

I can't get in too much detail but stacks are useful (even crucial) when you're calculating with order of operations (you'll probably be given that programming assignment, so you can't rely on your calculator), and when you want to find the shortest route in a network (for example, finding the shortest Facebook friend path between you and a celebrity).我无法提供太多细节,但是当您按操作顺序进行计算时,堆栈很有用(甚至至关重要)(您可能会获得该编程任务,因此您不能依赖计算器),以及何时你想在网络中找到最短的路线(例如,找到你和名人之间最短的 Facebook 好友路径)。

Data structure is a way of organizing the data so that it can be used efficiently.数据结构是一种组织数据的方式,以便可以有效地使用它。 This mainly includes what type of data can be stored and how the data can be stored and processed.这主要包括可以存储什么类型的数据以及如何存储和处理数据。

Example 1: We can store list of elements of same data type in an array data structure, where elements can be accessed using the index of the array.示例 1:我们可以将相同数据类型的元素列表存储在数组数据结构中,其中可以使用数组的索引访问元素。

Example 2: We can store list of elements of the same data type in a stack data structure, where elements can be access in Last-In-First-Out manner.示例 2:我们可以将相同数据类型的元素列表存储在堆栈数据结构中,其中元素可以采用后进先出的方式访问。

Abstract data types are the data types where you specify the behavior that you want and do not specify the implementation.抽象数据类型是您指定所需行为但不指定实现的数据类型。 For eg Interfaces in java, define the operations to be performed and the expected behavior but the implementation is hidden.例如,Java 中的接口,定义要执行的操作和预期的行为,但实现是隐藏的。

There are already defined classes in Java which provide you the behavior of the data structure. Java 中已经定义了类,它们为您提供数据结构的行为。 One of the class is java.util.Stack class which provides you the operations that can be performed on the Stack but hides the implementation details of those operations.其中一个类是java.util.Stack类,它为您提供可以在 Stack 上执行的操作,但隐藏了这些操作的实现细节。 You can check more about java.util.Stack class on my blog https://hetalrachh.home.blog/2019/12/25/stack-data-structure/ .您可以在我的博客https://hetalrachh.home.blog/2019/12/25/stack-data-structure/上查看有关java.util.Stack类的更多信息。

Stack can be implemented in a number of ways, which means implementing Last-In-First-Out behavior using different data structures like array and linked list.堆栈可以通过多种方式实现,这意味着使用不同的数据结构(如数组和链表)实现后进先出行为。 You can find a detailed explanation with a diagram on my blog http://hetalrachh.home.blog/2020/01/12/stack-implementation-using-array-and-linkedlist-in-java/ .您可以在我的博客http://hetalrachh.home.blog/2020/01/12/stack-implementation-using-array-and-linkedlist-in-java/上找到带有图表的详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM