简单队列c语言实现

 1 /*
 2 队列queue:特殊的有序表,插入在表的一端,删除在表的另一端 
 3 特点:先进先出(fifo) 
 4 简单队列:
 5     缺点:在会导致队列(右进左出)一直移动右移,直到队列满。
 6     当队列满时,需要重新移动到最初始位置 才能继续使用。
 7 指针: 后部:rear
 8       前部  front 
 9 队列空:front=rear;前部等于后部时
10  队列满;rear=MAX_QUEUE_SIZE-1
11  如  进 rear->edbca->front 出 
12             【    】rear和front都是相对于栈来说的 
13 数组位置:              值                         
14 3                       4  rear=3                 4    rear=3 
15 2                       3                        3    
16 1                       2                            front=2 
17 0                       1
18 -1=rear=front   入队列: front=-1        出队列: 
19 */ 
20 #include<stdio.h>
21 
22 #define MAX_QUEUE_SIZE 100
23 typedef struct{
24     int key;
25     //other fields
26 }element;
27 
28 element queue[MAX_QUEUE_SIZE];
29 
30 
31 void Addq(int *rear,element item)
32 {
33     if(*rear==MAX_QUEUE_SIZE-1){
34         printf("队列满"); 
35         return ; 
36     }
37     queue[++*rear]=item;
38 }
39 
40 element Deleteq(int *front,int rear)//删除的时候rear不变只有front动所以传道rear的值 
41 {
42     if(*front == rear){
43         printf("队列空");
44     }
45     return queue[++*front];
46 }
47 
48 bool IsFullq(int *rear)
49 {
50     if(*rear==MAX_QUEUE_SIZE-1){
51         return 1; 
52     }
53     else
54         return 0; 
55 }
56 
57 bool IsEmptyq(int *front,int rear)
58 {
59     if(*front == rear){
60         return 1;
61     }
62     return 0;
63 }
64 
65 int main()
66 {
67     element item;
68     
69     int front,rear;
70     int i;
71     
72     rear=front=-1;
73     if(IsEmptyq(&front,rear)){
74         printf("
入队列:
");
75         item.key=0;
76         for(i=0;i<30;i++){
77             if(!IsFullq(&rear)){
78                 Addq(&rear,item);
79                 printf("%d
",item.key++);
80             }
81             else
82                 break;
83         }
84     }
85     printf("
出队列
"); 
86     while(!IsEmptyq(&front,rear)){
87         printf("%d
",Deleteq(&front,rear).key);
88     }
89     printf("
队列为空
"); 
90     
91     return 0;
92 }
93 
94  
原文地址:https://www.cnblogs.com/hysz/p/7147401.html