package bit08.Stack.Queue;
import java.util.Scanner;
public class MemorryTest {
public static void main(String[] args) {
Memory m = null;
MyQueue mq = new MyQueue();
MyStack ms = new MyStack();
Scanner sc = new Scanner(System.in);
while (true) {
boolean exit_flag = false;
System.out.println("Menu");
System.out.println("1.Stack 2.Queue 3.Exit");
switch (sc.nextInt()) {
case 1:
m = ms;
break;
case 2:
m = mq;
break;
case 3:
System.out.println("프로그램을 종료헙니다.");
System.exit(-1);
break;
default:
break;
}
while (true) {
System.out.println("1.Push 2.Pop 3.Exit");
switch (sc.nextInt()) {
case 1:
System.out.print("Push ==> ");
m.push(sc.nextInt());
break;
case 2:
System.out.print("Pop Out : ");
System.out.println(m.pop());
break;
case 3:
exit_flag = true;
break;
default:
System.out.println("Input Error");
break;
}
if (exit_flag == true) {
break;
}
}
}
}
}
abstract class Memory {
int a[];
int count;
public Memory() {
// TODO Auto-generated constructor stub
a = new int[5];
count = 0;
}
public void push(int num) {
if (count > 4) {
System.out.println("더 이상 값을 넣을 수 없습니다. Pop하세요^^");
} else {
a[count++] = num;
}
}
public abstract String pop();
}
class MyQueue extends Memory {
int front;
public MyQueue() {
// TODO Auto-generated constructor stub
super();
front = 0;
}
@Override
public String pop() {
// TODO Auto-generated method stub
if (count < 1) {
return "더 이상 값이 없습니다.";
} else {
int tmp = 0;
tmp = a[0];
for (int i = 0; i < a.length - 1; i++) {
a[i] = a[i + 1];
}
a[a.length - 1] = 0;
count--;
return String.valueOf(tmp);
}
}
}
class MyStack extends Memory {
public MyStack() {
// TODO Auto-generated constructor stub
super();
}
@Override
public String pop() {
// TODO Auto-generated method stub
if (count < 1) {
return "더 이상 값이 없습니다.";
} else
return String.valueOf(a[--count]);
}
}
[제어문][lavel문] break & continue (0) | 2017.10.13 |
---|---|
추상 메소드// 인터페이스 (0) | 2017.10.02 |
댓글 영역