算法导论第十章--队列的实现

来源:互联网 发布:新网域名备案流程 编辑:程序博客网 时间:2024/06/05 00:28

queue.h

#include<iostream>using namespace std;#define Max 20class queue{private:int length;int *head;int *tail;int Node[Max];public:queue():length(0){//head=new int;//tail=new int;}~queue(){//delete head;//delete tail;}bool Enqueue(int x){if(length>=Max){cout<<"the queue is full!"<<endl;return 0;}Node[length]=x;if(length==0){//注意这个地方不要写错,如果写成*tail=Node[length]是不可以的,因为tail为空tail=&Node[length];head=&Node[length];}elsetail=&Node[length];length++;return 1;}bool Dequeue(){if(length==0){cout<<"the queue  is empty!"<<endl;return 0;}int i;for(i=0;i<length-1;i++){Node[i]=Node[i+1];}length--;*head=Node[0];*tail=Node[length-1];return 1;}bool IsFull(){if(length==Max){return 1;}return 0;}bool IsEmpty(){if(length==0){return 1;}return 0;}void Print(){for(int i=0;i<length;i++){cout<<Node[i]<<" ";}cout<<endl;}};


 

main.cpp

#include"queue.h"int main(){queue q;int i;for(i=0;i<10;i++){q.Enqueue(i*2);}q.Print();cout<<endl;for(i=0;i<10;i++){q.Dequeue();}q.Print();return 0;}


 

原创粉丝点击