자료구조

ims·2020년 11월 6일
0

알고리즘

목록 보기
11/23

Stack

public class StackPractice {
    public static void main(String args[]){
        Stack<Integer> stack = new Stack<>();

        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        stack.push(7);
        stack.push(8);
        stack.pop();
        stack.pop();

        while (!stack.isEmpty()){
            System.out.println(stack.peek());
            stack.pop();
        }
    }
}
  • push, pop

  • while(!stack.isEmpty)

Queue

public class QueuePractice {
    public static void main(String args[]){
        Queue<Integer> q = new LinkedList<>();

        q.offer(5);
        q.offer(3);
        q.offer(4);

        q.poll();

        while(!q.isEmpty()){
            System.out.println(q.poll());
        }
    }
}
  • offer = add , poll = minus

add, offer -> 오류때 차이

https://goodteacher.tistory.com/112

  • poll을 하면서 data를 반환한다.

Recurse

public class RecursePractice {
    public static void main(String[] args) {
        recurse(10);
    }

    static void recurse(int i){

        if(i<0) return ;

        System.out.println(i + "번째");
        recurse(i-1);
    }
}

유클리드 호제법

A와B의 최대공약수는 A%B = R (A>B), B와 R의 최대공약수와 같다.

--> Recursion 가능

profile
티스토리로 이사했습니다! https://imsfromseoul.tistory.com/ + https://camel-man-ims.tistory.com/

0개의 댓글