A queue data structure

Disclaimer: This article is for the exchange of learning to use, prohibit the use of the techniques crimes, or peril

Circular queue issues

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
1, the static queue must be why the circular queue 
2, queue requires several cycles to determine the parameters
required to determine the two parameters
two parameters have different meanings in different situations
font and rear
meaning of each parameter 3, circular queue
1), initializing the queue
font values are zero and the rear
2), a non-empty queue
font represents the first element of the queue
rear represents a valid last queue element
3), the queue empty
font and rear equal, but not necessarily zero
4 cyclic pseudo algorithm team team included
two steps
1), the value represented by the stored position r
2), the correct wording: r = (r + 1) the length of the array%
error written: r = r + 1
. 5 , the dummy cycle team lists team algorithm
f = (f + 1)% the length of the array
6, how to determine the circular queue is empty
if the front and rear of equal value, i.e., the queue is empty f = r
7, how to determine the circular queue has full
two ways:
1, more than the increase of a table identification parameter
2, less commonly an element with a second embodiment] [
1), if the value of f r and next, it indicates that the queue is full
2), and c indicates the language pseudo algorithm is: IF ((R & lt +. 1)% array length == f) {at full} the else {} dissatisfied


Program realization

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

#include<malloc.h>


typedef struct // define a data type QUEUE
{ int * pBase; int Front; int REAR;




} QUEUE;
large column   data structures of the queue span>
void the init (QUEUE *) ;
BOOL en_queue (QUEUE *, int Val) ; // enqueue
void traverse_queue (QUEUE *) ; // queue traversal
BOOL full_queue (QUEUE *) ; // before the judge into the team is not already full
BOOL out_queue (QUEUE *, int *) ; // a team
BOOL emput_queue (QUEUE *) ; // determine the team's time is empty

int main(void)
{
QUEUE Q;
int val;

init(&Q);

en_queue (& Q, . 1 );
en_queue (& Q, 2 );
en_queue (& Q, . 3 );
en_queue (& Q, . 4 ); IF (out_queue (& Q, & Val)) { the printf ( "dequeued successful teams listed teams elements are:% DN " , Val); } the else { the printf ( " failed dequeued n-" ); } traverse_queue (& Q); return 0 ;














}

void init(QUEUE *pQ)
{
pQ->pBase = (int *)malloc(sizeof(int)*6);
pQ->front = 0;
pQ->rear = 0;

}

bool full_queue(QUEUE *pQ)
{
if((pQ->rear +1)%6 == pQ->front)
return true;
else
return false;
}


bool en_queue (QUEUE PQ *, int val)
{
if(full_queue(pQ))
{
return false;
}
else
{
pQ->pBase[pQ->rear] = val;
pQ->rear = (pQ->rear + 1)%6;
return true;
}

}

void traverse_queue (QUEUE * pQ) // traverse the queue
{
int i = pQ->front;
while(i != pQ->rear)
{
printf("%d ",pQ->pBase[i]);
i = (i+1)%6;
}


}

bool emput_queue(QUEUE *pQ)
{
if(pQ->front==pQ->rear)
{
return true;
}
else
{
return false;
}
}


bool out_queue(QUEUE * pQ, int * pVal)
{
if(emput_queue(pQ))
{
return false;
}
else
{
*pVal = pQ->pBase[pQ->front];
pQ->front = (pQ->front+1)%6;
return true;
}


}
1
2
The success of the team, the team element is 1, 
2, 3

Guess you like

Origin www.cnblogs.com/lijianming180/p/12360818.html