Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Stack | Additional Data Structures
Java Data Structures

StackStack

A Stack is another data structure that operates on the Last In, First Out (LIFO) principle. The same principle can be applied to a Deque, which you learned about earlier, and Java developers recommend using Deque if there is a need for a data structure that operates on the LIFO principle. The Stack data structure is deprecated.

Deprecated

Deprecated is a status assigned to a method, class, interface, or other programming element to indicate that this element is considered outdated and may be removed in future versions. It serves as a warning to developers that the use of this element is not recommended, and they should consider using more modern alternatives.

When an element is marked as deprecated, it means that the authors of the library or programming language advise avoiding its use in new code and recommend adopting new methods, classes, or approaches that may provide safer, more efficient, or more functional solutions.

An example is the Vector class in Java. Its methods have been deprecated in favor of more modern collections like ArrayList and LinkedList. If a programmer still uses Vector methods, the compiler may issue a warning that these methods are deprecated.

Example in Java:

java

main.java

Therefore, it is not recommended to use a data structure like Stack, but we will discuss it in this chapter because it is an interesting data structure used, for example, in the Java Stack memory.

Stack

A Stack is a data structure representing an ordered collection of elements where the addition and removal of elements occur at one end, called the stack's top. This operating principle is known as "Last In, First Out" (LIFO) - the last added element is the first to be removed.

Straight to the point, let's look at the methods of the Stack class:

Methods

  • push(E element) (addition): Adds an element to the top of the stack:
java

main.java

Adding is done the same way as in ArrayList, so let's immediately look at this method in combination with the pop() method:

  • pop(): Removes and returns the element from the top of the stack:
java

main.java

This method removed one element from the top of the stack. Note that the pop() method removes the last added element from the stack. That's exactly how the LIFO principle works.

We can also see which element is at the top of the stack:

  • Peek: Returns the element from the top of the stack without removing it:
java

main.java

With this method, we look at the top element in the stack.

Usage

Let's consider an example of using the stack data structure for navigating between pages in a browser (those forward and backward arrows that we frequently use).

Let's plan the implementation of the browser history and implement methods for two buttons (goBack() and goForward()). If you're not sure which buttons I'm referring to, I'm talking about these buttons:

content

Let's implement a class that has methods for operating these two buttons using the Stack data structure.

How it will work:

  • We will have two stacks and a String variable. The first stack will store the links we navigate by clicking the "back" arrow. The second stack will store the links we navigate by clicking the "forward" arrow. We will also have a String variable that stores the current page's link;
  • In this class, there will be four methods: visitPage(), goBack(), goForward(), and getCurrentPage(). Let's go through them step by step:
    • The visitPage() method will redirect us to the URL specified in the parameter. When moving to a new page, the old link will be added to the backStack. The forwardStack will be cleared as we move to a new page.

Let's look at the implementation in the code:

java

BrowserHistory.java

  • This way, we can return to the previous page when navigating to a new page. Let's implement the method for going back. It will work like this: we add the current link to the forwardStack, then remove this link from the backStack, and assign it to currentUrl. Let's look at the implementation in the code:
java

