链队列的几种基本操作

#include
#include
#include
#include
using namespace std;

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int QElemType;

typedef struct QNode {
	QElemType data;
	struct QNode *next;
} QNode,*QueuePtr;

typedef struct {
	QueuePtr front,rear;
} LinkQueue;

//初始化队列
int InitQueue(LinkQueue &Q) {
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front) {
		cout<<"初始化失败!"<next=NULL;
	return OK;
}

//销毁队列
int DestroyQueue(LinkQueue &Q) {
	while(Q.front) {
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	return OK;
}

//清空队列
int ClearQueue(LinkQueue &Q) {
	QueuePtr p,q;
	Q.rear=Q.front;
	p=Q.front->next;
	Q.front->next=NULL;
	while(p) {
		q=p;
		p=p->next;
		free(q);
	}
	return OK;
}

//判断队列是否为空
int QueueEmpty(LinkQueue Q) {
	if(Q.front==Q.rear) {
		cout<<"队列为空!"<next;
	}
	cout<<"队列中元素个数为:"<next;
	cout<<"队列头元素为:"<data<data=e;
	s->next=NULL;
	Q.rear->next=s;
	Q.rear=s;
	return OK;
}

//删除队头元素
int DeQueue(LinkQueue &Q) {
	QueuePtr p;
	if(Q.front==Q.rear) {
		cout<<"该队列是空的,删除失败!"<next;
	Q.front->next=p->next;
	if(Q.rear==p) Q.rear=Q.front;
	free(p);
	cout<<"删除成功!"<>e)!=0) {
		if(e<0) return ERROR;
		InsertQueue(Q,e);
	}
	return OK;
}

//输出队列元素
int QueueTraverse(LinkQueue Q) {
	QueuePtr p;
	p=Q.front->next;
	while(p) {
		cout<data<<" ";
		p=p->next;
	}
	return OK;
}

int main() {
	cout<<"可执行操作有:" <>num) != 0 ) {
		switch(num) {
			case 1:
				if(q.front) {
					cout<<"已初始化队列!"<>e;
					int a=InsertQueue(q,e);
					if(a==1) cout<<"插入成功!"<

你可能感兴趣的:(Algorithm)