1. 스택
- 후입선출(Last-In-First-Out(LIFO)) 원리의 자료구조이다.
- 한 쪽 끝에서만 자료를 넣거나 뺄 수 있는 선형구조로 되어있다.
2. stack 함수
- int stackpop(int *) : 스택에서 top이 가리키는 요소를 꺼내는 함수
- void stackpush(int *,int) : 스택에 요소를 삽입하는 함수
- int stackempty(int) : 스택이 비어있는지 검사 하는 함수
3. 소스
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- node *pre;
- }node;
- node * top = NULL;
- int stackisempty(node*);
- int stackpop();
- void stackpush(int);
- int main(void)
- {
- stackpush(123);
- stackpush(222);
- stackpush(333);
- stackpop();
- stackpop();
- stackpop();
- stackpop();
- return 0;
- }
- void stackpush(int data)
- {
- if(stackisempty == NULL) // 스택이 비어있고 첫 요소를 삽입
- {
- item->pre = NULL;
- item->data = data;
- top = item;
- }
- else //스택의 처음 요소 이후의 데이터를 삽입
- {
- item->data = data;
- item->pre = top;
- top = item;
- }
- }
- int stackpop()
- {
- node *freenode = NULL;
- int data = 0;
- if(stackisempty(top)) //스택이 비어있으면 리턴
- return 0;
- else // 스택에 요소가 있을 때
- {
- data = top->data;
- if(stackisempty(top) == 0)
- {
- freenode = top; // malloc 반환 하기 위한 freenode
- top = top->pre; // 탑의 위치를 내린다
- }
- return data;
- }
- }
- int stackisempty(node *top)
- {
- if(top == NULL)
- {
- return 1;
- }
- else
- return 0;
- }
4. 결과
'c언어(알고리즘)' 카테고리의 다른 글
| 큐(Queues) - 배열로 구현(The implementation with the array) (0) | 2016.11.02 |
|---|---|
| 깊이우선탐색(DFS : Depth First Search) (0) | 2016.11.01 |
| 스택(Stacks) - 배열로 구현(The implementation with the array) (0) | 2016.11.01 |
| 힙 정렬 (Heap-Sort) (0) | 2016.11.01 |
| 퀵 정렬(Quick-Sort) (0) | 2016.11.01 |