简体   繁体   中英

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

I'm trying to understand about data structure and 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. 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. 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 ).

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.

Maybe you will find the explanations here useful. They come with a nice animations (as java applets).

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).

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.

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.

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.

There are already defined classes in Java which provide you the behavior of the data structure. 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. You can check more about java.util.Stack class on my blog https://hetalrachh.home.blog/2019/12/25/stack-data-structure/ .

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/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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