@@ -10,9 +10,66 @@ Queue *queueCreate() {
1010 Queue * q = malloc (sizeof (Queue ));
1111 assert (q );
1212 q -> theSize = 0 ;
13- q -> capacity = 0 ;
14- q -> dataStore
13+ q -> capacity = DEFAULT_QUEUE_SIZE ;
14+ q -> dataStore = malloc ( sizeof ( void * ) * DEFAULT_QUEUE_SIZE );
1515
16+ q -> enqueue = queueEnqueue ;
17+ q -> dequeue = queueDequeue ;
18+ q -> size = queueSize ;
19+ q -> isEmpty = queueIsEmpty ;
20+ q -> font = queueFont ;
21+ q -> end = queueEnd ;
22+ q -> destory = queueDestory ;
1623}
1724
25+ void queueShouldRealloc (Queue * q ) {
26+ if (q -> theSize == q -> capacity ) {
27+ q -> capacity = q -> capacity * 2 ;
28+ q -> dataStore = realloc (q -> dataStore , sizeof (void * ) * q -> capacity );
29+ }
30+ }
31+
32+ void queueEnqueue (Queue * q , void * element ) {
33+ queueShouldRealloc (q );
34+ q -> dataStore [q -> theSize ++ ] = element ;
35+ }
36+
37+ void * queueDequeue (Queue * q ) {
38+ if (q -> theSize == 0 ) return NULL ;
39+ void * d = q -> dataStore [0 ];
40+ if (q -> theSize > 1 ) {
41+ int i ;
42+ for (i = 0 ; i < q -> theSize - 1 ; i ++ ) {
43+ q -> dataStore [i ] = q -> dataStore [i + 1 ];
44+ }
45+ }
46+ q -> theSize -- ;
47+ return d ;
48+ }
49+
50+ int queueSize (Queue * q ) {
51+ return q -> theSize ;
52+ }
53+
54+ bool queueIsEmpty (Queue * q ) {
55+ return q -> theSize == 0 ;
56+ }
1857
58+ void * queueFont (Queue * q ) {
59+ if (q -> theSize == 0 ) return NULL ;
60+ return q -> dataStore [0 ];
61+ }
62+
63+ void * queueEnd (Queue * q ) {
64+ if (q -> theSize == 0 ) return NULL ;
65+ return q -> dataStore [q -> theSize - 1 ];
66+ }
67+
68+ void queueClear (Queue * q ) {
69+ q -> theSize = 0 ;
70+ }
71+
72+ void queueDestory (Queue * q ) {
73+ free (q -> dataStore );
74+ free (q );
75+ }
0 commit comments