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
- 자바독학
- 이웃한 칸
- 코딩
- 개발자
- Python
- 주가예측
- Project
- 개인프로젝트
- 프로그래머스
- java정리
- 자바
- 개발
- 코딩독학
- SNS
- 빅데이터
- 사물인터넷
- backend
- Reflex
- frontend
- 코딩테스트
- 프로그램
- 코테
- 프로그래밍
- 자바정리
- C++
- 코스닥
- java
- 백준
- 풀스택프레임워크
- 모의 주식
Archives
- Today
- Total
Creative Code
main.c(queue malloc) 본문
※main.c파일
#include <stdio.h>
#include "queue.h"
int main(void)
{
Queue s1, s2;
initQueue(&s1,10);
initQueue(&s2,100);
push(&s1,100); //구조체 자료는 인자를 전달할 때 오버헤드를 줄이기 위해 포인터를 쓴다.
push(&s1,200);
printf("s1 1st pop() : %d\n",pop(&s1));
push(&s1,300);
printf("s1 2nd pop() : %d\n",pop(&s1));
printf("s1 3rd pop() : %d\n",pop(&s1));
push(&s2,900);
push(&s2,800);
printf("s2 1st pop() : %d\n",pop(&s2));
push(&s2,700);
printf("s2 2nd pop() : %d\n",pop(&s2));
printf("s2 3rd pop() : %d\n",pop(&s2));
cleanupQueue(&s1);
cleanupQueue(&s2);
return 0;
}
※queue.h파일
#ifndef QUEUE_H
#define QUEUE_H
typedef struct queue {
int *array;
int rear;
int front;
int size;
}Queue;
void initQueue(Queue *s, int size);
void cleanupQueue(Queue *s);
void push(Queue *s, int data);
int pop(Queue *s);
#endif
※queue.c파일
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "queue.h"
void initQueue(Queue *s, int size)
{
s->array = malloc(sizeof(int)*size);
assert(s->array /*!= NULL*/);
s->size = size;
s->rear = 0;
s->front = 0;
}
void cleanupQueue(Queue *s)
{
free(s->array);
}
void push(Queue *s, int data)
{
assert(s->rear != s->size);
s->array[s->rear] = data;
++s->rear;
}
int pop(Queue *s)
{
assert(s->front < s->rear);
int index = s->front;
++s->front;
return s->array[index];
}
'C Programming' 카테고리의 다른 글
main.c(리스트) (0) | 2023.08.28 |
---|---|
main.c(queue memcpy) (0) | 2023.08.25 |
main.c(queue 구조체) (0) | 2023.08.25 |
main.c(queue) (0) | 2023.08.25 |
main.c(스택-메모리함수사용) (0) | 2023.08.25 |