www.pudn.com > 4rd.rar > queue.h


#include "head.h" 
 
struct listnode{		//链表结点数据结构 
	node *data; 
	listnode *next; 
}; 
 
class queue{ 
public: 
	listnode *front;		//指向表头结点的指针 
	listnode *rear;			//指向链尾结点的指针 
public: 
	queue(void);			//构造函数 
	~queue(void){};			//析构函数 
	void EnQueue(node *x);	//入列操作 
	node *DelQueue(void);	//出列操作 
	node *GetFront(void)	//取队头元素 
	{ 
		if(front==rear) 
			return NULL; 
		else 
			return front->next->data; 
		return NULL; 
	} 
	int IsEmpty(void)		//判队空 
	{ 
		if(front==rear) 
			return 1; 
		else 
			return 0; 
	} 
	void Clear(void);		//清队空 
	void Delete(node *x);	//删除指定结点 
}q,qe; 
 
void queue::Delete(node *x) 
{ 
	listnode *p,*q; 
	p=front->next; 
	q=front; 
	while(p!=NULL) 
	{ 
		if(p->data==x) 
		{ 
			q->next=p->next; 
			delete p; 
			break; 
		} 
		p=p->next; 
		q=q->next; 
	} 
} 
 
queue::queue(void) 
{ 
	listnode *tempnode; 
	tempnode=new listnode; 
	tempnode->next=NULL; 
	tempnode->data=NULL; 
	front=rear=tempnode; 
} 
 
void queue::EnQueue(node *x) 
{ 
	listnode *tempnode; 
	tempnode=new listnode; 
	tempnode->data=x; 
	tempnode->next=NULL; 
	rear->next=tempnode; 
	rear=tempnode; 
} 
 
node* queue::DelQueue(void) 
{ 
	listnode *tempnode; 
	node *temp; 
	if(front==rear) 
		return NULL; 
	else 
	{ 
		tempnode=front->next; 
		temp=tempnode->data; 
		front->next=tempnode->next; 
		if(tempnode==rear) 
			rear=front; 
		delete tempnode; 
		return temp; 
	} 
	return NULL; 
} 
 
void queue::Clear(void) 
{ 
	listnode *temp1,*temp2; 
	temp1=front->next; 
	while(temp1!=NULL) 
	{ 
		temp2=temp1; 
		temp1=temp1->next; 
		delete temp2; 
	} 
	rear=front; 
	front->next=NULL;	/////////// 
} 
 
class stacklist{ 
public: 
	listnode *top; 
public: 
	stacklist(void){top=NULL;}	//构造函数 
	~stacklist(void){}; 
	void Push(node *x);		//把元素x压入栈中 
	node* Pop();			//使栈顶元素出栈 
	node* GetTop();			//取栈顶元素 
	void Clear(){top=NULL;}	//清空栈 
	int IsEmpty()			//判断栈是否为空,如果为空则返回1,否则返回0 
	{ 
		if(top==NULL) 
			return 1; 
		else 
			return 0; 
	} 
}sl; 
 
void stacklist::Push(node *x) 
{ 
	listnode *tempnode; 
	tempnode=new listnode; 
	tempnode->data=x; 
	tempnode->next=top; 
	top=tempnode; 
} 
 
node* stacklist::Pop() 
{ 
	node *x; 
	listnode *tempnode; 
	if(top==NULL) 
		return NULL; 
	else 
	{ 
		tempnode=top; 
		top=top->next; 
		x=tempnode->data; 
		delete tempnode; 
		return x; 
	} 
} 
 
node* stacklist::GetTop() 
{ 
	if(top==NULL) 
		return NULL; 
	else 
		return top->data; 
}