-
C언어- 스택Stack(C언어 코드로 구현하기)macOS코딩Coding/알고리즘♡Algorithm♡ 2022. 9. 12. 06:27728x90
// Stack Example #include <stdio.h> #include <stdlib.h> int Stack[10]; int top=-1; int push(int dat); int pop(void); int printstack(void); int main(void) { int inval, innum; printf(" - Algorithm Stack - \n"); printf("Made by POM(lovebeen04@gmail.com)\n"); printf("=================================\n"); printf("1. Push Value\n"); printf("2. Pop Value\n"); printf("3. Print Stack\n"); printf("4. Exit\n"); printf("=================================\n"); printf("> Enter Number: "); scanf("%d", &inval); switch(inval) { case 1: system("clear"); printf("> Enter Number: "); scanf("%d", &innum); push(innum); break; case 2: pop(); break; case 3: printstack(); break; case 4: printf("Bye ~ Bye~ \n"); return 0; default: system("clear"); printf(" Error\n"); break; } return 0; } int push(int dat) { if(top >= 9) { system("clear"); printf("Stack Overflow\n"); getchar(); system("clear"); main(); } else { top++; Stack[top] = dat; system("clear"); printf("Push %d\n", dat); getchar(); system("clear"); main(); } } int pop(void) { system("clear"); if(top <= -1) { system("clear"); printf("Stack Downflow\n"); getchar(); system("clear"); main(); } else { Stack[top] = 0; top--; main(); } } int printstack(void) { int a; system("clear"); printf("=================================\n"); for(a=9; a>=0; a--) printf("%d\n", Stack[a]); printf("=================================\n"); getchar(); main(); }
conio.h 를 대체한 코드 getchar를 쓰자!
https://www.quora.com/What-can-be-a-standard-alternative-to-getch
* Stack Example */ #include <stdio.h> #include <stdlib.h> #include <conio.h> int Stack[10]; int top=-1; int push(int dat); int pop(void); int printstack(void); int main(void) { int inval, innum; printf(" - Algorithm Stack - \n"); printf("Made by POM(lovebeen04@gmail.com)\n"); printf("=================================\n"); printf("1. Push Value\n"); printf("2. Pop Value\n"); printf("3. Print Stack\n"); printf("4. Exit\n"); printf("=================================\n"); printf("> Enter Number: "); scanf("%d", &inval); switch(inval) { case 1: system("cls"); printf("> Enter Number: "); scanf("%d", &innum); push(innum); break; case 2: pop(); break; case 3: printstack(); break; case 4: system("pause"); break; default: system("cls"); printf(" Error\n"); break; } return 0; } int push(int dat) { if(top >= 9) { system("cls"); printf("Stack Overflow\n"); getch(); system("cls"); main(); } else { top++; Stack[top] = dat; system("cls"); printf("Push %d\n", dat); getch(); system("cls"); main(); } } int pop(void) { system("cls"); if(top <= -1) { system("cls"); printf("Stack Downflow\n"); getch(); system("cls"); main(); } else { Stack[top] = 0; top--; main(); } } int printstack(void) { int a; system("cls"); printf("=================================\n"); for(a=9; a>=0; a--) printf("%d\n", Stack[a]); printf("=================================\n"); getch(); system("cls"); main(); }
출처 :
반응형'코딩Coding > 알고리즘♡Algorithm♡' 카테고리의 다른 글
C++❤️deque 예제 코드 (0) 2022.09.20 Data Structure❤️Queues in C 큐 구현하기C언어 코드 (0) 2022.09.19 C++❤️Container classes (0) 2022.09.18 Queue Program In C (0) 2022.09.18 알고리즘❤️Stack스택& Queue큐&LIFO&FIFO개념이해❤️Nomad Coders (0) 2022.09.12 알고리즘_데이터구조_Hash Table 의 모든 것! (0) 2022.09.12 8. Analysis of Multithreaded Algorithms❤️MIT OpenCourseWare (0) 2022.08.28 Ep2. Models of computation, Document Distance | MIT❤️ (0) 2022.08.28