循环队列的基本操作
程序员文章站
2022-06-01 18:35:59
...
循环队列:
为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular
Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。
链表结构:
typedef struct lnode
{
ElemType data;
struct lnode *next;
} CLNode,*CList;
循环队列ADT:
void Initial_CL(CList &L); //初始化链表
CList Create_CL(CList &L,int n); //创造有n个结点的循环链表
Status Insert_CL_head(CList &L, ElemType e); //头插法插入结点
Status Insert_CL_tail(CList &L, ElemType e); //尾插法插入结点
int Length_CL(CList L); //求循环链表的长度
bool Empty_CL(CList L); //链表是否为空
Status Delete_CL(CList &L, ElemType data); //链表是否为空
void Destory_CL(CList &L); //销毁循环链表
CLNode *Search_CL(CList L, ElemType e); //查找元素e并返回所在的结点
void Display_CL(CList L); //遍历输出循环链表
void Initial_CL(CList &L)
{
L = new CLNode;
L->next = L;
L->data = 0;
}
bool Empty_CL(CList L)
{
return (L == L->next);
}
int Length_CL(CList L)
{
int len = 0;
CList p = L->next;
while(p != L)
{
p = p->next;
len++;
}
return len;
}
Status Insert_CL_head(CList &L, ElemType e)
{
CList p = new CLNode;
p->data = e;
p->next = L->next;
L->next = p;
return true;
}
Status Insert_CL_tail(CList &L, ElemType e)
{
CList p, q;
for(p = L; p->next != L; p = p->next);
q = new CLNode;
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
Status Delete_CL(CList &L, ElemType data)
{
CLNode *q, *p;
for(p = L; p->next != L && p->next->data != data; p = p->next);
if(p->next == L)
return false;
q = p->next;
p->next = q->next;
delete q;
return true;
}
CList Create_CL(CList &L,int n)
{
int i;
for(i = 1; i <= n; ++i)
{
Insert_CL_tail(L,i);
}
return L;
}
void Destory_CL(CList &L)
{
CLNode *p;
while(L->next != L)
{
p = L->next;
L->next = p->next;
delete p;
}
}
CLNode *Search_CL(CList L, ElemType e)
{
CList p;
for(p = L; p ->next != L ; p = p->next)
{
if(p->data == e)
return p;
}
return NULL;
}
void Display_CL(CList L)
{
CLNode *p;
for(p = L->next; p != L; p = p->next)
cout << p->data<<endl;
}
完整代码:
#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
typedef bool Status;
typedef struct lnode
{
ElemType data;
struct lnode *next;
} CLNode,*CList;
void Initial_CL(CList &L);//初始化链表
CList Create_CL(CList &L,int n);//创造有n个结点的循环链表
Status Insert_CL_head(CList &L, ElemType e); //头插法插入结点
Status Insert_CL_tail(CList &L, ElemType e); //尾插法插入结点
int Length_CL(CList L);//求循环链表的长度
bool Empty_CL(CList L);//链表是否为空
Status Delete_CL(CList &L, ElemType data); //链表是否为空
void Destory_CL(CList &L);//销毁循环链表
CLNode *Search_CL(CList L, ElemType e); //查找元素e并返回所在的结点
void Display_CL(CList L);//遍历输出循环链表
//链表初始化
void Initial_CL(CList &L)
{
L = new CLNode;
L->next = L;
L->data = 0;
}
//链表是否为空
bool Empty_CL(CList L)
{
return (L == L->next);
}
//求循环链表的长度
int Length_CL(CList L)
{
int len = 0;
CList p = L->next;
while(p != L)
{
p = p->next;
len++;
}
return len;
}
//头插法插入链表
Status Insert_CL_head(CList &L, ElemType e)
{
CList p = new CLNode;
p->data = e;
p->next = L->next;
L->next = p;
return true;
}
//尾插法插入链表
Status Insert_CL_tail(CList &L, ElemType e)
{
CList p, q;
for(p = L; p->next != L; p = p->next);
q = new CLNode;
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
//删除链表中某个元素
Status Delete_CL(CList &L, ElemType data)
{
CLNode *q, *p;
for(p = L; p->next != L && p->next->data != data; p = p->next);
if(p->next == L)
return false;
q = p->next;
p->next = q->next;
delete q;
return true;
}
CList Create_CL(CList &L,int n)
{
int i;
for(i = 1; i <= n; ++i)
{
Insert_CL_tail(L,i);
}
return L;
}
//销毁循环链表
void Destory_CL(CList &L)
{
CLNode *p;
while(L->next != L)
{
p = L->next;
L->next = p->next;
delete p;
}
}
//查找元素e并返回所在的结点
CLNode *Search_CL(CList L, ElemType e)
{
CList p;
for(p = L; p ->next != L ; p = p->next)
{
if(p->data == e)
return p;
}
return NULL;
}
//遍历输出循环链表
void Display_CL(CList L)
{
CLNode *p;
for(p = L->next; p != L; p = p->next)
cout << p->data<<endl;
}
int main()
mian函数根据自己的需要进行写,在此不再赘述
下一篇: gogland配置说明