728x90
반응형
스택(stack)
- 데이터를 쌓아놓은 더미.
- A collection of elements that are inserted and removed according to the last-in first-out(LIFO) principle.
- 마지막으로 들어온 요소가 가장 먼저 제거됩니다.
- Input과 Output은 stack의 가장 맨 위에서만 이루어집니다.
- 용어(Terminology):
- Top: The top of stack (default = -1)
- Push: Insert an item on the top.
- Pop: Remove the item on the top.
- 작동(Operation):
- InitStack: Make stack empty.
- IsFull: Check whether stack is full.
- IsEmpty: Check whether stack is empty.
- Peek: Read the item at the top.
- Push: Insert an item at the top.
- Pop: Remove the item at the top.
#define MAT_STACK 100
typedef enum {false, true} bool;
typedef int Data;
typedef struct{
Data item[MAX_STACK];
int top;
}Stack;
void InitStack(Stack *pstack);
bool IsFull(Stack *pstack);
bool IsEmpty(Stack *pstack);
Data Peek(Stack *pstack);
void Push(Stack *pstack, Data item);
void Pop(Stack *pstack);
void InitStack(Stack *pstack){
pstack->top = -1;
}
bool IsFull(Stack *pstack){
return pstack->top == MAX_STACK - 1;
}
bool IsEmpty(Stack *pstack){
return pstack->top == -1;
}
Data Peek(Stack *pstack){
if(IsEmpty(pstack))
exit(1);
return pstack->items[pstack->top];
}
void Push(Stack *pstack, Data item){
if(IsFull(pstack))
exit(1);
pstack->items[++(pstack->top)] = item;
}
void Pop(Stack *pstack){
if(IsEmpty(pstack))
exit(1)
--(pstack->top);
}
728x90
반응형
'Computer Science > Data Structure' 카테고리의 다른 글
[Data Structure] Graph (0) | 2023.12.26 |
---|---|
[Data Structure] 리스트(List) (0) | 2023.12.26 |
[Data Structure] 큐(Queue) (0) | 2023.12.26 |
[Data Structure] 자료구조와 알고리즘 (0) | 2023.12.26 |