Stack
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100 /*최대스택크기*/
typedef struct { char name[20]; //이름
char hakbun[20]; //학번
}element;
element stack[MAX_STACK_SIZE]; //스택크기 MAX
void push();
element pop();
void display(int top,element *item);
void main(){
element item[MAX_STACK_SIZE];
int top = -1; //스택 start
int a=0;
while(1){
printf("***********************************\n");
printf("******* 메 뉴 ******\n");
printf("***********************************\n");
printf(" 1.삽입\n");
printf(" 2.삭제\n");
printf(" 3.출력\n");
printf(" 4.종료\n");
printf(" 선택:");scanf("%d",&a);
if(a==1){ //삽입
push(&top,&item);
}else if(a==2){ //삭제
pop(&top,&item);
}else if(a==3){ //출력
display(top,&item);
}else if(a>=4){
printf("프로그램이 종료되었습니다.\n");
exit(-1);
}
}
}
void push(int *top, element *item){
int i=0;
if(*top>= MAX_STACK_SIZE -1){
printf("스택이 꽉찼습니다.\n");
return;
}
printf("Name : "); gets(stack[++(*top)].name);
printf("\n");
printf("Hakbun : "); gets(stack[*top].hakbun);
}
element pop( int *top, element *item){
if(*top == -1){
printf("스택이비었습니다.\n");
}else{
printf("PoP.Name: %s \nPoP.Hakbun : %s \n ", stack[*top].name, stack[*top].hakbun);
printf("Complete. \n");
}
return stack[(*top)--];
}
void display(int top,element *item)
{
int i=0;
if(top == -1){
printf("NULL\n");
}else{
for(i=top; i>-1; i--){
printf("display.Name : %s display.Hakbun : %s \n", stack[i].name, stack[i].hakbun);
}
}
}