数据结构--队列之C数组实现

队列是一种限定操作的线性表,它只能在表的一段插入,另外一段取出.所以也称为先进先出数据结构(FIFO---First In First Out)


C代码如下:

 1 #include<stdio.h>
 2 #define maxsize 5
 3 
 4 typedef int ElemType;
 5 
 6 typedef struct queue
 7 {
 8     int head;
 9     int tail;
10     ElemType Data[maxsize];
11 }Queue;
12 
13 void InitQueue(Queue *Q)
14 {
15     Q->tail=0;
16     Q->head=0;
17 }
18 
19 void EnQueue(Queue *Q)
20 {
21     int value;
22     int i;
23     printf("Input Queue Value:\n");
24     scanf("%d",&value);
25     Q->head++;
26     int len=Q->head-Q->tail;
27     if(len<maxsize)
28     {
29         for(i=len-1;i>=0;i--)
30             Q->Data[i+1]=Q->Data[i];
31     }
32     Q->Data[Q->tail]=value;
33     printf("\n");
34 }
35 
36 void DeQueue(Queue *Q)
37 {
38     int len=Q->head-Q->tail-1;
39     Q->head=Q->head-1;
40     if(len<=maxsize)
41     {
42         printf("Out put Value:\n");
43         printf("%d ",Q->Data[len]);
44     }
45 
46     printf("\n");
47 }
48 
49 void IsEmpty(Queue *Q)
50 {
51     if(Q->head==0&&Q->tail==0)
52         printf("Queue is empty.\n");
53     else
54         printf("Queue is not empet.\n ");
55 
56         printf("\n");
57 }
58 
59 void IsFull(Queue *Q)
60 {
61     if(Q->head-Q->tail>=maxsize)
62         printf("Queue is Full.\n");
63     else
64         printf("Queue is not Full.\n");
65 
66         printf("\n");
67 }
68 
69 void main()
70 {
71     Queue Q;
72     InitQueue(&Q);
73     EnQueue(&Q);
74     EnQueue(&Q);
75     EnQueue(&Q);
76     EnQueue(&Q);
77     EnQueue(&Q);
78     IsEmpty(&Q);
79     IsFull(&Q);
80 
81     DeQueue(&Q);
82     DeQueue(&Q);
83     DeQueue(&Q);
84     DeQueue(&Q);
85     DeQueue(&Q);
86     IsEmpty(&Q);
87     IsFull(&Q);
88 }

 

结果图:


 

posted @ 2014-07-29 14:17  vpoet  阅读(186)  评论(0编辑  收藏  举报