[C] Stack 구현 (non -Circular)
- Code
#include<stdio.h>
#define MAX_SIZE 10
void push(int data);
int pop();
int stack_full();
int stack_empty();
int stack[MAX_SIZE] = {};
int top = -1;
int main(){
int want= 0; //operation number
int input = 0; //input data
while (1) {
printf("What do you want? 1.push 2.pop 3.end :");
scanf_s("%d", &want);
switch (want) {
case 1:
if (stack_full()) // is stack full?
printf("Now stack is full... you can't push.\n\n");
else {
printf("Input push number: ");
scanf_s("%d", &input);
push(input);
printf("Done.\n");
}
break;
case 2:
if (stack_empty()) // is stack empty?
printf("Now stack is empty... you can't pop.\n\n");
else {
printf("pop is %d \n", pop());
}
break;
case 3:
return 0;
}
}
}
void push(int data)
{
top++;
stack[top] = data;
}
int pop()
{
int temp = stack[top];
top--;
return temp;
}
int stack_full()
{
if (top >= MAX_SIZE-1)
return 1;
else
return 0;
}
int stack_empty()
{
if (top == -1)
return 1;
else
return 0;
}
'공부 > C, C++' 카테고리의 다른 글
[C/C++] C 에서 C++로의 변화 - 구조체 (0) | 2022.03.21 |
---|---|
[C] Queue 구현 (non-Circular) (0) | 2022.03.14 |
[C] 링크드 리스트 LinkedList (0) | 2022.03.13 |
[C] 함수 포인터 (0) | 2022.03.12 |
[C] Bubble sort (0) | 2022.03.11 |
댓글