キューのエンキューとデキュー操作を実現
2199 ワード
1)シーケンスチーム
2)スタックチーム
#include
using namespace std;
const int MAX=10;
class Queue{ private:
int front;
int rear;
int data[MAX];
public:
Queue(){front=rear=MAX-1;}
~Queue(){} ;
void EnQueue(int x);
int DeQueue();
int GetQueue()
{if
(front!=rear) return data[(front+1)%MAX];
};
int Empty(); };
void Queue::EnQueue(int x){
if ((rear-1)%MAX==front) throw " ";
rear=(rear+1)%MAX; data[rear]=x;
}
int Queue::DeQueue(){
if (front==rear) throw " ";
int x=data[front];
front=(front+1)%MAX;
return x;
}
int Queue::Empty(){
if (front==rear) return 1;
else return 0;
}
int main(){
Queue S;
if (S.Empty())
cout<
2)スタックチーム
#include
using namespace std;
struct Data {
int data;
struct Data *next;
} *q;
class Queue{
private:
Data *rear,*front;
public:
Queue(){rear=NULL;front=NULL;}
~Queue(){}
void EnQueue(int x);
int DeQueue();
int GetQueue();
int Empty();
};
void Queue::EnQueue(int x){
q=new Data;
q->data=x;
q->next=NULL;
if (Empty())
front=rear=q; // ,
else {
rear->next=q;
rear=q;}
}
int Queue::DeQueue(){
if (Empty()) throw " ";
q=front;
int x=front->data;
front=front->next;
delete q;
return x;
}
#include
using namespace std;
const int MAX=10;
class Queue{ private:
int front;
int rear;
int data[MAX];
public:
Queue(){front=rear=MAX-1;}
~Queue(){} ;
void EnQueue(int x);
int DeQueue();
int GetQueue()
{if
(front!=rear) return data[(front+1)%MAX];
};
int Empty(); };
void Queue::EnQueue(int x){
if ((rear-1)%MAX==front) throw " ";
rear=(rear+1)%MAX; data[rear]=x;
}
int Queue::DeQueue(){
if (front==rear) throw " ";
int x=data[front];
front=(front+1)%MAX;
return x;
}
int Queue::Empty(){
if (front==rear) return 1;
else return 0;
}
int main(){
Queue S;
if (S.Empty())
cout<