1 #include"../utili.h"
2
3 typedef struct Queue
4 {
5 ElemType *base;
6 int front;
7 int rear;
8 }Queue;
9
10 void init_queue(Queue *Q);//初始化队列
11 void enqueue(Queue *Q,ElemType x);//入队
12 void dequeue(Queue *Q);//出队
13 ElemType gethead(Queue *Q);//获取队头元素
14 bool isfull(Queue *Q);//判满
15 bool isempty(Queue *Q);//判空
16 void show(Queue *Q);
17 //
18 bool isfull(Queue *Q)
19 {
20 if(Q->rear >= DEFAULT_SIZE)
21 return TRUE;
22 return FALSE;
23 }
24
25 bool isempty(Queue *Q)
26 {
27 if(Q->front == Q->rear)
28 return TRUE;
29 return FALSE;
30 }
31
32 void init_queue(Queue *Q)
33 {
34 Q->base = (ElemType*)malloc(sizeof(ElemType)*DEFAULT_SIZE);
35 assert(Q->base != NULL);
36 Q->front = 0;
37 Q->rear = 0;
38 }
39
40 void show(Queue *Q)
41 {
42 for(int i = Q->front;i<Q->rear;++i)
43 {
44 cout<<Q->base[i]<<endl;
45 }
46 }
47
48 void dequeue(Queue *Q)
49 {
50 if(isempty(Q))
51 {
52 cout<<"queue empty"<<endl;
53 }
54 Q->front++;
55 }
56
57 void enqueue(Queue *Q,ElemType x)
58 {
59
60 if(isfull(Q))
61 {
62 cout<<"queue full"<<endl;
63 }
64 Q->base[Q->rear++] = x;
65 }
66 ElemType gethead(Queue *Q)
67 {
68 if(isempty(Q))
69 {
70 cout<<"queue empty,no head"<<endl;
71 return;
72 }
73 return Q->base[Q->front];
74 }
39
40 void show(Queue *Q)
41 {
42 for(int i = Q->front;i<Q->rear;++i)
43 {
44 cout<<Q->base[i]<<endl;
45 }
46 }
47
48 void dequeue(Queue *Q)
49 {
50 if(isempty(Q))
51 {
52 cout<<"queue empty"<<endl;
53 }
54 Q->front++;
55 }
56
57 void enqueue(Queue *Q,ElemType x)
58 {
59
60 if(isfull(Q))
61 {
62 cout<<"queue full"<<endl;
63 }
64 Q->base[Q->rear++] = x;
65 }
66 ElemType gethead(Queue *Q)
67 {
68 if(isempty(Q))
69 {
70 cout<<"queue empty,no head"<<endl;
71 return;
72 }
73 return Q->base[Q->front];
74 }
2
3 typedef struct Queue
4 {
5 ElemType *base;
6 int front;
7 int rear;
8 }Queue;
9
10 void init_queue(Queue *Q);//初始化队列
11 void enqueue(Queue *Q,ElemType x);//入队
12 void dequeue(Queue *Q);//出队
13 ElemType gethead(Queue *Q);//获取队头元素
14 bool isfull(Queue *Q);//判满
15 bool isempty(Queue *Q);//判空
16 void show(Queue *Q);
17 //
18 bool isfull(Queue *Q)
19 {
20 if(Q->rear >= DEFAULT_SIZE)
21 return TRUE;
22 return FALSE;
23 }
24
25 bool isempty(Queue *Q)
26 {
27 if(Q->front == Q->rear)
28 return TRUE;
29 return FALSE;
30 }
31
32 void init_queue(Queue *Q)
33 {
34 Q->base = (ElemType*)malloc(sizeof(ElemType)*DEFAULT_SIZE);
35 assert(Q->base != NULL);
36 Q->front = 0;
37 Q->rear = 0;
38 }
39
40 void show(Queue *Q)
41 {
42 for(int i = Q->front;i<Q->rear;++i)
43 {
44 cout<<Q->base[i]<<endl;
45 }
46 }
47
48 void dequeue(Queue *Q)
49 {
50 if(isempty(Q))
51 {
52 cout<<"queue empty"<<endl;
53 }
54 Q->front++;
55 }
56
57 void enqueue(Queue *Q,ElemType x)
58 {
59
60 if(isfull(Q))
61 {
62 cout<<"queue full"<<endl;
63 }
64 Q->base[Q->rear++] = x;
65 }
66 ElemType gethead(Queue *Q)
67 {
68 if(isempty(Q))
69 {
70 cout<<"queue empty,no head"<<endl;
71 return;
72 }
73 return Q->base[Q->front];
74 }
39
40 void show(Queue *Q)
41 {
42 for(int i = Q->front;i<Q->rear;++i)
43 {
44 cout<<Q->base[i]<<endl;
45 }
46 }
47
48 void dequeue(Queue *Q)
49 {
50 if(isempty(Q))
51 {
52 cout<<"queue empty"<<endl;
53 }
54 Q->front++;
55 }
56
57 void enqueue(Queue *Q,ElemType x)
58 {
59
60 if(isfull(Q))
61 {
62 cout<<"queue full"<<endl;
63 }
64 Q->base[Q->rear++] = x;
65 }
66 ElemType gethead(Queue *Q)
67 {
68 if(isempty(Q))
69 {
70 cout<<"queue empty,no head"<<endl;
71 return;
72 }
73 return Q->base[Q->front];
74 }
/
1 #include"../utili.h"
2 #include"queue.h"
3
4 int main()
5 {
6 Queue Q;
7 init_queue(&Q);
8 enqueue(&Q,1);
9 enqueue(&Q,2);
10 enqueue(&Q,3);
11 enqueue(&Q,4);
12 show(&Q);
13 cout<<"==============="<<endl;
14 dequeue(&Q);
15 show(&Q);
16 return 0;
17 }