数据结构——顺序队列(sequence queue)
作者:互联网
/* sequenceQueue.c */ /* 顺序队列 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAXSIZE 100 /* 顺序循环队列数据结构 */ /* 一个圆圈,front指向队列头,rear指向队列尾 */ /* front -> ... -> rear data[0] -> data[...] -> data[n] */ typedef struct { int data[MAXSIZE]; int rear, front; int num; /* 队列元素的个数 */ } SeqQueue; void interface(void); /* 顺序列表函数声明 */ SeqQueue *initializeSeqQueue(); bool isFullSeqQueue(SeqQueue*); bool isEmptySeqQueue(SeqQueue*); void inSeqQueue(SeqQueue*, int); int outSeqQueue(SeqQueue*); int main(){ SeqQueue *sq = initializeSeqQueue(); int flag, number; interface(); for(;;){ printf("Command: "); scanf("%d", &flag); switch(flag){ case 0: printf("Bye!\n"); return 0; break; case 1: if(isFullSeqQueue(sq)) printf("Queue is full!\n"); else{ printf("Enter number: "); scanf("%d", &number); inSeqQueue(sq, number); } break; case 2: if(isEmptySeqQueue(sq)) printf("Queue is empty!\n"); else{ printf("value: %d\n", outSeqQueue(sq)); } break; } } return 0; } void interface(void){ puts("+********************+"); puts("+ 0, quit 退出 +"); puts("+ 1, in 入队 +"); puts("+ 2, out 出队 +"); puts("+********************+"); } /* 顺序列表函数实现 */ /* 初始化顺序列表,列表为空,front和read都为-1,num为0 */ SeqQueue *initializeSeqQueue(){ SeqQueue *sq = (SeqQueue*)malloc(sizeof(SeqQueue)); sq->front = sq->rear = -1; sq->num = 0; return sq; } /* 判断队列是否为空 */ bool isEmptySeqQueue(SeqQueue *sq){ if(sq->num==0) return true; return false; } /* 判断队列是否已满 */ bool isFullSeqQueue(SeqQueue *sq){ if(sq->num==MAXSIZE) return true; return false; } /* 入队 */ void inSeqQueue(SeqQueue *sq, int number){ /* 循环队列,取余MAXSIZE */ sq->rear = sq->rear+1 % MAXSIZE; sq->data[sq->rear] = number; sq->num++; } /* 出队 */ int outSeqQueue(SeqQueue *sq){ sq->front = sq->front+1 % MAXSIZE; sq->num--; return sq->data[sq->front]; }
标签:return,sequence,int,sq,queue,队列,front,数据结构,SeqQueue 来源: https://www.cnblogs.com/noonjuan/p/11493507.html