Created
June 1, 2020 03:00
-
-
Save dalcon10028/fc196b468e331ce50a5085a88c745aa2 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 10 | |
| char stack[SIZE]; // 스택 생성 | |
| int top = -1; // 배열의 맨 위 인덱스(top)를 가리킬 변수 | |
| void view_stack(), push_menu(), pop_menu(); | |
| void topexam_menu(), isempty_menu(), isfull_menu(); | |
| void push(char obj); // 삽입(푸시) | |
| char pop(); // 삭제(팝) | |
| int isfull(); // 가득 차있는지 확인 | |
| int isempty(); // 비어있는지 확인 | |
| void main(void) { | |
| int choice; | |
| do{ | |
| choice = menu(); // 선택한 메뉴를 치환 | |
| switch(choice){ | |
| case 1: push_menu(); | |
| getchar(); | |
| break; | |
| case 2: pop_menu(); | |
| break; | |
| case 3: topexam_menu(); | |
| break; | |
| case 4: isempty_menu(); | |
| break; | |
| case 5: isfull_menu(); | |
| break; | |
| } | |
| }while(choice!=6); | |
| } | |
| // 사용자가 메뉴를 선택하여 선택한 번호를 반환 | |
| int menu(){ | |
| int input; | |
| printf("Basic Stack Program 1.0\n"); | |
| printf("1. push\n"); | |
| printf("2. pop\n"); | |
| printf("3. topexam\n"); | |
| printf("4. isempty\n"); | |
| printf("5. isfull\n"); | |
| printf("6. quit\n"); | |
| do{ | |
| printf("Choose your selection: "); | |
| scanf("%d", &input); | |
| }while(input<1 || input>6); // 1부터 6까지만 입력 가능하게 | |
| return input; // 사용자가 입력한 값을 반환 | |
| } | |
| // 전체 스택 보기 | |
| void view_stack(){ | |
| int i; | |
| printf("+-+-+-+-+-+-+-+-+-+-+\n"); | |
| for(i=0; i<=top; i++) | |
| printf("|%c",stack[i]); | |
| printf("\n+-+-+-+-+-+-+-+-+-+-+\n"); | |
| } | |
| // 스택에 객체를 삽입하는 메뉴 | |
| void push_menu(){ | |
| char obj; | |
| printf("Insert New Object : "); | |
| getchar(); | |
| scanf("%c",&obj); | |
| push(obj); // 객체 삽입 | |
| view_stack(); // push에 성공하면 스택 보여주기 | |
| } | |
| void push(char obj){ | |
| if(isfull()) return; | |
| stack[++top] = obj; // 푸시푸시 베이베 | |
| } | |
| int isfull(){ | |
| if(top==SIZE-1){ // 스택이 꽉차 있는지 확인합니다. | |
| printf("Stack is full!!\n"); | |
| return 1; // 꽉 차있으면 1반환 | |
| }else return 0; // 안차있으면 0반환 | |
| } | |
| void pop_menu() { | |
| char obj; | |
| if(obj=pop()) view_stack(); | |
| // 스택이 비어있지 않으면 pop을 한 후 스택을 보여줍니다. | |
| } | |
| char pop(){ | |
| char ch; | |
| if(isempty()) return 0; // 비어있으면 반환 할 수 없습니다. | |
| ch = stack[top--]; | |
| return ch; | |
| } | |
| // 스택이 비어있는지 확인합니다. | |
| int isempty(){ | |
| if(top==-1){ | |
| printf("Stack is empty!!\n"); | |
| return 1; // 비어있으면 1반환 | |
| }else return 0; // 안비어있으면 0반환 | |
| } | |
| void isfull_menu(){ | |
| if(!isfull()) printf("Stack is not full\n"); // 스택이 꽉 차있지 않으면 | |
| } | |
| void isempty_menu(){ | |
| if(!isempty()) printf("Stack is not empty\n"); // 스택이 비어있지 않으면 | |
| } | |
| void topexam_menu(){ | |
| if (!isempty()) printf("Top data : %c\n",stack[top]); | |
| // 현재 top에 있는 데이터를 보여줍니다. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment