【链式队列】接口的封装

来源:互联网 发布:最近mmd喵萝脸型数据 编辑:程序博客网 时间:2024/05/16 14:00
//编译环境 VS2013//头文件 queue.h#include<stdio.h>#include<stdlib.h>struct QUEUE{int num;//数据int high;//优先级struct QUEUE *pNext;//存储下一个节点地址};typedef struct QUEUE Queue;Queue * initQueue(Queue *queueA);Queue * enQueue(Queue * queueA, int num, int high); //入队Queue * deQueue(Queue *queueA, Queue *pOut); //出队列Queue * freeAll(Queue *queueA);//释放队列void  sortQueue(Queue *queueA); //仅仅按优先级排序 入队时调用,void printfAll(Queue *queueA); //打印所有成员Queue * insertSortEnQueue(Queue * queueA, int num, int high); //入队和排序一个函数完成//函数定义  queue.c#include<stdio.h>#include<stdlib.h>#include "queue.h"Queue * initQueue(Queue *queueA){return NULL;}//顺序入队,这个函数实现的功能是每次入队一个数据,就把这个数据链在尾部//没有按照优先级进行排队Queue * enQueue(Queue * queueA, int num, int high) //入队{//申请空间Queue *pNewNode = (Queue *)malloc(sizeof(Queue));pNewNode->num = num;pNewNode->high = high;pNewNode->pNext = NULL;if (queueA == NULL){queueA = pNewNode;//sortQueue(queueA); //只有一个节点没有必要排序return queueA;}else{Queue *p = queueA;while (p->pNext != NULL){p = p->pNext; //循环到队尾}p->pNext = pNewNode; //插入新节点sortQueue(queueA); //每次有新节点插入 就需要排序return queueA;}}//顺序出出队Queue * deQueue(Queue *queueA, Queue *pOut) //出队列{if (queueA == NULL){return NULL;}else{pOut->num = queueA->num;pOut->high = queueA->high; //出队列的节点赋值给poutQueue *pTemp = queueA;queueA = queueA->pNext; //头结点移动到下一个节点free(pTemp);return queueA;}}Queue * freeAll(Queue *queueA)//释放队列{if (queueA == NULL) {return NULL;}else{Queue *p1 = queueA;  //p1z指向头结点Queue *p2 = NULL;while (p1->pNext != NULL)//如果超过一个节点{p2 = p1->pNext;//p2指向头结点的下一个节点p1->pNext = p2->pNext;  //跳过p2free(p2);               //释放p2}free(queueA);  //最后剩下头结点 释放return NULL;}}//在顺序入队之后void  sortQueue(Queue *queueA) //按优先级排序{if (queueA == NULL || queueA->pNext == NULL){return;}//冒泡排序for (Queue *p1 = queueA; p1 != NULL; p1 = p1->pNext){for (Queue *p2 = queueA; p2 != NULL; p2 = p2->pNext){if (p1->high > p2->high)//high值越大优先级越靠前{Queue temp;temp.num = p1->num;p1->num = p2->num;p2->num = temp.num; //交换num值temp.high = p1->high;p1->high = p2->high;p2->high = temp.high; //交换high值//如果是字符串就不可以使用 = }}}}//递归实现void printfAll(Queue *queueA)//打印所有成员{if (queueA == NULL){return;}else{printf("%d, %d\n", queueA->num, queueA->high);printfAll(queueA->pNext); //进入下一个节点}}//插入排序Queue * insertSortEnQueue(Queue * queueA, int num, int high) //入队{//申请空间Queue *pNewNode = (Queue *)malloc(sizeof(Queue));pNewNode->num = num;pNewNode->high = high;  //赋值,这里没有对pNewNode->pNext 赋值为NULL,因为pNewNode可能指向下一个非空节点if (queueA == NULL) //还没有任何节点{pNewNode->pNext = NULL; //传入节点的下一个节点为空queueA = pNewNode;//直接将第一个入队的数据链在头结点return queueA;}else{if (pNewNode->high > queueA->high)//后入队的优先级高于先入队的{pNewNode->pNext = queueA;//头部插入queueA = pNewNode;return queueA;}else//后入队的优先级小于先入队的优先级尾部插入{Queue *p = queueA;while (p->pNext != NULL)//循环到尾部{p = p->pNext;}if (pNewNode->high <= p->high) //p循环到了尾部,直接链在末尾{p->pNext = pNewNode;pNewNode->pNext = NULL;return queueA;}else  //p没有循环到尾部 {Queue *p1, *p2;p1 = p2 = NULL;p1 = queueA;while (p1->pNext != NULL){p2 = p1->pNext;if (pNewNode->high <= p1->high && pNewNode->high > p2->high){pNewNode->pNext = p2;p1->pNext = pNewNode;break;}p1 = p1->pNext;}return queueA; }}}}//测试文件  man.c#include<stdio.h>#include<stdlib.h>#include "queue.h"void main(){Queue *pHead = NULL;pHead = initQueue(pHead);//标准初始化//按优先级入栈测试pHead = insertSortEnQueue(pHead, 10, 0);pHead = insertSortEnQueue(pHead, 20, 2);pHead = insertSortEnQueue(pHead, 30, 6);pHead = insertSortEnQueue(pHead, 50, 7);pHead = insertSortEnQueue(pHead, 40, 1);pHead = insertSortEnQueue(pHead, 400, 1);pHead = insertSortEnQueue(pHead, 500, 7);printfAll(pHead);//顺序入栈测试/*pHead = enQueue(pHead, 10, 2);pHead = enQueue(pHead, 20, 1);pHead = enQueue(pHead, 30, 4);pHead = enQueue(pHead, 40, 3);*///printfAll(pHead);////出队列测试//{//int i = 1;//while (pHead != NULL)//{//Queue *pOut = (Queue *)malloc(sizeof(Queue));//pHead = deQueue(pHead, pOut);//printf("\n第%d次出队列:\n", i);//printfAll(pHead);//printf("\n第%d次出队列的数据是:\n%d, %d\n", i, pOut->num, pOut->high);//i++;//}//}////释放queue空间测试//pHead = freeAll(pHead);//printfAll(pHead);//if (pHead == NULL)//{//printf("释放成功\n");//}system("pause");}

1 0
原创粉丝点击