-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_DynamicArray_VS_SinglyLinkedList.c
236 lines (194 loc) · 5.31 KB
/
Queue_DynamicArray_VS_SinglyLinkedList.c
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define T_NUM 5000 //몇번 PUSH할 것인지
#define MAX_STACK_SIZE 5 //DynamicArray의 최대 스택값
typedef int element;
//===============================================================================
//DynamicRoundArray
typedef struct QueueType
{
int front, rear;
int capacity;
element* array;
}QueueType;
QueueType* CreateQueue_DA();
int isFull_DA(QueueType* Q);
void enQueue_DA(QueueType* Q, element item);
//===============================================================================
//SinglyLinkedList
typedef struct ListNode {
//스택의 노드를 구조체로 정의
element data;
struct ListNode* link;
}ListNode;
typedef struct Queue {
struct ListNode* front; //맨처음 data
struct ListNode* rear; //맨뒤 data
int count;
}Queue;
Queue* creatQueue_SLL();
int isFull_SLL(Queue* Q);
void enQueue_SLL(Queue* Q, element data);
int RandIndexValue(Queue* Q, int randInt); //랜덤한 인덱스의 값을 가져오기위한 함수.
//===============================================================================
int main(void)
{
//Queue에서의 DynamicRoundArray와 SinglyLinkedList의 동작 시간 비교
srand(time(NULL));
int start_time = 0, end_time = 0;
float time = 0.0f;
int randint = 0;
double sum = 0;
printf("Queue_Push %d번 반복\n\n", T_NUM);
//===============================================================================
//DynamicRoundArray
QueueType* myQ_DA = CreateQueue_DA();
//DynamicRoundArray에 PUSH하는 수행 시간 측정
start_time = clock();
for (int i = 0; i < T_NUM; i++)
{
enQueue_DA(myQ_DA, i);
}
end_time = clock();
time = (float)(end_time - start_time) / CLOCKS_PER_SEC;
printf("[DynamicRoundArray_PUSH] duration : %f\n", time);
printf("\n");
//DynamicRoundArray에 Random Access For Read하는 수행 시간 측정
start_time = clock();
for (int i = 0; i < 100; i++)
{
randint = rand() % T_NUM;
//printf("%d -> ", myS_DA->array[randint]);
sum += myQ_DA->array[randint];
}
end_time = clock();
time = (float)(end_time - start_time) / CLOCKS_PER_SEC;
printf("[DynamicRoundArray_Random Access For Read] duration : %f\tSum : %.0lf\n", time, sum); //더한 값은 소수점 없애고 출력.
printf("\n");
//===============================================================================
//===============================================================================
//SinglyLinkedList
Queue* myQ_SLL = creatQueue_SLL();
//SinglyLinkedList에 PUSH하는 수행 시간 측정
start_time = clock();
for (int i = 0; i < T_NUM; i++)
{
enQueue_SLL(myQ_SLL, i);
}
end_time = clock();
time = (float)(end_time - start_time) / CLOCKS_PER_SEC;
printf("[SinglyLinkedList_PUSH] duration : %f\n", time);
printf("\n");
//SinglyLinkedList에 Random Access For Read하는 수행 시간 측정
sum = 0;
start_time = clock();
for (int i = 0; i < 100; i++)
{
randint = rand() % T_NUM;
sum += RandIndexValue(myQ_SLL, randint);
}
end_time = clock();
time = (float)(end_time - start_time) / CLOCKS_PER_SEC;
printf("[SinglyLinkedList_Random Access For Read]\tduration : %f\tSum : %.0lf\n", time, sum); //더한 값은 소수점 없애고 출력.
printf("\n");
return 0;
}
//===============================================================================
//DynamicRoundArray
QueueType* CreateQueue_DA()
{
QueueType* Q;
Q = (QueueType*)malloc(sizeof(QueueType));
if (!Q)
{
//만약 큐가 동적할당에 실패했다면!
return NULL;
}
Q->capacity = MAX_STACK_SIZE; //Queue의 크기
Q->front = -1; //초기값
Q->rear = -1;
Q->array = (element*)malloc(sizeof(element) * Q->capacity);
if (!Q->array)
{
//만약 큐안의 배열이 동적할당에 실패했다면!
return NULL;
}
return Q;
}
int isFull_DA(QueueType* Q)
{
return ((Q->rear + 1) % Q->capacity == Q->front);
}
void enQueue_DA(QueueType* Q, element item)
{
if (isFull_DA(Q))
{
Q->capacity *= 2;
Q->array = (element*)realloc(Q->array, Q->capacity * sizeof(element));
}
Q->rear = (Q->rear + 1) % Q->capacity;
Q->array[Q->rear] = item;
if (Q->front == -1)
{
//만약 첫 데이터일 경우, -1임
//그 경우
Q->front = Q->rear;
}
}
//===============================================================================
//SinglyLinkedList
Queue* creatQueue_SLL()
{
Queue* Q;
//ListNode* temp;
Q = (Queue*)malloc(sizeof(Queue));
if (!Q)
{
//만약 Q가 NULL이면
return NULL;
}
//temp = (ListNode*)malloc(sizeof(ListNode));
Q->front = NULL;
Q->rear = NULL;
Q->count = -1;
return Q;
}
void enQueue_SLL(Queue* Q, element data)
{
ListNode* newNode;
newNode = (ListNode*)malloc(sizeof(ListNode));
if (!newNode)
return;
newNode->data = data;
newNode->link = NULL;
if (Q->rear == NULL)
{
//만약 첫 데이터일경우.. rear과 front모두 newnode를 가리키게
Q->rear = newNode;
Q->front = newNode;
}
else
{
Q->rear->link = newNode; //첫데이터가 아닌경우, rear
Q->rear = newNode;
}
Q->count += 1; //나중에 count의 위치 조절이 필요할 수도 있음.
if (Q->front == NULL)
{
//만약 Q가 비어있을 경우..
Q->front = Q->rear;
}
}
int RandIndexValue(Queue* Q, int randInt)
{
//랜덤한 인덱스의 값을 가져오기위한 함수.
ListNode* q = Q->front;
while (randInt != 0)
{
randInt--;
q = q->link;
}
return q->data;
}