코락 CoRock
코딩하는 락스타
코락 CoRock
  • 분류 전체보기 (393)
    • frameworks (19)
      • spring (19)
      • spring-boot (0)
      • testing (0)
    • languages (94)
      • java (39)
      • kotlin (0)
      • python (42)
      • r (13)
    • libraries (0)
    • programming (239)
      • android (13)
      • c (17)
      • cpp (22)
      • database (18)
      • design-pattern (4)
      • data-structures (11)
      • git (8)
      • hadoop (6)
      • html-css (7)
      • issue (4)
      • javascript (26)
      • jsp (34)
      • os (29)
      • php (6)
      • preferences (19)
      • etc (15)
    • discography (37)
      • k-pop (18)
      • pop (19)
    • blog (3)

블로그 메뉴

  • Programming
  • Java
  • JavaScript
  • Discography
  • K-Pop Songs
  • Pop Songs
  • Blog
  • Guestbook

공지사항

인기 글

태그

  • r
  • 파이썬
  • javascript
  • jsp
  • Java
  • oracle
  • 자바스크립트
  • Spring
  • Android
  • linux
  • CentOS
  • python

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
코락 CoRock

코딩하는 락스타

programming/c

[DAY 07] Stack and Queue (Version 1.1)

2018. 1. 18. 21:19
반응형

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
Colored by Color Scripter
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();
    
}
Colored by Color Scripter
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];
    }
    
}
Colored by Color Scripter
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--;
    }
 
}
Colored by Color Scripter
cs


반응형
저작자표시 비영리 변경금지 (새창열림)
    'programming/c' 카테고리의 다른 글
    • [DAY 10] FILE I/O
    • [Android] WebView | Thread
    • [DAY 10] const
    • [DAY 09] Memory Allocation
    코락 CoRock
    코락 CoRock
    A COder dreaming of being a ROCKstar

    티스토리툴바