My Queue ADT 链表实现
来源:互联网 发布:洛阳平安数据科技待遇 编辑:程序博客网 时间:2024/05/16 10:59
紧接着 stack ADT ,又完成了个简单的链表,没什么好说的。
主要是 默认QueueElement 使用 void*,若要自定义,请于引用queue.h前 插入一些代码:
#define QUEUE_ELEMENT_DEF
typedef TypeNullable QueueElement;
类型最好是指针之类的可以 赋值为NULL的,不然就自己改函数去吧。<^_^>
(Win7, VIm + GCC)
queue.h
/* * * * * * * * * * * * * * * * * * * * * * * * * * A ADT Queue. * Notice: This queue only save pointer that point * to data. * Because the elements are void pointers, * you should convert into the correct * type that you need. * * * * * * * * * * * * * * * * * * * * * * * * */#ifndef QUEUE_ADT#define QUEUE_ADT/* * * * * * * * * * * * * * * * * * * * * * * * * * The default type of the queue's element is void*. * If you want to use your type(must be a kind of * pointer(or that value can be NULL), or you have * to change the code of the functions), to do like * this: * #define QUEUE_ELEMENT_DEF * typedef TypeNullable QueueElement; * And these codes must be written before inlclude * queue.h. * * * * * * * * * * * * * * * * * * * * * * * * */#ifndef QUEUE_ELEMENT_DEF#define QUEUE_ELEMENT_DEFtypedef void* QueueElement;#endifstruct QueueNode;typedef struct QueueNode QueueNode;typedef QueueNode* NodePointer;struct QueueNode{ QueueElement data; NodePointer next;};typedef struct{ int nodeCount; int maxSize; NodePointer front; NodePointer rear;} Queue;typedef Queue* QueuePointer;#define MIN_QUEUE_SIZE (8)QueuePointer NewQueue(int maxSize);int IsQueueFull(QueuePointer queue);int IsQueueEmpty(QueuePointer queue);int EnQueue(QueuePointer queue, QueueElement element);QueueElement GetFrontElement(QueuePointer queue);QueueElement DeQueue(QueuePointer queue);int EmptyQueue(QueuePointer queue);int DisposeQueue(QueuePointer queue);int DoubleQueueSize(QueuePointer queue);#endif
queue.c
#include <stdlib.h>/* * * * * * * * * * * * * * * * * * * * * * * * * * One implementation of ADT Queue. * Notice: This queue only save pointer that point * to data. * Because the elements are void pointers, * you should convert into the correct * type that you need. * * * * * * * * * * * * * * * * * * * * * * * * *//* * * * * * * * * * * * * * * * * * * * * * * * * * The default type of the queue's element is void*. * If you want to use your type(must be a kind of * pointer(or that value can be NULL), or you have * to change the code of the functions), to do like * this: * #define QUEUE_ELEMENT_DEF * typedef TypeNullable QueueElement; * And these codes must be written before inlclude * queue.h. * * * * * * * * * * * * * * * * * * * * * * * * *///Heder file of ADT queue.#include "queue.h"/* * * * * * * * * * * * * * * * * * * * * * * * * * NewQUeue: Create a new queue that size is * maxSize( >=8 ). * Return: A pointer of queue, if create * successfully. * NULL: Fail to create. * * * * * * * * * * * * * * * * * * * * * * * * */QueuePointer NewQueue(int maxSize){ if (maxSize < MIN_QUEUE_SIZE) { maxSize = MIN_QUEUE_SIZE; } QueuePointer newQueue = NULL; if ( newQueue = (QueuePointer)malloc(maxSize * sizeof(Queue)) ) { newQueue->nodeCount = 0; newQueue->maxSize = maxSize; newQueue->front = NULL; newQueue->rear = NULL; return newQueue; } else { return NULL; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsQueueFull: Decide whether a queue is full; * Return: 1: Is full. * 0: Not full. * * * * * * * * * * * * * * * * * * * * * * * * */int IsQueueFull(QueuePointer queue){ if (queue->nodeCount == queue->maxSize) { return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * IsQueueEmpty: Decide whether a queue is empty; * Return: 1: Is empty. * 0: Not empty. * * * * * * * * * * * * * * * * * * * * * * * * */int IsQueueEmpty(QueuePointer queue){ if (queue->nodeCount == 0) { return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * EnQueue: Add a new element to the rear of queue. * Return: 1: Successfully. * 0: Fail to add. * -1: Queue is full. * -2: Invalid element, can't be NULL. * * * * * * * * * * * * * * * * * * * * * * * * */int EnQueue(QueuePointer queue, QueueElement element){ if (!element) { return -2; } if (queue) { if ( !IsQueueFull(queue)) { NodePointer theNode = (NodePointer)malloc(sizeof(QueueNode)); if (!theNode) { return 0; } theNode->data = element; theNode->next = NULL; if ( queue->rear ) { queue->rear->next = theNode; queue->rear = queue->rear->next; } else { queue->rear = theNode; queue->front = theNode; } queue->nodeCount++; return 1; } else { return -1; } } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * GetFrontElement: Just get the front element. * Return: QueneElement: The front element of the * queue. * NULL: Unsuccessfully or empty queue. * * * * * * * * * * * * * * * * * * * * * * * * */QueueElement GetFrontElement(QueuePointer queue){ if (queue) { if (!IsQueueEmpty(queue)) { return queue->front->data; } else { return NULL; } } else { return NULL; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DeQueue: Delete the front of queue and return it. * Return: QueneElement: The front element of the * queue. * NULL: Unsuccessfully or empty queue. * * * * * * * * * * * * * * * * * * * * * * * * */QueueElement DeQueue(QueuePointer queue){ if (queue) { if (!IsQueueEmpty(queue)) { QueueElement element = queue->front->data; NodePointer node = queue->front; queue->front = queue->front->next; queue->nodeCount--; if (!queue->front) { queue->rear = queue->front; } free(node); return element; } else { return NULL; } } else { return NULL; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * EmptyQueue: Delete all elements from a queue. * Return: 1: All deleted. * 0: ERROR. * * * * * * * * * * * * * * * * * * * * * * * * */int EmptyQueue(QueuePointer queue){ if (queue) { if (queue->front) { NodePointer temp; while (queue->rear != queue->front) { temp = queue->front; queue->front = queue->front->next; free(temp); queue->nodeCount--; } free(queue->front); queue->front = NULL; queue->rear = NULL; queue->nodeCount--; } return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DisposeQueue: Delete a queue itself(and delete * all elements). * Return: 1: Successfully. * 0: Unsuccessfully. * Warning: After disposing a queue, you must make * all pointer pointing into the queue * be NULL! * * * * * * * * * * * * * * * * * * * * * * * * */int DisposeQueue(QueuePointer queue){ if ( EmptyQueue(queue) ) { free(queue); return 1; } else { return 0; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * DoubleQueueSize: Double the size of the queue. * Return: 1: Successfully. * 0: Unsuccessfully. * * * * * * * * * * * * * * * * * * * * * * * * */int DoubleQueueSize(QueuePointer queue){ if (queue) { queue->maxSize *= 2; return 1; } else { return 0; }}
//做个测试。
program.c
#include <stdio.h>#define QUEUE_ELEMENT_DEFtypedef char QueueElement;#include "queue.h"void PrintInf(QueuePointer queue);int main(void){ int size = 0; do { printf("Enter the size of queue:"); scanf("%d", &size); } while (size < 1); //Create a new queue. QueuePointer theQ = NewQueue(size); if (!theQ) { return -1; } PrintInf(theQ); //EnQueue test. EnQueue(theQ, 'a'); EnQueue(theQ, 'b'); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'd'); PrintInf(theQ); //GetFrontElement test. char ch = GetFrontElement(theQ); printf(" %c\n", ch); PrintInf(theQ); //DeQueue test. ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); //FullQueue test. EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); EnQueue(theQ, 'c'); PrintInf(theQ); puts("\n"); DoubleQueueSize(theQ); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); EnQueue(theQ, 'x'); PrintInf(theQ); /* //DeQueue all. ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); ch = DeQueue(theQ); printf(" %c\n", ch); PrintInf(theQ); */ //EmptyQueue test EmptyQueue(theQ); PrintInf(theQ); if (DisposeQueue(theQ)) { theQ = NULL; puts(" Dised!"); } getch(); return 0;}void PrintInf(QueuePointer queue){ printf(" maxSize = %d, node count = %d\n", queue->maxSize, queue->nodeCount); printf(" Front = %d, Rear = %d\n", queue->front, queue->rear);}
简单测试
- My Queue ADT 链表实现
- 队列(Queue ADT)C语言实现
- My ADT Stack...简单数组实现
- My New ADT Stack 数组实现[修改]
- queue链表实现C++
- ADT 队列 queue
- 多项式ADT的链表实现
- ADT堆栈的链表实现
- Practice.3_YU My Queue
- 用无序链表实现字典ADT(C++描述)
- 用无序链表实现字典ADT(C++描述)
- Data Structure 初学链表(ADT实现)
- 链表实践—多项式ADT(数组实现)
- adt之链表
- ADT- 双向链表
- C++用数组和链表分别实现Queue
- Queue类模板的链表实现(基本功能)
- 自己实现 STL Queue(用数组和链表)
- 驱动学习总结(二)
- EJB---->实体继承
- 第十三实验报告(二)
- 新建表的时候:关键字已定义; 字段 CSMC 可能不在关键字中
- arcsde9.3 for oracle11g 安装详解
- My Queue ADT 链表实现
- net-snmp开发简介
- JSF---->环境搭建及第一个程序
- xsl 文件新手入门
- 存储图片到iOS的Photo Album之中
- simplehelix设置mysql远程访问
- JSF---->导航规则设置
- 软件设计需求分析---用例说明模板2(单列表格式)
- 一、 桥梁(Bridge)模式