#include <cstdlib>
#include <iostream>
#define LIST_SIZE 5
using namespace std;
class LX{
private:
int key;
public:
LX(int key){this->key=key;}
int getkey(){return key;}
LX(){this->key=key;}
void set_key(int key){this->key=key;}
};
class Queue{
private:
LX *listarray;
int size;
int rear;
int front;
public:
Queue(int sz=LIST_SIZE){
size=sz+1;
listarray=new LX[size];
rear=front=0;
}
~Queue(){delete []listarray;}
void clear(){front=rear;}
bool isEmpty(){return rear==front;}
bool isFull(){return(front==(rear+1)%size);}
LX First(){
if(!isEmpty())
return listarray[(front+1)%size];
else
cout<<"queue is empty,don't have a last one"<<endl;}
LX last()
{ if(!isEmpty())
return listarray[(rear-1)%size];
else
cout<<"queue is empty,don't have a last one"<<endl;}
void add(int item){
if(!isFull()){
rear=(rear+1)%size;
listarray[rear].set_key(item);
}
else
cout<<"error,queue is full"<<endl;
}
LX _delete(){
if(!isEmpty()){
front=(front+1)%size;
return listarray[front];
}
else{
cout<<"error delete,it's a empty queue"<<endl;
}
}
void display();
};
void Queue::display(){
int count=1;
int i=0;
if(!isEmpty()){
cout<<endl<<endl;
cout<<"ITEM ↓"<<endl;
for(i=(front+1)%size;i!=rear;++i%size){
cout<<"["<<listarray.getkey()<<"]";
++count;
if(count%5==0){cout<<endl;
}
}
cout<<"["<<listarray.getkey()<<"]"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"total: "<<count<<endl;
}else{
cout<<"It's a emptu queue"<<endl;
}
}
void output_f(){
//-----------------------------------output begin
cout<<"This is the Yuanjian Liu's Queue~~"<<endl;
cout<<"----------------------------------"<<endl;
cout<<"please input your opion ~"<<endl;
cout<<"!~~~ i: insert(add)"<<endl;
cout<<"!~~~ d: delete"<<endl;
cout<<"!~~~ e: exit"<<endl;
cout<<"thank you for using"<<endl;
cout<<"----------------------------------"<<endl;
//--------------------------------------output end
}
int main(int argc, char *argv[])
{
Queue a;
int do_key;
char option;
while(1){
system("cls");
cout<<"Your option here: ";
cin>>option;
switch(option){
case 'i':
cout<<"please input do_key:"<<endl;
cin>>do_key;
a.add(do_key);
cout<<"After the insert(add),the queue is :"<<endl;
a.display();
system("PAUSE");
break;
case 'd':
a._delete();
cout<<"After the delete,the queue is:"<<endl;
a.display();
system("PAUSE");
break;
case 'e':
cout<<endl<<endl<<endl
<<" thank you for using"<<endl
<<" welcome again"<<endl;
system("PAUSE");
exit(0);
break;
default:
cout<<"error!don't input unlawful option!"<<endl;
system("PAUSE");
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
为什么进去循环以后 (rear在数组位置中front的前面)就出现内存不能读?
求教