conio.h <- 이거 먼지 잘모르겠다.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define EMPTY 0
struct node {
int data;
struct node * link;
};
typedef struct node Stack;
Stack * get_node(){
Stack * tmp;
tmp = (Stack *)malloc(sizeof(Stack));
tmp->link = EMPTY;
return tmp;
}
void push(Stack **top, int data){
Stack *tmp;
tmp = *top;
*top = get_node();
(*top)->data = data;
(*top)->link = tmp;
}
void pop(Stack **top){
Stack *tmp;
if (*top == EMPTY){
printf("Stack is empty!!!");
return;
}
tmp = *top;
printf("%d\n", tmp->data);
*top = (*top)->link;
free(tmp);
}
void print_data(Stack ** top){
Stack * tmp = *top;
if (tmp == EMPTY)
{
printf("Stack is empty!!!");
return;
}
else
{
while (1){
if (tmp != EMPTY){
printf("%d\n", tmp->data);
tmp = tmp->link;
}
else{
break;
}
}
}
}
void main() {
Stack * top = EMPTY;
int n, d;
while (1){
printf("stack프로그램 입니다.\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.print_data\n");
printf("4.종료\n");
printf(">>>>>>:");
scanf_s("%d", &n);
switch (n){
case 1: printf("데이터를 입력하세요\n");
scanf_s("%d", &d); push(&top, d); break;
case 2: pop(&top); getchar();
break;
case 3: print_data(&top); getchar(); break;
case 4: while (top!=NULL)
{
pop(&top);
}
printf("top->[%p]\n", top);
exit(1);
}
system("cls");
}
}