编程:循环队列的入队、出队(要有main函数)

2024-12-17 09:30:58
推荐回答(1个)
回答1:

#include
using namespace std;
template
struct Node
{
T data;
struct Node *next;
};
template
class cycle_queue
{
public:
Node *head;//队列头,不保存数据
Node *end;//队列尾,不保存数据
unsigned int size;
cycle_queue()
{
head=new Node();
end=new Node();
size=0;
}
void push_back(DATA d)//入队列
{
Node *tmp;
tmp=new Node;
tmp->data=d;
if(size==0)
{
head->next=tmp;
end->next=tmp;
tmp->next=tmp;
}
else
{
end->next->next=tmp;
end->next=tmp;
tmp->next=head->next;
}
++size;
}
DATA front()//取队头元素,不负责检查是否为空
{
DATA re;
if(size!=0)
re=head->next->data;
return re;
}
void pop()//队头元素出列,不负责检查是否为空
{

if(size!=0)
{
Node *tmp;
tmp=head->next;
head->next=head->next->next;
end->next->next=head->next;
delete tmp;
--size;
}
}
bool empty()//队列判空
{return size==0;}
};
int main()
{
int a[10]={10,9,8,7,6,5,4,3,2,1};
class cycle_queue cq;
short i;
for(i=0;i!=10;++i)
cq.push_back(a[i]);
cout<<"**************"< while(!cq.empty())
{
cout< cq.pop();
}
cin>>i;
return 0;
}