JAVA

[Java] Stack 구현

SangRok Jung 2022. 5. 31. 18:21
반응형

 

 

 

구조


LIFO(선입후출) 구조.

 

 

* 스택 포인터

* 스택

 

public class stack {
    int top = -1;
    final Object[] stack;
    }

 

 

 

 

 

 

 

 

생성자


스택의 사이즈 설정

 

public class stack {

    // 생성자
    // Set the Size of the Stack
    public stack(){
        this.stack = new Object[10];
    }
    public stack(int stackSize){
        this.stack = new Object[stackSize];
    }
    }

 

 

 

 

 

 

 

 

 

기능


1. 스택의 데이터가 가득 차있는지 확인하는 기능.

2. 스택의 데이터가 비어 있는지 확인하는 기능.

3. 스택에 데이터를 추가하는 기능.

4. 스택에 있는 데이터를 빼는 기능.

5. 스택에 있는 데이터를 확인 하는 기능.

 

 

import java.util.Arrays;
public class stack {

    // checking the empty of the data
    public boolean isEmpty(){
        if(-1 == top){
            return true;
        }
        return false;  
    }

    // Checking the full of the data
    public boolean isFull(){
        if(stack.length - 1 == top){
            return true;
        }
        else{
            return false;
        }
        
    }

    // Stack push Function
    public void push(Object data){
        if(isFull()){
            throw new RuntimeException("stack is full");
        }
        else{
            stack[++top] = data;
        }
    }

    // Stack Pop Function
    public Object pop(){
        if(isEmpty()){
            throw new RuntimeException("stack is empty");
        }
        else{
            Object tempData = stack[top];
            stack[top--] = null;
            return tempData;
        }
    }

    // Stack Cehck Function
    public String check(){
        return Arrays.toString(stack);
    }
}

 

 

 

 

 

 

 

 

출력


import java.util.Arrays;

    public static void main(String[] args) {
        stack cs = new stack(10);

        cs.push(10);
        cs.push(20);
        cs.push(30);
        System.out.println(cs.stack[0]);
        System.out.println(cs.top);
        System.out.println(cs.check());
    } 
}

 

 

 

 

+심화


public class stack {
    public static final int DEFAULT_STACK_SIZE = 10;
    public static final int ERROR_STACK_FULL = 0;
    public static final int SUCCESS_STACK_INSERT = 1;
    public static final int ERROR_STACK_EMPTY = Integer.MIN_VALUE;
    public static final int ERROR_INVALID_VALUE = Integer.MAX_VALUE;

    private int[]   frame;      //Stack Memory
    private int     size;       //Stack Memory size
    private int     pointer;    //Stack Sequnce Numbers
    private int     count;      //Number of data in to the Stack

    public stack(){
        this(stack.DEFAULT_STACK_SIZE);
    }

    public stack(int _size){
        size = _size;
        frame = new int[size];
        pointer = 0;
        count = 0;
    }


    //get the size
    public int getSize(){
        return size;
    }

    //get the number of data in to stack
    public int getCount(){
        return count;
    }
    
    private boolean isFull(){
        if(size == getCount())
            return true;

        return false;
    }

    private boolean isEmpty(){
        if(0 == getCount())
            return true; 
            
        return false;
    }

    //push the date in to stack
    public int push(int _data){
        if(isFull())
            return ERROR_STACK_FULL;
        
        if(_data == ERROR_STACK_EMPTY || _data == ERROR_INVALID_VALUE)
            return ERROR_INVALID_VALUE;

        frame[pointer] = _data;
        pointer++;
        count++;
        return SUCCESS_STACK_INSERT;
    }

    public int pop(){
        int result = 0;

        if(isEmpty())
            return ERROR_STACK_EMPTY;
            
        pointer--;
        count--;
        result =frame[pointer];

        return result;
    }
}
반응형