链队列的出队入队

#include
   
    
using 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; //装入元素e
	P->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<
    
   





总结:注意数据结构

版权声明:本文为zBlueny原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。