1 개념[ | ]
- C stack 구현
- C언어 스택 구현
C
CPU
0.0s
MEM
18M
0.1s
Copy
#include <stdio.h>
#define MAX_N 100
int top = 0;
int stack[MAX_N];
void clear() {
top = 0;
}
int empty() {
return top == 0;
}
void push(int value) {
if (top == MAX_N) {
printf("overflow!!!\n");
return;
}
stack[top] = value;
top++;
}
int pop() {
if (empty()) {
printf("empty!!!\n");
return 0;
}
top--;
return stack[top];
}
int main() {
push(1);
push(2);
push(3);
while(!empty()) {
printf("%d ", pop()); // 3 2 1
}
printf("\n");
clear();
push(3);
push(2);
push(1);
while(!empty()) {
printf("%d ", pop()); // 1 2 3
}
printf("\n");
pop(); // empty!!!
}
3 2 1 1 2 3 empty!!!
2 같이 보기[ | ]
편집자 Jmnote
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- C (7)
C, C++ 주석 ― YkhwongC, C++ 주석 ― John JeongC, C++ 주석 ― JmnoteC, C++ 주석 ― John JeongC언어 연결리스트 구현 ― 돌멩이C언어 연결리스트 구현 ― John JeongC언어 연결리스트 구현 ― 돌멩이