循环队列实现(非模板)


//CirQueue.h;
#include<iostream>
#include<string>
using namespace std;
const int QueueSize=30;
class CirQueue
 {
	  int date[QueueSize];
	  int front,rear;//定义队列的队头和队尾指针;
     public:
	 CirQueue();
	 ~CirQueue(){}
	 void EnQueue();
	 void DeQueue();
	 int GetQueue();
	 int Empty()
	 {
		 if(front==rear)
			 return 1;
		 else return 0;

	 }


 };
//CirQueue实现细节.cpp;
  #include"CirQueue.h"
  CirQueue::CirQueue()
 {
	 front=rear=QueueSize-1;
 }
  void CirQueue::EnQueue()//入队列操作;注意是使用rear指针的;
  {
      int x;
	
	  if((rear+1)%QueueSize==front)//判断队列是否满了;
		  throw "上溢";
	  int s;
	   do
	   {
	   cout<<"please input 1 number:"<<endl;
	   cin>>x;
	  
	   rear=(rear+1)%QueueSize;//在循环的情况下rear加1的形式;
	   date[rear]=x;
	   cout<<"是否要继续:是的话输入1,否则输入0"<<endl;
	   cin>>s;
	   
	   }while(s==1);


   }
   
   void CirQueue::DeQueue()//出队列;使用front指针;
   {
         int s=1;
	    
	    do{
		 if(rear==front)
		  throw "空队列";
		 front=(front+1)%QueueSize;
		 cout<<date[front]<<endl;
		  cout<<"是否要继续:是的话输入1,否则输入0"<<endl;
	     cin>>s;
		  }while(s==1);
   }

    int CirQueue::GetQueue()
	{
       if(rear==front)
		  throw "空队列";
	   int i=(front+1)%QueueSize;
	   return date[i];
	}
 //testDemo.cpp;
 #include"CirQueue.h"
 int main()
 {
	 CirQueue cir;
	cout<<"*********************"<<endl;
	cout<<"入队列操作"<<endl;
	cout<<"*********************"<<endl;
	 try
	 { cir.EnQueue();}
	 catch(char* s)
	 {
		 cout<<s<<endl;
	 }
	cout<<"...................."<<endl;
	cout<<"出队列操作"<<endl;
	cout<<"...................."<<endl;
	 try
	{cir.DeQueue();}
	catch(char* b)
	{
		cout<<b<<endl;
	}
	cout<<"........................"<<endl;
	cout<<"获取队头元素数据:"<<endl;
	cout<<"........................"<<endl;
	 try
	 {cout<<cir.GetQueue()<<endl;}
	 catch(char* s)
	 {
		 cout<<s<<endl;
	 }
	 cout<<"........................"<<endl;
	 cout<<"判断队列是否为空"<<endl;
	 cout<<"........................"<<endl;
	 if(cir.Empty())
	 {
		 cout<<"该队列为空!"<<endl;
	 }
	 else cout<<"该队列不为空!"<<endl;
	 cout<<"测试结束"<<endl;
	 system("pause");
	 return 0;

 }
***********************
R6010 abort has been called
***********************
第一:可能是申请的空间没有释放

第二:可能是一次申请空间过大

第三:指针指向了不可预期的内存位置



 

猜你喜欢

转载自blog.csdn.net/huangchongwen/article/details/49449359
今日推荐