반응형
Main.java
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package day_07_homework; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Memory me = null; MyStack ms = new MyStack(); MyQueue mq = new MyQueue(); int data; do { System.out.println("\n" + "========================"); System.out.println("수행할 자료구조를 선택하세요."); System.out.println("1. Stack data structure"); System.out.println("2. Queue data structure"); System.out.println("3. Exit"); System.out.println("========================"); int selDS = sc.nextInt(); if(selDS == 1) { // dynamic binding to stack me = ms; } else if(selDS == 2) { // dynamic binding to queue me = mq; } else if(selDS == 3) { // exit System.out.println("프로그램을 종료합니다."); System.exit(0); } else { System.out.println("잘못된 번호를 입력하였습니다. 다시 입력하세요."); continue; } while(true) { System.out.println("\n" + "========================"); System.out.println("수행할 연산을 선택하세요."); System.out.println("1. Push operation"); System.out.println("2. Pop operation"); System.out.println("3. Exit"); System.out.println("========================"); int num = sc.nextInt(); if(num == 1) { if(!me.checkOverflow()) { System.out.println("\n" + "========================"); System.out.println("Input data : "); me.push(sc.nextInt()); System.out.println("Complete!"); System.out.println("========================"); } } else if(num == 2) { if(!me.checkUnderflow()) { data = me.pop(); System.out.println("\n" + "========================"); System.out.println("Print out data : " + data); System.out.println("========================"); if(me instanceof MyQueue) { mq.shiftData(); } } } else if(num == 3) { break; } else { System.out.println("잘못된 번호를 입력하였습니다. 다시 입력하세요."); continue; } } } while(true); } // main period } // class period | cs |
Memory.java
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 32 33 34 35 36 37 38 | package day_07_homework; public abstract class Memory { // field protected int pos; protected int []data; // constructor public Memory() { data = new int[5]; pos = 0; } public boolean checkOverflow() { if(pos > 4) { System.out.println("Overflow!"); return true; } return false; } public boolean checkUnderflow() { if(pos < 1) { System.out.println("Underflow!"); return true; } return false; } public void push(int num) { data[pos++] = num; } // abstract method public abstract int pop(); } | cs |
MyStack.java
1 2 3 4 5 6 7 8 9 | package day_07_homework; public class MyStack extends Memory { public int pop() { return data[--pos]; } } | cs |
MyQueue.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package day_07_homework; public class MyQueue extends Memory { private int front; public MyQueue() { front = 0; } public int pop() { return data[front++]; } public void shiftData() { for(int i = 0; i<pos-1; i++) { data[i] = data[i+1]; } pos--; front--; } } | cs |
반응형