头文件:
#ifndef linkqueue_h
#define linkqueue_h
struct Node
{
int data;
Node * next;
};
class linkqueue
{
public:
linkqueue();
void input(int x);
void gettop();
void output();
void empty();
private:
Node *front,*rear;
};
#endif
函数定义;
#include"头文件.h"
#include<iostream>
using namespace std;
linkqueue::linkqueue()
{
Node * s=NULL;
s=new Node;
s->next=NULL;
front=rear=s;
}
void linkqueue::input(int x)
{
Node *s=NULL;
s=new Node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
void linkqueue::gettop()
{
if(front!=rear)
cout<<"队头数据是:"<<front->next->data<<endl;
}
void linkqueue::output()
{
if(rear==front)throw"下溢";
Node *s=NULL;
int x;
x=front->next->data;
s=front->next;
front->next=s->next;
if(s->next==NULL) rear=front;
delete s;
cout<<"出队的数据是:"<<x<<endl;
}
void linkqueue::empty()
{
if(rear==front)
cout<<"队为空"<<endl;
else
cout<<"队非空"<<endl;
}
main函数;
#include"头文件.h"
#include<iostream>
using namespace std;
void main()
{
linkqueue L;
int x[100];
int b;
cout<<"请输入要入队的数据个数:";
cin>>b;
cout<<"请输入要入队的数据:";
for(int a=0;a<b;a++)
{
cin>>x[a];
L.input(x[a]);
}
L.gettop();
y: int c;
cout<<"请输入要出队的数据个数:";
cin>>c;
if(c>b)
{
cout<<"出队的数据个数大于队现有的个数,请重新输入";
goto y;
}
else
{
for(int d=0;d<c;d++)
L.output();
}
}