Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 백준
- 개인프로젝트
- 주가예측
- 이웃한 칸
- SNS
- 프로그램
- 자바정리
- Project
- C++
- 코테
- 코딩
- 풀스택프레임워크
- 모의 주식
- 프로그래머스
- 코스닥
- 코딩테스트
- 개발자
- 자바독학
- 사물인터넷
- backend
- 자바
- java정리
- 개발
- Python
- 빅데이터
- 코딩독학
- Reflex
- frontend
- 프로그래밍
- java
Archives
- Today
- Total
Creative Code
main.c(스택) 본문
※main.c파일
#include <stdio.h>
#include "stack.h"
int main(void)
{
Stack s1, s2;
//Stack stacks[10];
s1.tos = 0;
s2.tos = 0;
push(&s1,100); //구조체 자료는 인자를 전달할 때 오버헤드를 줄이기 위해 포인터를 쓴다.
push(&s1,200);
push(&s1,300);
printf("s1 1st pop() : %d\n",pop(&s1));
printf("s1 2nd pop() : %d\n",pop(&s1));
printf("s1 3rd pop() : %d\n",pop(&s1));
pop(&s1);
push(&s2,900);
push(&s2,800);
push(&s2,700);
printf("s2 1st pop() : %d\n",pop(&s2));
printf("s2 2nd pop() : %d\n",pop(&s2));
printf("s2 3rd pop() : %d\n",pop(&s2));
return 0;
}
※stack.h파일
#ifndef STACK_H
#define STACK_H
#define STACKSIZE 100
typedef struct stack {
int array[STACKSIZE];
int tos;
}Stack;
void initStack(Stack *s);
void push(Stack *s, int data);
int pop(Stack *s);
#endif
※stack.c파일
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void initStack(Stack *s)
{
s->tos = 0;
}
void push(Stack *s, int data)
{
if (s->tos == STACKSIZE) {
fprintf(stderr,"STACK IS FULL");
exit(1);
}
s->array[s->tos] = data;
++s->tos;
}
int pop(Stack *s)
{
if (s->tos == 0) {
frpintf(stderr,"STACK IS EMPTY");
exit(2);
}
--s->tos;
return s->array[s->tos];
}
'C Programming' 카테고리의 다른 글
main.c(스택을 malloc으로 크기할당) (0) | 2023.08.25 |
---|---|
variantArray.c(malloc) (0) | 2023.08.25 |
main.c(볼링보드판) (0) | 2023.08.24 |
stringArray.c(문자열배열-이중포인터) (0) | 2023.08.08 |
hello7.c(문자열 배열-포인터) (0) | 2023.08.08 |