Doubt about the top of a stack in the Java language

Asked

Viewed 44 times

-1

I’m a beginner in programming and I was studying some data structures in the Java language. When I started studying about the "stack" structure, I had the following question: Let’s say I have a vector with five spaces (indexes ranging from 0-4), but only four spaces (indexes ranging from 0-3) are filled, so the "top of the stack" is the value "null"which is in the index "4" (which is empty), or is the value "3", which is not the last index of the stack that makes up this vector, but is the last space of the vector that has a value other than null?

Illustrating: vector 1 = [1, 2, 3, 4, null] - The top of the stack is either Indice "3" or index "4"?

  • Java offers the class Stack extending the Vector class by adding stack handling operations. There is also the Vector class Linkedlist representing a doubly chained list that offers editing methods at both ends of the list.

1 answer

0


Being you implementing the concept of stack in a vector, the top of the stack will be index 3 if you are always inserted from the lowest index to the highest.

Remember that the battery concept is mainly linked to the insertion and removal order.

LIFO (Last in First out - Last in First out). If you are going to delete an item from this list you should delete what is in index 3 and if you are going to insert it you should insert in index 4.

Usually this concept is implemented in a dynamic structure (without a limit of elements as is the case of arrays)

recommend reading the article below:

https://www.devmedia.com.br/pilhas-fundamentos-e-implementacao-da-estrutura-em-java/28241

Browser other questions tagged

You are not signed in. Login or sign up in order to post.