Last Updated:

Writing a stack in java

stack in java
 
 

Theory. Stack in java

To begin with, let's get acquainted with a small theoretical base. When you use the stack, you have access only to the last item you added. By deleting this item, the user has access to the penultimate item, and so on. Thus, this data structure implements the principle of LIFO (Last In First Out). For convenience, you can draw an analogy with a stack of plates or a pistol magazine (the last loaded cartridge will be fed into the chamber first). Many microprocessors have a stack architecture. When a method is called, the return address and arguments are entered into the stack, and when the method is exited, they are removed from the stack.

Stack example

Here 5 is the element at the top of the stack (let's call the last element top) 

Thus, in order to get the element "3" from the stack, we first need to remove "5" and "4". This is where the small theoretical part ends. I will add that the TOP element is sometimes also called a "head".

Stack implementation in java

So, I propose to implement the following methods for our stack:

1) - a method that will ensure the addition of an element (in the top position)addElement 

2) - the method that will be provided by the removal of the element (from the top position)deleteElement

3) — a method that will return the value of the element that is in the top positionreadTop

4) – a method that will check the stack for emptinessisEmpty

5) – the method that will check is not full of our array in which we store the stackisFull

First, let's create a class in our project, declare the fields necessary for work, and then initialize them in the constructor.Stack

The constructor has an option that will allow the user to enter the maximum stack size with the keyboard. Also, it is necessary to understand the difference between the syntax of top++ and ++top. In the first case, a certain action is performed first, and then the counter is incremented (increased). In the second, the counter is incremented first, and then the action is performed.m

We implement these methods of interacting with the stack.

1) addElement

Increment the array index and add the passed element to the specified position.

2) deleteElement

To delete an existing top element, simply reduce the array index. Thus, our top of the stack will be the penultimate element, and the last element will be deleted using Garbage Collector (Java's built-in machine for deleting certain elements).

3) readTop

The method returns to the user the item that is at the top of the stack.

4) IsEmpty

Returns true (if the data array is empty, the index of the element , which is why we set the initial value for this variable in the constructor).top = 1

5) isFull

The method returns a value (in a situation where our data array is fully populated and there is no way to add another element). By the way, the question may arise, why mSize-1? If we set the maximum number of elements of the array to 10, then the maximum index will be 9, since the indices go from 0 to 9.true

In the method, we implement the introduction of four elements, the removal of one and the further display of the stack on the screen.main

Notice that the stack returns items in reverse order. That is, if we entered 79, 59, 35, 24 into the stack, then it will return us 24, 35, 59, 79. That is why this data structure is convenient to use to solve the problem of reversing letters in a word or forming a prefix record of algebraic expressions (Reverse Polish Notation).

Program listing:

That's it, the java stack is ready.