일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- html plug-in
- html code
- #다차원포인터
- #1차원배열
- docker example
- #자바상속#자바이즈어#is~a
- #bubbleSort
- hyperledger transaction
- #JAVASCRIPT
- 하이퍼레저패브릭
- #3차원배열
- html video
- 토큰경제
- html object
- #성적관리프로그램
- #android activity
- html youtube
- relative path
- html id
- html multimedia
- git flow
- #2차원배열
- border-box
- html5 new tag
- mac terminal command
- html charset
- #binary
- #C++ 연산자함수오버로딩
- #CallByAddress
- #C++ has~a
- Today
- Total
A sentimental robot
Linear Queue 본문
#include<stdio.h>
int Enqueue(struct Queue*q, int data);
int Dequeue(struct Queu *q, int *data);
struct Queue
{
int data[5];
int front;
int rear;
};
int Enqueue(struct Queue*q, int data){
if( q->rear > 4)
return 0;
q->data[q->rear]=data;
q->rear++;
return 1;
}
int Dequeue(struct Queue *q, int *data){
*data=q->data[q->front];
for(int i=0 ; i<4 ; i++){ // shift 비어있는 앞자리로 한칸씩 이동하기!
q->data[i] = q->data[i+1];
}
q->rear--;
return 1;
}
void main()
{
struct Queue q={0};
int data;
Enqueue(&q,10);
Enqueue(&q,20);
Enqueue(&q,30);
Enqueue(&q,40);
Enqueue(&q,50);
Enqueue(&q,90);
Dequeue(&q,&data); printf("%d\n",data);
Dequeue(&q,&data); printf("%d\n",data);
Enqueue(&q,60);
Enqueue(&q,70);
Enqueue(&q,80);
Dequeue(&q,&data); printf("%d\n",data);
Dequeue(&q,&data); printf("%d\n",data);
Dequeue(&q,&data); printf("%d\n",data);
Dequeue(&q,&data); printf("%d\n",data);
Dequeue(&q,&data); printf("%d\n",data);
}
#include<stdio.h>
#define SIZE 5
struct Queue
{
int data[SIZE];
int front;
int rear;
};
int Enqueue(struct Queue* q, int data);
int Dequeue(struct Queue* q, int* data);
void main()
{
struct Queue q = { 0 };
int data;
Enqueue(&q, 10);
Enqueue(&q, 20);
Enqueue(&q, 30);
Enqueue(&q, 40);
Enqueue(&q, 50);
Enqueue(&q, 90); // 삽입 안됨
Dequeue(&q, &data); printf("%d\n", data); // 10
Dequeue(&q, &data); printf("%d\n", data); // 20
Enqueue(&q, 60);
Enqueue(&q, 70);
Enqueue(&q, 80); // 삽입 안됨
Dequeue(&q, &data); printf("%d\n", data); // 30
Dequeue(&q, &data); printf("%d\n", data); // 40
Dequeue(&q, &data); printf("%d\n", data); // 50
Dequeue(&q, &data); printf("%d\n", data); // 60
Dequeue(&q, &data); printf("%d\n", data); // 70
}
int Enqueue(struct Queue* q, int data)
{
if (q->rear == SIZE && q->front == 0) // Full 상태
{
return 0;
}
if ((q->rear == SIZE) && (q->front != 0)) // shift
{
for (int c = 0; c < SIZE - q->front; c++)
{
q->data[c] = q->data[q->front + c];
}
q->rear = SIZE - q->front;
q->front = 0;
}
q->data[q->rear++] = data;
return 1;
}
int Dequeue(struct Queue* q, int* data)
{
if (q->front == q->rear)
{
return 0;
}
*data = q->data[q->front++];
if (q->front == SIZE)
{
q->front = 0;
q->rear = 0;
}
return 1;
}
'Data Structure' 카테고리의 다른 글
Map에 대하여.. (0) | 2018.01.03 |
---|---|
Stack (0) | 2018.01.03 |
Babygin (0) | 2018.01.03 |
Baseball Game(computer vs user) (0) | 2018.01.03 |
Single LinkedList Exercise2 (0) | 2018.01.03 |