队列的链式表示和实现----单链队列

来源:互联网 发布:进驻淘宝协议 编辑:程序博客网 时间:2024/05/22 12:44

头文件 head.h

#include<string.h>#include<ctype.h>#include<malloc.h> /* malloc()等 */#include<limits.h> /* INT_MAX等 */#include<stdio.h> /* EOF(=^Z或F6),NULL */#include<stdlib.h> /* atoi() */#include<io.h> /* eof() */#include<math.h> /* floor(),ceil(),abs() */#include<process.h> /* exit() *//* 函数结果状态代码 */#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1/* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */typedef int QElemType;//数据类型//单链队列,队列的链式存储结构typedef struct QNode{QElemType data;//数据域struct QNode *next;//指针域}QNode, *QueuePtr;typedef struct{QueuePtr front;//队头指针,始终指向头结点QueuePtr rear;//队尾指针,指向最后一个节点,当是空队列时,都指向头结点}LinkQueue;Status InitQueue(LinkQueue *Q);Status DestoryQueue(LinkQueue *Q);Status ClearQueue(LinkQueue *Q);Boolean QueueEmtpy(LinkQueue Q);int QueueLength(LinkQueue Q);Status GetHead(LinkQueue Q, QElemType *e);Status EnQueue(LinkQueue *Q, QElemType e);Status DeQueue(LinkQueue *Q, QElemType *e);Status QueueTraverse(LinkQueue Q, Status (*visit)(QElemType e));


算法实现


#include"head.h"Status InitQueue(LinkQueue *Q){//构造一个空队列Q(*Q).front = (QueuePtr)malloc(sizeof(QNode));if (!(*Q).front)//判断存储空间非配是否成功{printf("初始化失败!");system("pause");exit(-1);}(*Q).rear = (*Q).front;(*Q).front->next = NULL;//空队列return OK;}Status DestoryQueue(LinkQueue *Q){//销毁队列Qwhile ((*Q).front){(*Q).rear = (*Q).front->next;free((*Q).front);(*Q).front = (*Q).rear;}return OK;}Status ClearQueue(LinkQueue *Q){DestoryQueue(Q);InitQueue(Q);return OK;}Boolean QueueEmtpy(LinkQueue Q){if (Q.front == Q.rear)return TRUE;elsereturn FALSE;}int QueueLength(LinkQueue Q){int n = 0;while (Q.front != Q.rear){Q.front = Q.front->next;n++;}return n;}Status GetHead(LinkQueue Q, QElemType *e){//若队列不为空,则用e返回Q的队头元素,并返回OK;否则,返回FALSE;if (QueueEmtpy(Q))return FALSE;else{*e = Q.front->next->data;return OK;}}Status EnQueue(LinkQueue *Q, QElemType e){//插入元素e为Q的新的队尾元素QueuePtr p = (QueuePtr)malloc(sizeof(QNode));if (!p){printf("插入元素失败!");system("pause");exit(-2);}p->data = e;p->next = NULL;(*Q).rear->next = p;(*Q).rear = p;return OK;}Status DeQueue(LinkQueue *Q, QElemType *e){//若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK;//否则,返回ERROR;if (QueueEmtpy(*Q))return ERROR;else{QueuePtr p = (*Q).front->next;//被删除的元素(*Q).front->next = p->next;*e = p->data;if ((*Q).rear == p)//如果队列中只有一个元素,应该处理一下队尾元素(*Q).rear = (*Q).front;free(p);return  OK;}}Status QueueTraverse(LinkQueue Q, Status(*visit)(QElemType e)){if (QueueEmtpy(Q)){printf("队列为空!");return ERROR;}else{QueuePtr p = Q.front->next;//第一个元素while (p){visit(p->data);p = p->next;}}}


测试文件:

#include"head.h"Status visit(QElemType e){printf("%d  ", e);return OK;}void main(){int i;QElemType d;LinkQueue q;i = InitQueue(&q);if (i)printf("成功地构造了一个空队列!\n");printf("是否空队列?%d(1:空 0:否)  ", QueueEmtpy(q));printf("队列的长度为%d\n", QueueLength(q));EnQueue(&q, -5);EnQueue(&q, 5);EnQueue(&q, 10);printf("插入3个元素(-5,5,10)后,队列的长度为%d\n", QueueLength(q));printf("是否空队列?%d(1:空 0:否)  ", QueueEmtpy(q));printf("队列的元素依次为:");QueueTraverse(q, &visit);i = GetHead(q, &d);if (i == OK)printf("队头元素是:%d\n", d);DeQueue(&q, &d);printf("删除了队头元素%d\n", d);i = GetHead(q, &d);if (i == OK)printf("新的队头元素是:%d\n", d);ClearQueue(&q);printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n", q.front, q.rear, q.front->next);DestoryQueue(&q);printf("销毁队列后,q.front=%u q.rear=%u\n", q.front, q.rear);system("pause");}

Running Result:

成功地构造了一个空队列!是否空队列?1(1:空 0:否)  队列的长度为0插入3个元素(-5,5,10)后,队列的长度为3是否空队列?0(1:空 0:否)  队列的元素依次为:-5  5  10  队头元素是:-5删除了队头元素-5新的队头元素是:5清空队列后,q.front=5014240 q.rear=5014240 q.front->next=0销毁队列后,q.front=0 q.rear=0请按任意键继续. . .


1 0
原创粉丝点击