目录
队列概念
队列是限制在两端进行插入操作和删除操作的线性表,允许进行存入操作的一端称为“队尾”,允许进行删除操作的一端称为“队头”。当线性表中没有元素时,称为“空队”。特点 :先进先出(FIFO)。
队列的特征
特殊的线性表,先进先出(FIFO)。
1)数据:
对于非空的队列,表头没有直接前驱,表尾没有直接后继,其它有且仅有一个直接前驱和一个直接后继。
2)操作:
只允许在表尾插入数据,在表头删除数据。
顺序队列结构
循环顺序队列
规定:front指向队头元素的位置; rear指向队尾元素的下一个位置。
在队列操作过程中,为了提高效率,以调整指针代替队列元素的移动,并将数组作为循环队列的操作空间。
为区别空队和满队,满队元素个数比数组元素个数少一个。
typedef int data_t;
#define N 126
typedef struct{
data_t data[N];
int front;
int rear;
}sequeue,*sqe;
创建队列
sqe queue_create(){
sqe sq;
if((sq = (sqe)malloc(sizeof(sequeue))) == NULL){
printf("malloc failed\n");
return NULL;
}
memset(sq->data,0,sizeof(sq->data));
sq->front = sq->rear = 0;
return sq;
}
入队
int enqueue(sqe sq,data_t value){
if(sq == NULL){
printf("sq is NULL\n");
return -1;
}
if((sq->rear+1)%N == sq->front){
printf("sq is full\n");
return -1;
}
sq->data[sq->rear] = value;
sq->rear = (sq->rear+1) % N;
return 0;
}
出队
data_t dequeue(sqe sq){
if(sq == NULL){
printf("sq is NULL\n");
return -1;
}
data_t t;
t= sq->data[sq->front];
sq->front = (sq->front+1)%N;
return t;
}
判断空
int queue_empty(sqe sq){
if(sq == NULL){
printf("sq is NULL\n");
return -1;
}
return ((sq->rear == sq->front)?1:0);
}
清空队列
int queue_clear(sqe sq){
if(sq == NULL){
printf("sq is NULL\n");
return 0;
}
sq->rear = sq->front =0;
return 0;
}
判断满
int queue_full(sqe sq){
if(sq == NULL){
printf("sq is NULL\n");
return -1;
}
return (((sq->rear+1)%N== sq->front)?1:0);
}
删除队列
sqe queue_free(sqe sq){
if(sq == NULL){
printf("sq is NULL\n");
return NULL;
}
free(sq);
sq = NULL;
return NULL;
}
链式队列结构
插入操作在队尾进行,删除操作在队头进行,由队头指针和队尾指针控制队列的操作。
typedef int datatype;
typedef struct node{
datatype data;/*数据域*/
struct node *next;/*链接指针域*/
}listnode, *linklist;
typedef struct{ /*链队列指针*/
linklist front;
linklist rear;
}linkqueue;
创建队列
linkqueue * queue_create(){
linkqueue *lq;
//判断队列数据域是否创建成功
if((lq = (linkqueue *)malloc(sizeof(linkqueue)))==NULL){
printf("malloc linkqueue failed\n");
return NULL;
}
//判断队列指针是否成功
lq->front = lq->rear = (linklist)malloc(sizeof(listnode));
if(lq->front == NULL){
printf("malloc linknode failed\n");
return NULL;
}
lq->front->data = 0;
lq->front->next = NULL;
return lq;
}
入队
int enqueue(linkqueue *lq,datatype x){
if(lq == NULL){
printf("lq is NULL\n");
return -1;
}
linklist p;
if((p = (linklist)malloc(sizeof(listnode)))==NULL){
printf("listnode malloc failed\n");
return -1;
}
p->data = x;
p->next = NULL;
lq->rear->next = p;
lq->rear = p;
return 0;
}
出队
datatype dequeue(linkqueue *lq){
linklist q;
datatype t;
if(lq == NULL){
printf("lq is NULL\n");
return -1;
}
if(lq->front == lq->rear){
printf("lq is empty\n");
return -1;
}
q = lq->front;
lq->front = lq->front->next;
t = lq->front->data;
lq->front->data = 0;
free(q);
q = NULL;
return t;
}
判断空
int queue_empty(linkqueue *lq){
if(lq == NULL){
printf("lq is NULL\n");
return -1;
}
return ((lq->front == lq->rear)?1:0);
}
输出队列元素
int queue_show(linkqueue *lq){
linklist p = lq->front;
while(p->next!=NULL){
p = p->next;
printf("%d ",p->data);
}
printf("\n");
return 0;
}
删除队列
linkqueue * queue_free(linkqueue *lq){
linklist p = lq->front;
while(lq->front->next!=NULL){
lq->front = lq->front->next;
free(p);
p = lq->front;
}
p = NULL;
printf("lq is NULL\n");
return NULL;
}
清空队列
int queue_clear(linkqueue *lq){
linklist p ;
if(lq == NULL){
printf("lq is NULL\n");
return -1;
}
while(lq->front->next){
p = lq->front;
lq->front = p->next;
free(p);
}
p = NULL;
return 0;
}