Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- https://coding-factory.tistory.com/641
- SQL
- TCP
- HTTP
- https://firecatlibrary.tistory.com/49?category=874970
- group by
- 정글#정글사관학교#3기#내일#기대#설렘#희망#노력
- mysql
- C언어
- BSD소켓
- web-proxy lab
- strcat
- Error Code: 1055
- DNS
- strcpy
- ip
Archives
- Today
- Total
매일을 설렘으로
[C언어] stack 자료 구조 구현 본문
C를 활용한 stack 자료 구조 구현
전체 구조
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
// stack struct 구성
typedef struct stack {
// arr[SIZE], top
int arr[SIZE];
int top;
} stack;
// init 함수
void init(stack *s)
{
s -> top = -1; // stack에 초기상태 -1, Push해서 data 들어오면 0부터 index시작
}
// is_empty 함수 구성
int is_empty(stack *s)
{
if (s->top == -1)
return 1;
return 0;
}
// is_full 함수 구성
int is_full(stack *s){
if (s->top == SIZE - 1)
return 1;
return 0;
}
// push 기능 구현
int push(stack *s, int value) //*s 포인터 변수와 값을 입력
{
if (is_full(s))
{
printf("stack is full");
exit(1);
}
s->arr[++(s->top)] = value; //다음 인덱스에 value 대입
printf("pushed : %d\n", value);
}
// pop 기능 구현
int pop(stack *s) //*s 포인터 변수을 입력
{
if (is_empty(s))
{
printf("stack is empty");
exit(1);
}
return s->arr[(s->top)--]; //(s->top)값으로 참조 후, s->top값 -= 1
}
// peek 기능함수 구현
int peek(stack *s)
{
if (is_empty(s) == 1)
{
printf("stack is empty");
exit(1);
}
return s->arr[s->top];
}
// main 함수 구현
int main(void)
{
stack s;
init(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
printf("%d\n", peek(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
printf("%d\n", pop(&s));
return 0;
}
'CS > 자료구조' 카테고리의 다른 글
[C언어] Red-Black Tree (0) | 2021.12.09 |
---|---|
[C언어] typedef/enum (0) | 2021.12.08 |
[자료구조] 이진 트리 및 이진 검색 트리 (0) | 2021.11.25 |