본문 바로가기

Java스터디/문제풀이

#04 자료구조 Stack

TestApp

import java.util.Stack;

public class TestApp {
    public static void main(String[] args) {
//        Stack<Integer> stack = new Stack<Integer>();
        MyStack<Integer> stack = new MyStack<Integer>();
        System.out.println("capacity:" + stack.capacity()); // 10 최초 10
        System.out.println("size:" + stack.size()); // 0
        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.push(9);
        stack.push(10);
        stack.push(11);
        System.out.println("capacity:" + stack.capacity()); // 20 :: 부족하면 2배씩 증가
        System.out.println("size:" + stack.size()); // 11

        System.out.println(stack); // 1 2 3 4 5 6 7 8 9 10 11

        System.out.println(stack.peek()); // 11
        System.out.println(stack); // 1 2 3 4 5 6 7 8 9 10 11

        Integer cnt = stack.pop();
        System.out.println(cnt); // 11
        System.out.println(stack); // 1 2 3 4 5 6 7 8 9 10

        System.out.println("capacity:" + stack.capacity()); // 20
        System.out.println("size:" + stack.size()); // 10

        System.out.println(stack.search(8)); // 3 :: 8은 스택의 3번째 위치에 있다(index 아니고 카운트임)
    }
}

 

MyArrayList

import java.util.ArrayList;

public class MyArrayList<T> {
    public static final int STEP_NEXT = 2;
    private int size;
    private int capacity;
    private Object[] arr;

    public MyArrayList() {
    
    }

    public boolean add(T data){
    
    }

    public T get(int index){
    
    }

    public int size(){
    
    }

    public int capacity(){
    
    }

    public boolean remove(int index){
    
    }

    @Override
    public String toString() {
    
    }
}

 

MyStack

public class MyStack<T> {
    private MyArrayList<T> arr;

    public MyStack() {
        arr = new MyArrayList<>();
    }
	
    // 담을수 있는 용량
    // 용량을 초과한다면 ?? => 용량을 2배로 늘려야한다
    public int capacity() {
    }

	// 현재 담겨져 있는 싸이즈
    public int size() {
    }

	// 스텍에 맨 위에 추가
    public void push(T item) {
    }

	// 스택에 맨위에 값 가져오기
    // 호출후에도 갯수는 변함 없다.
    public T peek() {
    }

	// 스택에 맨위에 값 가져오기
    // 호출하면 맨위에 값은 사라진다.
    public T pop() {
    }

	// 스택에 몇번째 위치에 있는지??
    // 제일 나중에 들어간 값이 위치가 1이다
    // 제일 먼저 들어간 값이 제일 높다
    // 못찾으면 -1 리턴
    public int search(T item) {
        return -1; // 못찾음
    }

	// 값을 볼수 있게 toString 구현
    @Override
    public String toString() {
    
    }
}
반응형