链队列的出队入队

来源:互联网 发布:最好玩的网络手机游戏 编辑:程序博客网 时间:2024/04/30 07:36
#includeusing namespace std;struct Node{    //存放元素的结点类型,数据域和指针域int data;Node *next;};class LinkQueue{private:Node *front,*rear;    //头指针和尾指针public:LinkQueue();~LinkQueue();void EnQueue(int e);   //入队int DeQueue();         //出队int GetQueue();         //取队头元素int Empty();              //判断链队列是否为空};                                        //成员函数的类外定义LinkQueue::LinkQueue(){       front=new Node;front->next=NULL;rear=front;}LinkQueue::~LinkQueue(){Node *p;while(front!=NULL){p=front;front=p->next;delete p;}}void LinkQueue::EnQueue(int e){                                 //新建一个结点,其数据域存放入队元素e,指针域置空Node *P=new Node;   //生成新元素结点P->data=e; //装入元素eP->next=NULL;//为队尾结点rear->next=P; //插入新结点rear=P;     //修改尾指针}int LinkQueue::DeQueue(){                                   //把队头结点地址赋给p,头指针front下移,返回p结点的数据元素,if(front==rear) throw"队列为空";//无法删除Node *p=front->next;  //定义结点P指向要删除的队头结点front->next=p->next;//删除队头结点int e=p->data;//取出数据域的值if(rear==p) rear=front;free(p);return e;}int LinkQueue::GetQueue()//读取队头元素{if(front==rear) throw"队列为空";return front->next->data;}int LinkQueue::Empty(){if(front==rear) return 1;else return 0;}void main(){try{LinkQueue Q;     //对象try{cout<<"执行元素32入队操作,";Q.EnQueue(32);}catch(char *p){cout<





总结:注意数据结构
原创粉丝点击