c语言链队的实现

#include
typedef int Status;
using namespace std;
typedef struct QNode{
	int data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct{
	QueuePtr front;//队头指针 
	QueuePtr rear;//队尾指针 
}LinkQueue;

Status InitQueue(LinkQueue &q){
	q.front = q.rear = new QNode;
	q.front->next = NULL;
	return 1;
}

Status EnQueue(LinkQueue &q,int e){
	QNode *p = new QNode;
	p->data = e;
	p->next = NULL;
	q.rear->next = p;
	q.rear = p;
	return 1;
}

Status DeQueue(LinkQueue &q,int &e){
	if(q.front == q.rear)
		return 0;
	QNode *p = q.front->next;	
	e = p->data;
	q.front->next = p->next;
	if(q.rear == p)
		q.rear = q.front;//当最后一个元素被删除,队尾指针指向头结点 
	delete p;
}

int GetHead(LinkQueue q){
	if(q.front != q.rear)
		return q.front->next->data;
	exit(-2);
}

void PrintQueue(LinkQueue q){
	if(q.rear == q.front)
		return ;
	q.front = q.front->next;
	while(1){
		cout<data<<" ";
		if(q.rear == q.front)
			break;
		q.front = q.front->next;
	}
	cout<>n;
	 cout<<"请输入"<>e;
	 	status = EnQueue(q,e);
	 	if(status)
	 		cout<>n;
	 for(int i=0;i

 

你可能感兴趣的:(栈和队列,数据结构,数据结构)