#include<iostream>
using namespace std;
template<class T>
struct Node{
T data;
Node<T> *next;
};
template<class T>
class Linkqueue{
public:
Linkqueue();
~Linkqueue();
void In(T x);
T Out();
T Getfrist();
bool Isempty()const;
int Length()const;
void Print();
private:
Node<T> *front;
Node<T> *rear;
int size;
};
template<class T>
Linkqueue<T>::Linkqueue():size(0)
{
front=rear=new Node<T>;
front->next=NULL;
}
template<class T>
Linkqueue<T>::~Linkqueue()
{
while(front)
{
rear=front->next;
delete front;
front=rear;
}
}
template<class T>
void Linkqueue<T>::In(T x)
{
Node<T> *s=new Node<T>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
size++;
}
template<class T>
T Linkqueue<T>::Out()
{
if(Isempty())throw"队列为空!";
Node<T> *p;
p=front->next;
front->next=p->next;
if(rear==p)
rear=front;
T x=p->data;
delete p;
size--;
return x;
}
template<class T>
T Linkqueue<T>::Getfrist()
{
if(Isempty())throw"队列为空!";
return front->next->data;}
template<class T>
bool Linkqueue<T>::Isempty()const
{return size==0;}
template<class T>
int Linkqueue<T>::Length()const
{
return size;
}
template<class T>
void Linkqueue<T>::Print()
{
Node<T> *p;
p=front->next;
while(p!=rear)
{
cout<<p->data<<" "<<endl;
p=p->next;
}
}
int main()
{
Linkqueue<int> s;
cout<<"队列为空则返回1,否则返回0:"<<s.Isempty()<<endl;
cout<<"依次插入20~27八个元素:"<<endl;
s.In(20);
s.In(21);
s.In(22);
s.In(23);
s.In(24);
s.In(25);
s.In(26);
s.In(27);
cout<<"此时队列全部元素为:"<<endl;
s.Print();
cout<<"取出队头元素为:"<<s.Out()<<endl;
cout<<"队列元素个数为:"<<s.Length()<<endl;
cout<<"列出此时队列全部元素:"<<endl;
s.Print();
return 0;
}
一、实验目的
1、 熟练掌队列的结构特点,掌握队列的链式存储结构和实现。2、 学会使用栈和队列解决实际问题。
二、实验内容
1、自己确定结点的具体数据类型和问题规模;2、建立一个链队列,实现队列的入队和出队操作。
三、运行结果
四、心得
经过实验二线性表综合实验和实验三关于栈和队列的实现的操作,现在大概建立了对这些知识点的“立体认识”,虽然自己本身就对编程语言学习起来比较迟钝,但现在至少可以意识到这各种线性表之间的联系和相同点和不同点,自己开始有了意识独立去思考相关的操作,尝试经过特意的修改看看最后的运行结果的异同。
综合下来,我发现自己对于C++语言编程,在主函数的设计与实现、函数的声明与调用这几个方面尤为困惑,常常就在这些部分出现编译错误。