队列

来源:互联网 发布:恒生数据 编辑:程序博客网 时间:2024/05/20 06:30
//顺序循环队列
//本程序队列模型规定:对头指针指向队头所在元素,队尾指针指向队尾元素的后一个元素
#include "common.h"


void creat_squeue(squeuelink *sq_head)
{
*sq_head = (squeuelink)malloc(sizeof(squeue));
(*sq_head)->front = (*sq_head)->rear = 0;
}
void clear_squeue(squeuelink sq_head)
{
sq_head->front = sq_head->rear = 0;
}
int empty_squeue(squeuelink sq_head)
{
return sq_head->front == sq_head->rear ? TRUE : FALSE;
}
int full_squeue(squeuelink sq_head)
{
return sq_head->front == (sq_head->rear+1)%MAXSIZE ? TRUE : FALSE;
}
void in_squeue(squeuelink sq_head, datatype x)
{
if(full_squeue(sq_head))
{
printf("squeue is full!\n");
return;
}
sq_head->data[sq_head->rear] = x;
sq_head->rear = (sq_head->rear+1) % MAXSIZE;
}
void out_squeue(squeuelink sq_head, datatype *ret)
{
if(empty_squeue(sq_head))
{
printf("squeue is empty!\n");
return;
}
*ret = sq_head->data[sq_head->front];
sq_head->front = (sq_head->front+1) % MAXSIZE;
}
int get_head_squeue(squeuelink sq_head, datatype *ret)
{
if(empty_squeue(sq_head))
{
printf("squeue is empty!\n");
return FALSE;
}
*ret = sq_head->data[sq_head->front];
return TRUE;
}
void display_squeue(squeuelink sq_head)
{
int i = sq_head->front;
for(i; i != (sq_head->rear); i = (i+1) % MAXSIZE)
{
printf("%d,",sq_head->data[i]);
}
putchar('\n');

}


//链表实现

//程序中的队列带头节点


#include "common.h"


void creat_cqueue(cqueue *cq)
{
cq->front = cq->rear = malloc(sizeof(cqueuenode));
cq->front->next = NULL;
}
void clear_cqueue(cqueue *cq)
{
cq->rear = cq->front;
cq->front->next = NULL;
}
int empty_cqueue(cqueue *cq)
{
return cq->front == cq->rear ? TRUE : FALSE;
}
void in_cqueue(cqueue *cq, datatype x)
{
cqueuelink q = (cqueuelink)malloc(sizeof(cqueuenode));
q->data = x;
q->next = NULL;

cq->rear->next = q;
cq->rear = q;
}
int de_cqueue(cqueue *cq, datatype *ret)
{
cqueuelink q = NULL;
if(empty_cqueue(cq))
{
printf("cqueue is empty!\n");
return FALSE;
}
q = cq->front->next;
*ret = q->data;
cq->front->next = q->next;
free(q);
q = NULL;
if(cq->front->next == NULL)
cq->rear = cq->front;
return TRUE;
}
int get_head_cqueue(cqueue *cq, datatype *ret)
{
if(empty_cqueue(cq))
{
printf("cqueue is empty!\n");
return FALSE;
}
*ret = cq->front->next->data;
return TRUE;
}
void display_cqueue(cqueue *cq)
{
cqueuelink p = cq->front->next;
while(p)
{
printf("%d,",p->data);
p = p->next;
}
putchar('\n');
}

原创粉丝点击