顺序队列【数据结构-朱战立】
1 //这是一个循环顺序队列的判空、压栈、出栈、取栈顶等操作 2 #include <stdio.h> 3 4 #define MaxSize 100 5 typedef int bool; 6 #define T 1 7 #define F 0 8 9 typedef int DataType; 10 typedef struct queue{ 11 DataType data[MaxSize]; 12 int rear; 13 int front; 14 int count; 15 }SeqQueue; 16 17 //队列初始化 18 void SeqQueueInit(SeqQueue *queue) 19 { 20 queue->count = 0; 21 queue->front = 0; 22 queue->rear = 0; 23 } 24 25 //判断队列非空否 26 bool SeqQueueNoEmpty(SeqQueue queue) 27 { 28 if (queue.count!=0) //队列不为空 29 { 30 return 1; 31 } 32 else //队列为空 33 { 34 return 0; 35 } 36 } 37 38 //进入队列 39 int SeqQueuePush(SeqQueue *queue, DataType x) 40 { 41 if (queue->count>=MaxSize) 42 { 43 return 0; 44 } 45 queue->data[queue->rear]=x; 46 queue->rear = (queue->rear + 1) % MaxSize; 47 queue->count++; 48 return 1; 49 } 50 51 //出队列 52 int SeqQueuePop(SeqQueue *queue, DataType *x) 53 { 54 if (queue->count==0) 55 { 56 return 0; 57 } 58 *x = queue->data[queue->front]; 59 queue->front = (queue->front + 1) % MaxSize; 60 queue->count--; 61 return 1; 62 } 63 64 //取出队头 65 int SeqQueueTop(SeqQueue queue) 66 { 67 if (queue.count==0) 68 { 69 return 0; 70 } 71 return queue.data[queue.front]; 72 } 73 74 //主函数 75 int main() 76 { 77 SeqQueue queue; 78 int x, i; 79 80 SeqQueueInit(&queue); 81 for ( i = 0; i < 15; i++) 82 { 83 if ((SeqQueuePush(&queue, i)) == 0) 84 { 85 printf("1error"); 86 getchar(); 87 return; 88 } 89 } 90 printf("队头:%d\n", SeqQueueTop(queue)); 91 while (SeqQueueNoEmpty(queue)!=0) 92 { 93 if ((SeqQueuePop(&queue, &x)) == 0) 94 { 95 printf("2error"); 96 getchar(); 97 return; 98 } 99 else 100 { 101 printf("%d ", x); 102 } 103 } 104 getchar(); 105 return 0; 106 }