티스토리 뷰

Introduction

One of the most famous data structures used in JAVA is Stack.

Stack follows "LIFO (Last In First Out)" order, meaning a data value pushed on stack firstly is popped with priority. This rule Stack data structure follows may be unfamiliar to us, as we are more used to the concept of lining up. In the formation of lining up, the person who walked into a line earlier would enter into the place whatever the line is for (e.g. restaurant) faster. However, in Stack data structure, even though the person arrived earlier, they could not take temporal advantage in entering over others.

 


Implementation

 

Stack in JAVA uses the keyword 'Stack', and declared in a similar sense with any other data structures in JAVA (e.g. Arrays, ArrayList, etc.). Below is how an empty stack variable is declared using JAVA.

import java.util.Stack;

public class StackExample {
	public void createStack() {
		Stack<String> strStack = new Stack<>();
	}
}

code block 1-1: Code snippets that create an empty Stack taking as elements String type values.

 

Now, as we have already created an empty Stack, all we are to do is to enjoy every various methods that are allowed with Stack data structure!

Methods in Stack data structure

1. push(data_type data_value)

import java.util.Stack;

public class StackExample {
    public void createStack() {
        Stack<String> strStack = new Stack<>();

        strStack.push("Element 1");
        strStack.push("Element 2");
        strStack.push("Element 3");

        System.out.println(strStack);
    }

    public static void main(String args[]) {
        StackExample test = new StackExample();
        test.createStack();
    }
}

code block 1-2: Code snippets that add values "Element 1", "Element 2", "Element 3" to Stack data structure and print.

 

The code above adds three elements each using 'push' method. Output of the above code is like below:

[Element 1, Element 2, Element 3]

2. pop()

import java.util.Stack;

public class StackExample {
    public void createStack() {
        Stack<String> strStack = new Stack<>();

        strStack.push("Element 1");
        strStack.push("Element 2");
        strStack.push("Element 3");
		
        strStack.pop();
        strStack.pop();
        
        System.out.println(strStack);
    }

    public static void main(String args[]) {
        StackExample test = new StackExample();
        test.createStack();
    }
}

code block 1-3: Code snippets that delete values from Stack data structure and print.

 

As Stack follows LIFO order, The code above deletes the elements with the priority given to the most recently added element. Therefore, the code above outputs below:

[Element 1]

Conclusion

The most important point to remember today is the below:

Stack data structure is useful when

1. Implementation of Depth First Search (DFS) algorithm

2. Calling recursive functions

as it follows LIFO order.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함