데이터를 일시적으로 쌓아 놓는 자료구조로, 데이터의 입력과 출력 순서는 후입선출(LIFO: Last In First Out) 방식
가장 나중에 넣은 데이터를 가장 먼저 꺼냅니다.
- 스택에 데이터를 넣는 작업을 푸시(push)
- 데이터를 꺼내는 작업을 팝(pop)
- 푸시와 팝이 이루어지는 쪽을 꼭대기(top)
- 가장 아랫부분을 바닥(bottom)
public class IntStackTester{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
IntStack s = new IntStack(64);
while(true){
System.out.println();
System.out.prinf("현재 데이터 개수: %d / %d\n", s.size(), s.getCapacity());
System.out.printf("(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: ");
int menu = stdIn.nextInt();
if(menu == 0) break;
int x;
switch(menu){
case 1:
System.out.print("데이터: ");
x = stdIn.nextInt();
try{
s.push(x);
}catch(IntStack.OverflowIntStackException e){
System.out.println("스택이 가득 찼습니다.");
}
break;
case 2:
try{
x = s.pop();
System.out.println("팝한 데이터는 " + x + "입니다.");
}catch(IntStack.EmptyIntStackException e){
System.out.println("스택이 비어 있습니다.");'
}
break;
case 3:
try{
x = s.peek();
System.out.println("피크한 데이터는 " + x + "입니다.");
}catch(IntStack.EmptyIntStackException e){
System.out.println("스택이 비어 있습니다.");
}
break;
case 4:
s.dump();
break;
}
}
}
}
실행 결과
현재 데이터 개수: 0/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 1
현재 데이터 개수: 1/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 2
현재 데이터 개수: 2/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 3
현재 데이터 개수: 3/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 4
현재 데이터 개수: 4/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 3
피크한 데이터는 4입니다.
현재 데이터 개수: 4/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 4
1 2 3 4
현재 데이터 개수: 4/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 2
팝한 데이터는 4입니다.
현재 데이터 개수: 3/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 2
팝한 데이터는 3입니다.
현재 데이터 개수: 2/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 4
1 2
현재 데이터 개수: 2/64
(1) 푸시 (2) 팝 (3) 피크 (4) 덤프 (0) 종료: 0
데이터를 일시적으로 쌓아 두는 기본 자료구조
가장 먼저 넣은 데이터를 가장 먼저 꺼내는 (FIFO: First In First Out) 방식
- 큐에 데이터를 넣는 작업을 인큐(en-queue)
- 데이터를 꺼내는 작업을 디큐(de-queue)
- 데이터가 나오는 쪽을 프론트(front, 맨 앞)
- 데이터를 넣는 쪽을 리어(rear, 맨 뒤)
class IntQueueTester {
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
IntQueue s = new IntQueue(54);
while(true){
System.out.println();
System.out.print("현재 데이터 개수: %d / %d\n", s.size(), s.getCapacity));
System.out.print("(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료 : ");
int menu = stdIn.nextInt();
if(menu == 0) break;
int x;
switch(menu){
case 1:
System.out.print("데이터: ");
x = stdIn.nextInt();
try{
s.enque(x);
}catch(IntQueue.OverflowIntQueueException e){
System.out.println("큐가 가득 찼습니다.");
}
break;
case 2:
try{
x = s.deque();
System.out.println("디큐한 데이터는 " + x + "입니다.");
}catch(IntQueue.EmptyIntQueueException e){
System.out.println("큐가 비어 있습니다.");
}
break;
case 3:
try{
x=s.peek();
System.out.println("피크한 데이터는 " + x +"입니다.");
}catch(IntQueue.EmptyIntQueueException e){
System.out.println("큐가 비어 있습니다.");
}
break;
case 4:
s.dump();
break;
}
}
}
}
실행 결과
현재 데이터 개수: 0/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 1
현재 데이터 개수: 1/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 2
현재 데이터 개수: 2/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 1
데이터: 1 2
현재 데이터 개수: 2/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 2
디큐한 데이터는 1입니다.
현재 데이터 개수: 1/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 4
데이터: 2
현재 데이터 개수: 1/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 3
피크한 데이터는 2입니다.
현재 데이터 개수: 1/64
(1) 인큐 (2) 디큐 (3) 피크 (4) 덤프 (0) 종료: 0