본문 바로가기

알고리즘(Java)/덱

백준 10866 덱

 

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		
		int commandCt = Integer.parseInt(sc.nextLine());
		String commandLine = null;
		
		ArrayDeck deck = new ArrayDeck(commandCt);
		
		for(int i = 1; i <= commandCt; i++){
			commandLine = sc.nextLine();
			String[] temp = commandLine.split(" ");
			
			if(temp[0].equals("push_front")){
				deck.push_front(Integer.parseInt(temp[1]));
			}else if(temp[0].equals("push_back")){
				deck.push_back(Integer.parseInt(temp[1]));
			}else if(temp[0].equals("pop_front")){
				System.out.println(deck.pop_front());
			}else if(temp[0].equals("pop_back")){
				System.out.println(deck.pop_back());
			}else if(temp[0].equals("size")){
				System.out.println(deck.size());
			}else if(temp[0].equals("empty")){
				System.out.println(deck.empty());
			}else if(temp[0].equals("front")){
				System.out.println(deck.front());
			}else if(temp[0].equals("back")){
				System.out.println(deck.back());
			}
		}
		sc.close();
	}
	
	
}

class ArrayDeck{
	private int[] deck;
	private int rear = -1;
	
	public ArrayDeck(int n){
		deck = new int[n];
	}
	
	public void push_front(int value){
		rear++;
		

for(int i=rear;i>0;i--){

                                deck[i]=deck[i-1];

                              }

deck[0] = value; } public void push_back(int value){ rear++; deck[rear] = value; } public int pop_front(){ if(rear <= -1){ return -1; } int temp = deck[0];

for(int i=0;i<rear;i++){

                                deck[i]=deck[i+1];

}

rear--; return temp; } public int pop_back(){ if(rear <= -1){ return -1; } int temp = deck[rear]; rear--; return temp; } public int size(){ if(rear <= -1){ return 0; }else{ return rear + 1; } } public int empty(){ if(rear <= -1){ return 1; }else{ return 0; } } public int front(){ if(rear <= -1){ return -1; }else{ return deck[0]; } } public int back(){ if(rear <= -1){ return -1; }else{ return deck[rear]; } } }

'알고리즘(Java) > ' 카테고리의 다른 글

백준 1021번 회전하는 큐  (0) 2017.11.29
백준 1158 조세퍼스 문제 0  (0) 2017.11.23
[자료구조]덱(Deque)  (0) 2017.11.23