ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Data Structure] Java ์Šคํƒ(Stack) ์‚ฌ์šฉ
    Data Structure 2021. 1. 15. 13:53
    ๋ฐ˜์‘ํ˜•
    • ์ด ๊ธ€์€ "์˜จ๋ผ์ธ ์ž๋ฐ” ์Šคํ„ฐ๋”” ๋‚ด์šฉ"์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

    Stack์ด๋ž€?

    • ์Šคํƒ์€ ์ฑ…์„ ์Œ“๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์ž…๋‹ˆ๋‹ค.

    • ์Šคํƒ์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ First In Last Out (FILO) ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


    ๋ฐฐ์—ด๋กœ ์Šคํƒ ๊ตฌํ˜„

    public class Stack {
    
        final int MAX_SIZE = 100;
        int arr[];
        int size;
    
        public Stack(){
            arr = new int[MAX_SIZE];
            size = 0;
        }
    
        public void push(int data){
            if(size<MAX_SIZE){
                arr[size++] = data;
            }
        }
    
        public int pop(){
            if(size>0){
                size--;
                return 1;
            }
            return 0;
        }
    
        public int top(){
            if(size>0){
                return arr[size-1];
            }
            return -1;
        }
    
        public boolean isFull(){
            if(size == MAX_SIZE){
                return true;
            }
            return false;
        }
    }
    public class Main {
        public static void main(String[] args) {
            Stack stack = new Stack();
            stack.push(1);
            stack.push(2);
            stack.push(3);
    
            System.out.println(stack.top());        // 3
            stack.pop();
            System.out.println(stack.top());        // 2
            stack.pop();
            System.out.println(stack.top());        // 1
            stack.pop();
        }
    }

    ๋…ธ๋“œ๋กœ ์Šคํƒ ๊ตฌํ˜„

    public class ListNode {
        int data;
        ListNode next;
    
        public ListNode(int data) {
            this.data = data;
            next = null;
        }
    }

    ๋‹ค์Œ ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

    public class ListNodeStack {
    
        ListNode head;
        int size;
    
        public ListNodeStack() {
            head = null;
            size = 0;
        }
    
        void push(int data){
            ListNode node = head;
            if(size == 0){
                node = new ListNode(data);
                head = node;
            }
            else{
                while(node.next != null){
                    node = node.next;
                }
                node.next = new ListNode(data);
            }
            size++;
        }
    
        int pop(){
            ListNode node = head;
            int data;
            if(size == 1){
                data = node.data;
                node = null;
                head = node;
            }
            else{
    
                while(node.next.next!=null){
                    node = node.next;
                }
                data = node.next.data;
                node.next = null;
            }
            size--;
            return data;
        }
    
        public int getSize() {
            return size;
        }
    }
    public class Main {
        public static void main(String[] args) {
    
            ListNodeStack stack = new ListNodeStack();
            stack.push(1);
            stack.push(2);
            stack.push(3);
    
            System.out.println(stack.getSize());        // 3
    
            System.out.println(stack.pop());        // 3
            System.out.println(stack.pop());        // 2
            System.out.println(stack.pop());        // 1
        }
    }
    ๋ฐ˜์‘ํ˜•
Designed by Tistory.