본문 바로가기

데이터구조/소스코드

Queue 구현코드 (큐)

반응형
SMALL
  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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>

#define SZ 5

int queue[5];
int front = 0;
int rear = 0;

int isEmpty()
{
	return (front == rear);
}

int isFull()
{
	return ((rear + 1) % SZ == front);
}

// put the data d into the queue
// only if there is room.
void enque(int d)
{
	if (isFull() == 1)
	{
		return;
	}

	queue[rear] = d;
	// consider that the queue is circular,,
	rear = (rear + 1) % SZ;

	return;
}

// take out one item from the queue
// returns -999 if the queue is empty.
int deque()
{
	if (isEmpty() == 1)
	{
		return -999;
	}

	{
		int temp = queue[front];
		front = (front + 1) % SZ;
		return temp;
	}

}


int main(void)
{

	// test case for the full queue
	for (int i = 0; i <10; i++)
	{
		enque(i);
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d\n", deque());
	}

#if 0
	printf("%d\n", deque());
#endif

#if 0
	enque(3);
	enque(4);
	enque(5);
	printf("%d\n", deque());
	printf("%d\n", deque());
	printf("%d\n", deque());
#endif
	return 0;
}




#if 0

#include <stdio.h>

#define SZ 5

int stack[5];
int top = -1;

// check the stack is empty
// it returns 1 if so,
//    returns 0 otherwise
int isEmpty()
{
	return (top == -1);
}

// check the stack is full
// it returns 1 if so,
//    returns 0 otherwise
int isFull()
{
	// (SZ-1) is the last index of the stack
	return (top == (SZ - 1));
}

// put the data d into the stack
// if the stack is full, do nothing, just return
void push(int d)
{
	if (isFull() == 1)
	{
		return;
	}
	top++;  // because the initial value of top is -1
	stack[top] = d;

	// stack[++top] = d; the above two lines can be 
	//                   written in one line.

	return;
}

// take out one item from the stack
// returns -999 if the stack is empty.
int pop()
{
	if (isEmpty() == 1)
	{
		return -999;  // -999 means the stack is empty.
	}

	top--;
	return stack[top + 1];

	/* the above two lines mean the following.
	{
		int temp = stack[top];
		top--;
		return temp;
	}
	*/

}
int main(void)
{
#if 0
	// test case forf the full stack
	for (int i = 0; i <10; i++)
	{
		push(i);
	}
	printf("%d\n", pop());
#endif
#if 0
	// test case for the empty stack
	printf("%d \n", pop());
#endif
#if 0
	push(1);
	push(2);
	push(3);
	for (int i = 0; i < 3; i++)
	{
		printf("%d\n", pop());
	}
#endif
	return 0;
}

#endif
반응형
LIST

'데이터구조 > 소스코드' 카테고리의 다른 글

OJ 1132번 해답  (0) 2016.04.11
Online Judge 1117 문제의 해답  (0) 2016.04.04
Stack 구현 소스  (0) 2016.04.04
Doubly Linked List (DLL) 소스코드  (0) 2016.03.28
Singly Linked List (SLL) 소스코드 (Source code)  (0) 2016.03.24