#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int data;
struct _node * next;
} Node;
Node *head = NULL;
////// stack의 기본함수 push(), pop(), +알파함수 peek()
void push(int data){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
if(head == NULL){
newNode->next = NULL;
head = newNode;
}
else{
newNode->next = head;
head = newNode;
}
}
int pop(){
int data = -1;
if(head == NULL){
printf("Empty\n");
}
else{
Node *cur = head;
data = cur->data;
head = cur->next;
free(cur);
}
return data;
}
int peek(){
if(head == NULL){
printf("Empty\n");
return -1;
}
else{
return head->data;
}
}
////////// stack 추가 함수
void initStack(){
head = NULL;
}
int isEmpty(){
if(head == NULL){
return 1; //true
}
else{
return 0; //false
}
}
int isFull(){} //존재할 필요가 없음
void printStack(){
Node *cur = head;
while(cur != NULL){
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n\n");
}
void Free(){
Node *cur;
while(head != NULL) {
cur = head;
head = head->next;
free(cur);
}
}
///////// 메인 함수
void main(){
push(1); printStack();
push(3); printStack();
push(5); printStack();
printf("POP : %d\n", pop()); printStack();
printf("PEEK : %d\n", peek()); printStack();
printf("POP : %d\n", pop()); printStack();
printf("PEEK : %d\n", peek()); printStack();
printf("POP : %d\n", pop()); printStack();
Free();
}