Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- 채보
- pandas
- label
- C#
- LIST
- SSH
- 기타 연주
- Docker
- Visual Studio
- 오류
- Numpy
- pip
- ubuntu
- C
- Linux
- 핑거스타일
- Selenium
- pytorch
- OpenCV
- Python
- VS Code
- windows forms
- JSON
- mysql
- 컨테이너
- paramiko
- YOLO
- C++
- error
Archives
- Today
- Total
기계는 거짓말하지 않는다
스택(Stack) 본문
Last In First Out(LIFO) 구조
#include <stdio.h>
#define SIZE 100
typedef enum eSelectMenu {
selectPush = 1,
selectPop,
selectPeek,
selectSize,
selectExit
} eSelectMenu;
typedef enum eBool{
false,
true
} eBool;
int top = -1; // -1 or 0
int stack[SIZE];
void Push(int data) {
stack[++top] = data;
}
int Pop() {
return stack[top--];
}
eBool IsEmpty() {
return top == -1 ? true : false;
}
eBool IsFull() {
return top + 1 == SIZE ? true : false;
}
int Peek() {
return stack[top];
}
int Size() {
return top + 1;
}
int main(void) {
eSelectMenu select;
int data;
while (1) {
printf("1. push / 2. pop / 3. peek / 4. size / 5. exit\n");
printf("select: ");
scanf_s("%d", &select);
switch (select) {
case selectPush:
if (IsFull() == true) {
printf("stack full\n\n");
break;
}
printf("data: ");
scanf_s("%d", &data);
Push(data);
printf("\n");
break;
case selectPop:
if (IsEmpty() == true) {
printf("stack empty\n\n");
break;
}
printf("return data: %d\n\n", Pop());
break;
case selectPeek:
if (IsEmpty() == true) {
printf("stack empty\n\n");
break;
}
printf("last data: %d\n\n", Peek());
break;
case selectSize:
printf("current stack size: %d\n\n", Size());
break;
case selectExit:
printf("exit program\n");
return 0;
default:
printf("re\n\n");
}
while(getchar() != '\n') {}
}
}
top 의 초기 값이 -1 or 0 인지에 따라 push, pop 전, 후위 증감 연산 위치가 달라야 한다.
'C' 카테고리의 다른 글
이중 연결 리스트(Doubly Linked List) (0) | 2021.04.25 |
---|---|
링크드 리스트(Linked List) (0) | 2020.12.12 |
원형 큐(Circular Queue) (0) | 2020.12.09 |
Short Circuit Evaluation(SCE, 단축 평가) (0) | 2020.12.06 |
C언어 키워드 (0) | 2020.12.05 |
Comments