본문 바로가기

Data Structure/Sample Code

Queue 응용

Queue 응용


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define EMPTY 0
 
 
typedef struct Linkedlist{
    int number;
    struct Linkedlist *link;
    struct Studentlist *datalink;
}queue;
 
typedef struct Studentlist{
    char name[20];
    int kor, eng, math;
}Student;
 
queue * Initial_queue(){
    queue *tmp = (queue*)malloc(sizeof(queue));
    tmp->link = NULL;
    return tmp;
}
 
Student * Initial_Stu(){
    Student *tmp = (Student*)malloc(sizeof(Student));
    return tmp;
}
 
Student * Input_data(Student * stu) {
    printf("이   름 : ");
    scanf("%s", stu->name);
    fflush(stdin);
    printf("국어점수 : ");
    scanf("%d", &(stu->kor));
    fflush(stdin);
    printf("수학점수 : ");
    scanf("%d", &(stu->math));
    fflush(stdin);
    printf("영어점수 : ");
    scanf("%d", &(stu->eng));
    fflush(stdin);
 
    return stu;
}
 
queue * find_rear(queue **front){
    queue * tmp;
    tmp = *front;
    if ((*front)->link == NULL)
    {
        return tmp;
    }
    else
    {
        tmp = tmp->link;
        return find_rear(&tmp);
    }
}
 
 
void Add_queue(queue **front){
    static int num = 0;
    queue * tmp;
    queue * rear = Initial_queue();
 
    rear->datalink = Initial_Stu();
    rear->datalink = Input_data(rear->datalink);
    rear->number = num + 1;
    if (*front == NULL) {
        *front = rear;
    }
    else {
        tmp = find_rear(front);
        tmp->link = rear;
    }
 
    num++;
}
queue * Print_queue(queue **front){
    queue *tmp = *front;
    queue * returnval = EMPTY;
    if (*front == EMPTY)
    {
        printf("학생이 입력되지 않았습니다.\n\n");
        return tmp;
    }
    else if (tmp->number != 1)
    {
        printf("%d\t%s\t%d\t%d\t%d\n", tmp->number, tmp->datalink->name, 
tmp->datalink->kor, tmp->datalink->eng, tmp->datalink->math);
        if (tmp->link != NULL)
        {
            return Print_queue(&(tmp->link));
        }
        return returnval;
    }
    else
    {
        printf("%d\t%s\t%d\t%d\t%d\n", tmp->number, tmp->datalink->name,
tmp->datalink->kor, tmp->datalink->eng, tmp->datalink->math);
        return Print_queue(&(tmp->link));
    }
}
 
 
int main(int argc, char *argv[]){
    queue *front = NULL;
    int select;
 
    while (1){
        printf("==================\n");
        printf(" 학생관리프로그램\n");
        printf("==================\n");
        printf(" 1.학생등록\n");
        printf(" 2.학생정보\n");
        printf(" 3.종료\n");
        printf(" 선택 : ");
        scanf("%d", &select);
        fflush(stdin);
 
        if (select >= 1 && select <= 4){
 
            switch (select){
            case 1//등록
                system("cls");
                Add_queue(&front);
                system("pause"); system("cls");
                break;
 
            case 2//보기
                system("cls");
                printf("번호\t이름\t국어\t영어\t수학\n");
                Print_queue(&front);    printf("\n");
                //test(&front);
                system("pause"); system("cls");
                break;
            case 3//종료
                printf("프로그램을 종료 합니다.\n");
                exit(0);
                break;
            }
        }
        else
        {
            printf("잘못된 선택입니다\n");
            system("pause");
            system("cls");
        }
    }
    return 0;
}
cs


'Data Structure > Sample Code' 카테고리의 다른 글

Tree  (0) 2015.08.19
LinkedList  (0) 2015.08.19
Queue  (0) 2015.08.19
Stack  (0) 2015.08.19