Created
June 11, 2020 05:36
-
-
Save dalcon10028/ca7ce75625b90b7d360fb773664850fc to your computer and use it in GitHub Desktop.
자료구조 큐
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #define SIZE 5 /* 큐의 크기 */ | |
| int menu(void); | |
| void view_queue(), insert_menu(), delete_menu(); | |
| int insertQ(char obj); | |
| char deleteQ(); | |
| int isempty(); | |
| int isfull(); | |
| char queue[SIZE]; // 배열로 만든 큐 | |
| int rear = -1; | |
| int front = -1; | |
| void main(){ | |
| int choice; | |
| do{ | |
| choice = menu(); | |
| switch(choice){ | |
| case 1: insert_menu(); | |
| break; | |
| case 2: delete_menu(); | |
| break; | |
| } | |
| }while(choice!=3); | |
| } | |
| int menu(){ | |
| int i; | |
| printf("Basic Queue Program 1.0:\n"); | |
| printf("1. Insert\n"); | |
| printf("2. Delete\n"); | |
| printf("3. Quit\n"); | |
| do{ | |
| printf("Choose your selection: "); | |
| scanf("%d",&i); | |
| }while(i<1 || i>3); | |
| return i; | |
| } | |
| // 전체 큐 보기 | |
| void view_queue(){ | |
| int i; | |
| printf("+0+1+2+3+4+\n"); | |
| printf("+-+-+-+-+-+\n"); | |
| for(i=0; i<=front; i++) | |
| printf("| "); | |
| for(i=front+1; i<=rear; i++) | |
| printf("|%c",queue[i]); | |
| printf("\n"); | |
| printf("+-+-+-+-+-+\n"); | |
| printf("front = %d, rear = %d\n\n", front, rear); | |
| } | |
| // 큐에 객체를 삽입하는 메뉴 | |
| void insert_menu(){ | |
| char obj; | |
| printf("Insert New Object : "); | |
| getchar(); | |
| scanf("%c",&obj); | |
| if(insertQ(obj)) view_queue(); | |
| } | |
| int insertQ(char obj){ | |
| int i; | |
| if(isfull()){ | |
| printf("Queue is full\n"); | |
| return 0; | |
| } | |
| if(rear == SIZE-1){ | |
| for(i=front+1; i<SIZE; i++) | |
| queue[i-front-1] = queue[i]; | |
| rear-=front+1; | |
| front = -1; | |
| } | |
| queue[++rear] = obj; | |
| return 1; | |
| } | |
| int isfull(){ | |
| if(rear==SIZE-1 && front==-1) return 1; | |
| return 0; | |
| } | |
| void delete_menu(){ | |
| char obj; | |
| if(obj=deleteQ()) view_queue(); | |
| } | |
| char deleteQ(){ | |
| char ch; | |
| if(isempty()){ | |
| printf("Queue is empty!!\n"); | |
| return 0; | |
| } | |
| ch = queue[++front]; | |
| if(front==rear) front = rear = -1; | |
| return ch; | |
| } | |
| int isempty(){ | |
| if(front == rear) return 1; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment