티스토리 뷰

알고리즘

10828번 스택

살구르 2017. 1. 14. 03:36

자바로 스택을 구현하는 방법은 여러가지가 있다.

1. 배열 이용하기

import java.io.*;
import java.lang.*;
import java.util.*;

public class Main{
    public static void main(String[] args) throws java.lang.Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = null;
        int N = Integer.parseInt(br.readLine());
        ArrayStack stk = new ArrayStack(100000);
        for(int i=0;i<N;i++){
            s = br.readLine();
            String[] temp = s.split(" ");
            if(temp.length==1){
                if(temp[0].equals("top"))
                    System.out.println(stk.top());
                else if(temp[0].equals("size"))
                    System.out.println(stk.size());
                else if(temp[0].equals("pop"))
                    System.out.println(stk.pop());
                else if(temp[0].equals("empty"))
                    System.out.println(stk.empty());               
            }else{
                stk.push(Integer.parseInt(temp[1]));
            }
        }
    }
   
   
}

class ArrayStack{
    private int top;
    private int maxSize;
    private int size;
    private int[] stack;
   
    public ArrayStack(int maxSize){
        this.maxSize=maxSize;
        this.stack = new int[maxSize];
        this.top=-1;       
    }
   
    public int empty(){
        if(top==-1)
            return 1;
        else
            return 0;
    }
   
    public void push(int x) throws ArrayIndexOutOfBoundsException{
        if(top==maxSize-1){ // 스택이 가득 찬 경우
            throw new ArrayIndexOutOfBoundsException("꽉 참");
        }else{
            stack[++top]=x;
        }
    }
   
    public int size(){
        return top+1;
    }
   
    public int top(){
        if(empty()==1)
            return -1;
        else
            return stack[top];
    }
   
    public int pop(){
        if(empty()==1)
            return -1;
        else{
            top--;
            return stack[top+1];          
        }
    }   
}

 

2. 연결리스트 구현 


class ListStack{
    private Node top;
    private int size;
   
    private class Node{
        private int data;
        private Node next;
       
        Node(int data){
            this.data = data;
            this.next = null;
        }
    }

    public ListStack(){
        this.top = null;
    }

    public int empty(){
        if(top==null)
            return 1;
        else
            return 0;
    }
   
    public int top(){
        if(empty()==1)
            return -1;
        else
            return top.data;
    }
   
    public int pop(){
        if(empty()==1)
            return -1;
        else{
            int num = top.data;
            top=top.next;
            size--;
            return num;
        }
    }
   
    public void push(int data){
        Node newNode = new Node(data);
        newNode.next = top;
        top=newNode;
        size++;
    }
   
    public int size(){
        return size;
    }
}   

 

3. ArrayList를 활용한 구현

class ArrayListStack{
    private ArrayList list;
   
    public ArrayListStack(){
        list = new ArrayList<Integer>();
    }
   
    public int size(){
        return list.size();
    }
   
    public void push(int num){
        list.add(num);
    }
   
    public int pop(){
        if(list.isEmpty())
            return -1;
        int num = (Integer)list.get(list.size()-1);
        list.remove(list.size()-1);
        return num;
    }
   
    public int top(){
        if(list.isEmpty())
            return -1;
        return (Integer)list.get(list.size()-1);
    }
    public int empty(){
        if(list.isEmpty())
            return 1;
        return 0;
    }
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
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
29 30 31
글 보관함