#include <stdio.h>
#include <stdlib.h>
struct Queue{
int *data;
int capacity;
int front;
int rear;
};
void init(struct Queue *pq,int capacity){
pq->capacity=capacity;
pq->data=(int*)malloc(sizeof(int)*(capacity+1));
pq->front=pq->rear=0;
}
int isFull(const struct Queue *pq){
if((pq->rear +1)%(pq->capacity+1)==pq->front) return 1;
else return 0;
}
int isEmpty(const struct Queue *pq){
return pq->front==pq->rear;
}
int enQueue(struct Queue *pq,int k){
if(isFull(pq)) return 0;
else{
pq->data[pq->rear]=k;
pq->rear=(pq->rear+1)%(pq->capacity+1);
return 1;
}
}
int deQueue(struct Queue *pq,int *px){
if(isEmpty(pq)) return 0;
else{
*px=pq->data[pq->front];
pq->front=(pq->front+1)%(pq->capacity+1);
return 1;
}
}
int main(){
struct Queue q;
init(&q,5);
enQueue(&q,11);
enQueue(&q,22);
enQueue(&q,33);
enQueue(&q,44);
enQueue(&q,55);
enQueue(&q,66);
int x;
deQueue(&q,&x);
printf("%d\n",x);
deQueue(&q,&x);
printf("%d\n",x);
deQueue(&q,&x);
printf("%d\n",x);
deQueue(&q,&x);
printf("%d\n",x);
deQueue(&q,&x);
printf("%d\n",x);
deQueue(&q,&x);
printf("%d\n",x);
return 0;
}
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
Node(int x){
data=x;
next=NULL;
}
};
class Queue{
private:
Node* front;
Node* rear;
public:
Queue(){
front=rear=NULL;
}
~Queue(){
Node* tmp;
while(front){
tmp=front;
front=front->next;
delete tmp;
}
}
bool isEmpty(){
return front==NULL;
}
void enQueue(int x){
Node *tmp;
tmp=new Node(x);
if(isEmpty()){
front=rear=tmp;
}
else{
rear->next=tmp;
rear=tmp;
}
}
bool deQueue(int *px){
if(isEmpty()) return false;
else{
*px=front->data;
Node *tmp;
tmp=front;
front=front->next;
delete tmp;
if(front==NULL)
rear=NULL;
return true;
}
}
};
int main(){
Queue q;
q.enQueue(11);
q.enQueue(22);
q.enQueue(33);
q.enQueue(44);
int x;
q.deQueue(&x);
cout<<x<<endl;
q.deQueue(&x);
cout<<x<<endl;
q.deQueue(&x);
cout<<x<<endl;
q.deQueue(&x);
cout<<x<<endl;
q.deQueue(&x);
cout<<x<<endl;
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> q;
q.push(11);
q.push(22);
int x;
x=q.front();
cout<<x<<endl;
q.pop();
x=q.front();
cout<<x<<endl;
return 0;
}