循环队列:(调试成功)
#include "iostream.h"
class ssqueue
{ private:
int front,rear; //rear 虚存
int *data;
int size;
public:
ssqueue(int s=10)
{ front=0;rear=0;size=s;
data=new int[size] ;
if(data==NULL)
{ cout<<"assign is fail!"<<endl; }
else
{ cout<<"bulid is suceed !"<<endl; }
}
~ssqueue()
{}
int Insert(const int& item )
{
if(Isempty())
{ this->data[rear++]=item; return 1;}
if(Isfull())
{ cout<<"fail! the squeue is full!"<<endl; return 0;}
data[rear]=item;
rear=(rear+1)%size;
return 1;
}
int Out()
{ int temp;
if(Isempty())
{ cout<<"the squeue is empty ,out fail!"<<endl; return 0; }
temp=data[front];
front=(front+1)%size;
return temp;
}
int getsize()
{ return (size-1);}
int Isempty() //empty retrun 1
{ return rear==front;}
int Isfull() //full return 1
{ return (rear+1)%size==front;}
int getfront()
{ if(Isempty())
{ cout<<"the queue is empty"<<endl; return 0;}
return data[front];}
int getrear()
{ if(Isempty())
{ cout<<"the queue is empty"<<endl;
return 0;
}
return data[rear-1];
}
void print()
{ int temp=front;
if(Isempty())
{ cout<<"the queue is empty"<<endl; return ; }
for(;temp!=rear;temp=(temp+1)%size)
cout<<data[temp]<<" ";
}
int count()
{ int temp=front;
for(int i=0;temp!=rear;i++) temp=(temp+1)%size ;
return i;
}
};
#include "iostream.h"
int main(int argc, char* argv[])
{ int n,flag=200,tag=1; int nu;
// cout <<"please input the numeber of the queue data"<<endl;
// cin>>n;
cout<<"the program begin!"<<endl<<"you can input '0'exit press any key to continue"<<endl;
cin>>tag;
if(tag==0)
return 0;
cout<<" 你是第一次运行这个程序,将为你建立一个循环队列! "<<endl;
cout <<"please input the numeber of the queue data"<<endl;
cin>>n;
int ch;int i; ssqueue q(n+1);
while (flag!=0)
{
cout<<" 0 退出 "<<endl;
cout<<" 1 元素入队 "<<endl;
cout<<" 2 出队 "<<endl;
cout<<" 3 查询对头元素 "<<endl;
cout<<" 4 查询对尾元素 "<<endl;
cout<<" 5 查询对列所有元素 "<<endl;
cout<<" 6 查询对列最大容量 "<<endl;
cout<<" 7 查询现在对列中元素的个数 "<<endl;
cin>>flag;
switch(flag)
{
case 0: break;
case 1:
cout <<"please input the numeber of the queue data"<<endl;
cin>>n;
for (i=0;i<n;i++)
{ cout<<"please input the data"<<endl;
cin>>ch;
q.Insert(ch);
}
cout<<"the front and the rear is/n";
cout<<q.getfront()<<"/t"<<q.getrear()<<endl;
break;
case 2:
cout<<" 请输入要出队元素的个数 /n";
cin>>nu;
for(i=0;i<nu;i++) q.Out();
break;
case 3: cout<<q.getfront()<<endl; break;
case 4: cout<<q.getrear()<<endl; break;
case 5: q.print(); cout<<endl; break;
case 6: cout<<q.getsize()<<endl; break;
case 7: cout<<q.count()<<endl; break;
default: cout<<" 你的输入有误!请重试 "<<endl;
}
}
return 0;
}
循环模板队列:(调试成功)
template<class type> class ssqueue
{ private:
int front,rear; //rear 虚存
type *data;
int size;
public:
ssqueue(int s=10)
{ front=0;rear=0;size=s;
data=new int[size] ;
if(data==NULL)
{ cout<<"assign is fail!"<<endl; }
else
{ cout<<"bulid is suceed !"<<endl; }
}
~ssqueue()
{}
int Insert(const type& item )
{
if(Isempty())
{ this->data[rear++]=item; return 1;}
if(Isfull())
{ cout<<"fail! the squeue is full!"<<endl; return 0;}
data[rear]=item;
rear=(rear+1)%size;
return 1;
}
int Out()
{ int temp;
if(Isempty())
{ cout<<"the squeue is empty ,out fail!"<<endl; return 0; }
temp=data[front];
front=(front+1)%size;
return temp;
}
int getsize()
{ return (size-1);}
int Isempty() //empty retrun 1
{ return rear==front;}
int Isfull() //full return 1
{ return (rear+1)%size==front;}
type getfront()
{ if(Isempty())
{ cout<<"the queue is empty"<<endl; return 0;}
return data[front];}
type getrear()
{ if(Isempty())
{ cout<<"the queue is empty"<<endl;
return 0;
}
return data[rear-1];
}
void print()
{ int temp=front;
if(Isempty())
{ cout<<"the queue is empty"<<endl; return ; }
for(;temp!=rear;temp=(temp+1)%size)
cout<<data[temp]<<" ";
}
int count()
{ int temp=front;
for(int i=0;temp!=rear;i++) temp=(temp+1)%size ;
return i;
}
};
#include"queue1.h"
#include "iostream.h"
#include "stdio.h"
int main(int argc, char* argv[])
{ int n,flag=200,tag=1; int nu;
// cout <<"please input the numeber of the queue data"<<endl;
// cin>>n;
cout<<"the program begin!"<<endl<<"you can input '0'exit press any key to continue"<<endl;
cin>>tag;
if(tag==0)
return 0;
cout<<" 你是第一次运行这个程序,将为你建立一个循环队列! "<<endl;
cout <<"please input the numeber of the queue data"<<endl;
cin>>n;
char ch;int i; ssqueue<int> q(n+1);
while (1)
{
cout<<" 0 退出 "<<endl;
cout<<" 1 元素入队 "<<endl;
cout<<" 2 出队 "<<endl;
cout<<" 3 查询对头元素 "<<endl;
cout<<" 4 查询对尾元素 "<<endl;
cout<<" 5 查询对列所有元素 "<<endl;
cout<<" 6 查询对列最大容量 "<<endl;
cout<<" 7 查询现在对列中元素的个数 "<<endl;
cin>>flag;
switch(flag)
{
case 0: break;
case 1:
cout <<"please input the numeber of the queue data"<<endl;
cin>>n;
for (i=0;i<n;i++)
{ cout<<"please input the data"<<endl;
cin>>ch;
if(!q.Insert(ch)) break;
}
cout<<"the front and the rear is/n";
cout<<q.getfront()<<"/t"<<q.getrear()<<endl;
break;
case 2:
cout<<" 请输入要出队元素的个数 /n";
cin>>nu;
if(nu>q.getsize())
{ cout<<" 出队失败,队列中的元素没有怎么多 /n"; break;}
for(i=0;i<nu;i++)
if(q.Out()==0) break;
break;
case 3: cout<<q.getfront()<<endl; break;
case 4: cout<<q.getrear()<<endl; break;
case 5: q.print(); cout<<endl; break;
case 6: cout<<q.getsize()<<endl; break;
case 7: cout<<q.count()<<endl; break;
default: cout<<" 你的输入有误!请重试 "<<endl;
}
}
return 0;
}