[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;
}
댓글