본문 바로가기

공부/컴퓨터

[자료구조] C로 구현한 Queue..

반응형


001: #include <stdio.h>
002:
003: // LIST 구조체 만들기.

004: typedef struct tag_list {
005:
006: char title[16];
007: char name[16];
008: struct tag_list* next;
009:
010: } LIST;
011:
012:
013: // 전체적인 QUEUE를 가르킬 queue 만들디.

014: LIST* queue=NULL;
015:
016:
017: // 데이터 삽입

018: // LIST* member로 넘어온 값을 삽입한다.

019: void enqueue(LIST* member)
020: {
021: LIST* t_list;
022: LIST* s_list;
023:
024:
025: if ( queue == NULL )
026: {
027: // 만약 제일 처음 만들어진다면 node를 하나 만들도 자기의 다음을 자기자신을 가르키게한다.

028: queue = (LIST*)malloc(sizeof(LIST));
029: memcpy(queue->title,member->title,sizeof(member->title));
030: memcpy(queue->name,member->name,sizeof(member->name));
031: queue->next = queue;
032: }
033: else
034: {
035: // 하나 이상 만들어져 있다면 새로운 노드를 하나 만들고

036: // queue의 제일 마지막을 찾아서 그곳에다가 추가하고

037: // 새로운 노드의 다음을 queue의 처음을 가르키게 한다.

038: t_list = (LIST*)malloc(sizeof(LIST));
039: memcpy(t_list->title,member->title,sizeof(member->title));
040: memcpy(t_list->name,member->name,sizeof(member->name));
041:
042: t_list->next = queue->next;
043:
044: queue->next = t_list;
045:
046: queue = t_list;
047: }
048:
049: }
050:
051:
052:
053: // 큐에서 꺼내기

054: LIST* dequeue()
055: {
056: LIST* s_list = NULL;
057: LIST* t_list = NULL;
058:
059: // 큐가 비어 있다면 예약중인 사람이 없다고 하고 NULL을 반환한다.

060: if ( queue == NULL )
061: {
062: printf("대여 예약 중인 사람이 없습니다.\n");
063: return s_list;
064: }
065: else
066: {
067: // 만약 노드가 하나 밖이라면...

068: if ( queue == queue->next )
069: {
070: // 현재를 리턴 시켜준다.

071: t_list = queue;
072: // queue는 비워준다.

073: queue = NULL;
074: }
075: // 노드가 하나 이상이라면.

076: else
077: {
078: // 리턴 시킬 제일 처음을 t_list에 넣어 두고.

079: t_list = queue->next;
080:
081: // queue의 제일 처음을. 한칸 다음으로 옮겨준다.

082: queue->next = queue->next->next;
083: }
084:
085: // t_list를 반환한다.

086: return t_list;
087:
088: }
089:
090: }
091:
092:
093: // 큐 안에 듣 내용 표시해주기.

094: int view_queue()
095: {
096: int i=0;
097: LIST* s_list;
098:
099: // 큐가 비었다면 -1을 리턴시킨다.

100: if ( queue == NULL ) {
101: return -1;
102: }
103:
104: s_list = queue->next;
105:
106: // 현재 가리키는 곳 다음에 queue가 아닐때까지 하나씩 출력해준다.

107: while( s_list != queue )
108: {
109: i++;
110: printf("%3d번째 이름 : %s\t\t책이름 : %s\n",i,s_list->name,s_list->title);
111: s_list = s_list->next;
112: }
113: // 마지막 하나가 출력이 되지 않았으므로.. 마지막것을 출력해준다.

114: i++;
115: printf("%3d번째 이름 : %s\t\t책이름 : %s\n",i,s_list->name,s_list->title);
116:
117:
118: return 1;
119: }
120:
121:
122: main()
123: {
124:
125: LIST* t_list;
126:
127: char t_name[15]={0,};
128: char t_title[15]={0,};
129: int ans=0;
130:
131: while(ans != 4)
132: {
133:
134: ans=0;
135: printf("책 대여 관리 프로그램\n");
136: printf(" 1. 대여 정보 입력\n");
137: printf(" 2. 대여 처리\n");
138: printf(" 3. 현재 상태 보기\n");
139: printf(" 4. 끝내기\n\n");
140: printf("명령을 입력하세요 ^_^ : ");
141:
142: scanf("%d",&ans);
143:
144: switch(ans) {
145: case 1:
146: printf("대여자명 입력(15byte) : ");
147: scanf("%s",t_name);
148: printf("대여 책 입력 (15byte) : ");
149: scanf("%s",t_title);
150: t_list=(LIST*)malloc(sizeof(LIST));
151: memcpy(t_list->name,t_name,sizeof(t_name));
152: memcpy(t_list->title,t_title,sizeof(t_title));
153: enqueue(t_list);
154: break;
155: case 2:
156: t_list = dequeue();
157: if ( t_list != NULL ) {
158: printf("처리된 사람 이름 : %s\n",t_list->name);
159: printf("대여해간 책 이름 : %s\n",t_list->title);
160: free(t_list);
161: } else {
162: printf("대기자 명단에 아무도 없습니다 ^_^\n");
163: }
164:
165: break;
166: case 3:
167: if ( view_queue() == -1 ) {
168: printf("대기자 명단에 아무도 없습니다.^_^\n");
169: }
170: break;
171: case 4:
172: break;
173: default:
174: printf("제대로된 숫자를 입력해 주세요 ^_^\n");
175: break;
176: }
177:
178: }
179:
180: }

반응형