스택입니다.
찰스
질문 제목 : 스택입니다 자세하게 설명좀 부탁드려요질문 요약 :거의 모르는 상태입니다.. 주석으로 자세히좀 설명 부탁드려요
소스만 보고 있으면 뭔지 정말 하나도 모르겠네요
공부는 해야되는데 답답해서 이렇게 통째로 올립니다.질문 내용 :
#include stdio.h
#include stdlib.h
#define MAX_STACK_SIZE 10 /* 스택의 최대 크기 */
typedef struct {
int key;
} element;
element stack[MAX_STACK_SIZE];
int top = -1;
bool isEmpty();
bool isFull();
void push(element item);
element pop();
void printstack(); //0부터 top까지 출력
int main() {
element item;
item.key = 10;
push(item);
printstack();
item.key = 20;
push(item);
printstack();
item.key = 30;
push(item);
printstack();
item.key = 40;
push(item);
printstack();
item = pop();
printf(\nItem %d is pop\n, item.key);
printstack();
item = pop();
printf(\nItem %d is pop\n, item.key);
printstack();
item = pop();
printf(\nItem %d is pop\n, item.key);
printstack();
item = pop();
printf(\nItem %d is pop\n, item.key);
printstack();
item = pop();
printf(\nItem %d is pop\n, item.key);
printstack();
return 1;
}
void push(element item) {
if(isFull())
{
exit(0);
}
stack[++top]=item;
}
element pop(){
element temp;
if(isEmpty())
{
exit(0);
}
temp = stack[top];
top--;
return temp;
}
bool isFull(){
if(top = MAX_STACK_SIZE-1)
{
printf(stack overflow\n);
return true;
}
else
return false;
}
bool isEmpty()
{
if(top = -1)
{
printf(stack is empty);
return true;
}
else
return false;
}
void printstack()
{
//if(isEmpty()) {
//}
printf(\n);
for(int i=0; i=top; i++)
printf(%d - , stack[i].key);
}