BrowserHistory.java

  • I'll remind you that the pop() method removes the element from the top of the stack and returns it. Therefore, using this method, we immediately assign the value of the URL to the variable currentUrl. We also check to ensure that the backStack is not empty; otherwise, we won't be able to go back to the previous link (simply because it won't be there). If the stack is empty, we output a corresponding message;
  • In the same manner, we implement the method for navigating to the forward page. We simply swap the elements in the stack:
java

BrowserHistory.java

  • Now, all that's left is to implement the getCurrentPage() method, which will simply return the value of currentUrl.

Testing

Next, let's test all of this in the main method. We'll use the visitPage() method three times to ensure these links are saved in the history. Then, we'll use the goBack() method twice, followed by the goForward() method once, to verify the functionality of the written methods.

During this process, we'll track our state using the getCurrentPage() method. You can run the code below and also try inserting more links and using different methods to test the functionality of this class:

java

Main.java

Note

If the large number of console outputs is bothersome, you can remove the console print operations from the methods, leaving only intermediate screen outputs.

Remember that the Stack class is deprecated, and it's better to use Deque instead. In this example, Stack is implemented based on the LIFO principle, and we can also implement Deque since it is a double-ended queue that can operate based on both FIFO and LIFO principles.

If you encounter difficulties understanding the example or the code in general, feel free to click on "Unclear?" and ask your question. I'll do my best to help you understand the material as soon as possible!

Also, don't forget that you can take advantage of the "Chat With Community" feature, where users like you are ready to help you!

1. What is the primary principle of a `Stack` data structure?
2. Which Java interface does `Stack` class implement?
3. What is the method used to add an element to the top of the stack in Java?
4. Which of the following Java collections is considered a more modern alternative to `Stack`?
5. In Java, what will the `pop()` method of a `Stack` return?

What is the primary principle of a Stack data structure?

Selecione a resposta correta

Which Java interface does Stack class implement?

Selecione a resposta correta

What is the method used to add an element to the top of the stack in Java?

Selecione a resposta correta

Which of the following Java collections is considered a more modern alternative to Stack?

Selecione a resposta correta

In Java, what will the pop() method of a Stack return?

Selecione a resposta correta

Tudo estava claro?

Seção 2. Capítulo 4
course content

Conteúdo do Curso

Java Data Structures

StackStack

A Stack is another data structure that operates on the Last In, First Out (LIFO) principle. The same principle can be applied to a Deque, which you learned about earlier, and Java developers recommend using Deque if there is a need for a data structure that operates on the LIFO principle. The Stack data structure is deprecated.

Deprecated

Deprecated is a status assigned to a method, class, interface, or other programming element to indicate that this element is considered outdated and may be removed in future versions. It serves as a warning to developers that the use of this element is not recommended, and they should consider using more modern alternatives.

When an element is marked as deprecated, it means that the authors of the library or programming language advise avoiding its use in new code and recommend adopting new methods, classes, or approaches that may provide safer, more efficient, or more functional solutions.

An example is the Vector class in Java. Its methods have been deprecated in favor of more modern collections like ArrayList and LinkedList. If a programmer still uses Vector methods, the compiler may issue a warning that these methods are deprecated.

Example in Java:

java

main.java

Therefore, it is not recommended to use a data structure like Stack, but we will discuss it in this chapter because it is an interesting data structure used, for example, in the Java Stack memory.

Stack

A Stack is a data structure representing an ordered collection of elements where the addition and removal of elements occur at one end, called the stack's top. This operating principle is known as "Last In, First Out" (LIFO) - the last added element is the first to be removed.

Straight to the point, let's look at the methods of the Stack class:

Methods

  • push(E element) (addition): Adds an element to the top of the stack:
java

main.java

Adding is done the same way as in ArrayList, so let's immediately look at this method in combination with the pop() method:

  • pop(): Removes and returns the element from the top of the stack:
java

main.java

This method removed one element from the top of the stack. Note that the pop() method removes the last added element from the stack. That's exactly how the LIFO principle works.

We can also see which element is at the top of the stack:

  • Peek: Returns the element from the top of the stack without removing it:
java

main.java

With this method, we look at the top element in the stack.

Usage

Let's consider an example of using the stack data structure for navigating between pages in a browser (those forward and backward arrows that we frequently use).

Let's plan the implementation of the browser history and implement methods for two buttons (goBack() and goForward()). If you're not sure which buttons I'm referring to, I'm talking about these buttons:

content

Let's implement a class that has methods for operating these two buttons using the Stack data structure.

How it will work:

  • We will have two stacks and a String variable. The first stack will store the links we navigate by clicking the "back" arrow. The second stack will store the links we navigate by clicking the "forward" arrow. We will also have a String variable that stores the current page's link;
  • In this class, there will be four methods: visitPage(), goBack(), goForward(), and getCurrentPage(). Let's go through them step by step:
    • The visitPage() method will redirect us to the URL specified in the parameter. When moving to a new page, the old link will be added to the backStack. The forwardStack will be cleared as we move to a new page.

Let's look at the implementation in the code:

java

BrowserHistory.java

  • This way, we can return to the previous page when navigating to a new page. Let's implement the method for going back. It will work like this: we add the current link to the forwardStack, then remove this link from the backStack, and assign it to currentUrl. Let's look at the implementation in the code:
java

BrowserHistory.java

  • I'll remind you that the pop() method removes the element from the top of the stack and returns it. Therefore, using this method, we immediately assign the value of the URL to the variable currentUrl. We also check to ensure that the backStack is not empty; otherwise, we won't be able to go back to the previous link (simply because it won't be there). If the stack is empty, we output a corresponding message;
  • In the same manner, we implement the method for navigating to the forward page. We simply swap the elements in the stack:
java

BrowserHistory.java

  • Now, all that's left is to implement the getCurrentPage() method, which will simply return the value of currentUrl.

Testing

Next, let's test all of this in the main method. We'll use the visitPage() method three times to ensure these links are saved in the history. Then, we'll use the goBack() method twice, followed by the goForward() method once, to verify the functionality of the written methods.

During this process, we'll track our state using the getCurrentPage() method. You can run the code below and also try inserting more links and using different methods to test the functionality of this class:

java

Main.java

Note

If the large number of console outputs is bothersome, you can remove the console print operations from the methods, leaving only intermediate screen outputs.

Remember that the Stack class is deprecated, and it's better to use Deque instead. In this example, Stack is implemented based on the LIFO principle, and we can also implement Deque since it is a double-ended queue that can operate based on both FIFO and LIFO principles.

If you encounter difficulties understanding the example or the code in general, feel free to click on "Unclear?" and ask your question. I'll do my best to help you understand the material as soon as possible!

Also, don't forget that you can take advantage of the "Chat With Community" feature, where users like you are ready to help you!

1. What is the primary principle of a `Stack` data structure?
2. Which Java interface does `Stack` class implement?
3. What is the method used to add an element to the top of the stack in Java?
4. Which of the following Java collections is considered a more modern alternative to `Stack`?
5. In Java, what will the `pop()` method of a `Stack` return?

What is the primary principle of a Stack data structure?

Selecione a resposta correta

Which Java interface does Stack class implement?

Selecione a resposta correta

What is the method used to add an element to the top of the stack in Java?

Selecione a resposta correta

Which of the following Java collections is considered a more modern alternative to Stack?

Selecione a resposta correta

In Java, what will the pop() method of a Stack return?

Selecione a resposta correta

Tudo estava claro?

Seção 2. Capítulo 4
some-alt