//定义结点
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode,*QNodeptr;
//该结构体含有两个结点指针变量
typedef struct{
QNodeptr front;//队头指针
QNodeptr rear;//队尾指针
}LinkQueue;
//初始化循环链式队列
Status Init_LQueue(LinkQueue *L)
{
L->rear=L->front=(QNodeptr)malloc(sizeof(QNode));//两个指针都指向头结点
if(!L->front)
return ERROR;
L->front->next = L->front;
return OK;
}
//销毁
Status Destroy_LQueue(LinkQueue *L)
{
QNodeptr p,q;
p = L->front->next;
while(p!=L->front)
{
q = p;
p = p->next;
free(q);
}
free(p);
return OK;
}
Status Clear_LQueue(LinkQueue *L)
{
QNodeptr p,q;
p = L->front->next;
while(p!=L->front)
{
q = p;
p = p->next;
free(q);
}
L->rear = L->front;
L->front->next = L->front;
return OK;
}
//判空
Status Empty_LQueue(LinkQueue L)
{
if(L.front->next==L.front)
return true;
else
return false;
}
//输出长度
int Length_LQueue(LinkQueue L)
{
int count=0;
QNodeptr p=L.front;
while(p!=L.rear)
{
++count;
p=p->next;
}
return count;
}
Status GetHead_LQueue(LinkQueue L,ElemType *e)
{
if(Empty_LQueue(L))
return ERROR;
else
*e = L.front->next->data;
return OK;
}
//入队
Status En_LQueue(LinkQueue *L,ElemType e)
{
QNodeptr s;
s=(QNodeptr)malloc(sizeof(QNode));
if(!s)
return ERROR;
s->data = e;
s->next = L->front;
L->rear->next = s;
L->rear = s;//rear指向最后一个元素
return OK;
}
//出队
Status De_LQueue(LinkQueue *L,ElemType *e)
{
if(Empty_LQueue(*L))
return ERROR;
else
{
*e = L->front->next->data;
L->front->next=L->front->next->next;
}
return 0;
}
void visit(ElemType c)
{
printf("%d ",c);
}
//输出所有节点数据
Status Out_LQueue(LinkQueue L)
{
QNodeptr p=L.front->next;
printf("循环链队列内容: ");
while(p!=L.front)
{
visit(p->data);
p=p->next;
}
printf("\n\n");
return 0;
}
循环链式队列
最新推荐文章于 2023-11-10 12:00:02 发布