队列ADT
来源:互联网 发布:平安委外管理岗 知乎 编辑:程序博客网 时间:2024/05/29 16:29
C语言中使用抽象数据类型方法编程的三个步骤
- 以抽象、通用的方式描述一个类型,包括该类型的操作。
- 设计一个函数接口表示这个新类型。
- 编写具体代码实现这个接口。
定义队列抽象数据类型
队列(queue)是具有两个特殊属性的链表:
- 新项只能添加到链表末尾;
- 只能从链表开头移除项;
队列是一种先进先出(FIFO)的数据形式。
抽象定义:
定义接口(queue.h接口头文件)
//Queue的接口#ifndef _QUEUE_H_#define _QUEUE_H_#include<stdbool.h>typedef int Item;#define MAXQUEUE 10typedef struct node{ Item item; struct node * next;} Node;typedef struct queue{ Node * front; //指向队列首项的指针 Node * rear; //指向队列尾项的指针 int items; //队列中的项数 } Queue;//初始化队列void InitializeQueue(Queue * pq);//检查队列是否已满bool QueueIsFull(const Queue * pq);//检查队列是否为空bool QueueIsEmpty(const Queue * pq);//确定队列中的项数int QueueItemCount(const Queue * pq);//在队列末尾添加项bool EnQueue(Item item, Queue * pq);//在队列开头删除项bool DeQueue(Item *pitem, Queue * pq);//清空队列void EmptyTheQueue(Queue * pq);#endif
实现接口函数(queue.c)
//Queue类型的实现#include<stdio.h>#include<stdlib.h>#include"queue.h"//局部函数static void CopyToNode(Item item, Node * pn);static void CopyToItem(Node * pn, Item * pi);void InitializeQueue(Queue * pq){ pq->front = pq->rear = NULL; pq->items = 0;}bool QueueIsFull(const Queue * pq){ return pq->items == MAXQUEUE; }bool QueueIsEmpty(const Queue * pq){ return pq->items == 0; }int QueueItemCount(const Queue * pq){ return pq->items;}bool EnQueue(Item item, Queue * pq){ Node * pnew; if (QueueIsFull(pq)) return false; pnew = (Node *)malloc(sizeof(Node)); if (pnew == NULL) { fprintf(stderr, "Unable to allocate memory!\n"); exit(1); } CopyToNode(item,pnew); pnew->next = NULL; if (QueueIsEmpty(pq)) pq->front = pnew; //项位于首端 else pq->rear->next = pnew; //项位于末端 pq->rear = pnew; pq->items++; //项数+1 return true;}bool DeQueue(Item * pitem, Queue * pq){ Node * pt; if(QueueIsEmpty(pq)) return false; CopyToItem(pq->front, pitem); pt = pq->front; pq->front = pq->front->next; free(pt); pq->items--; if(pq->items == 0) pq->rear == NULL; return true;} void EmptyTheQueue(Queue * pq){ Item dummy; while(!QueueIsEmpty(pq)) DeQueue(&dummy, pq);}static void CopyToNode(Item item, Node * pn){ pn->item = item;}static void CopyToItem(Node * pn, Item * pi){ *pi = pn->item; }
测试队列(驱动程序 use_q.c)
//驱动程序测试Queue接口#include<stdio.h>#include"queue.h"int main(void){ Queue line; Item temp; char ch; InitializeQueue(&line); puts("Testing the Queue interface. Type a to add a value,"); puts("type d to delete a value, and type q to quit."); while ((ch = getchar()) != 'q') { if (ch != 'a' && ch != 'd') continue;//忽略其他输出 if (ch == 'a') { printf("Integer to add: "); scanf("%d", &temp); if (!QueueIsFull(&line)) { printf("Putting %d into queue.\n", temp); EnQueue(temp,&line); } else puts("Queue is full!"); } else { if(QueueIsEmpty(&line)) puts("Nothing to delete."); else { DeQueue(&temp, &line); printf("Delete %d from queue.\n", temp); } } printf("%d items in queue.\n", QueueItemCount(&line)); puts("Type a to add. d to delete, q to quit:"); } EmptyTheQueue(&line); puts("88"); return 0;}
阅读全文
0 0
- 队列ADT
- 队列ADT
- 队列ADT
- 队列ADT
- 队列ADT实现
- ADT 队列 queue
- 数据结构:队列ADT
- C 基础数据结构---队列 ADT
- ADT LinkQueue 队列的实现
- 数据结构与算法 -- 队列 ADT
- 抽象数据类型总结:复数 adt 和 FIFO 队列adt
- 数据结构(三)之队列ADT
- 数据结构(scheme) -- 抽象数据类型(ADT) -- 队列
- 队列(Queue ADT)C语言实现
- ADT双端队列的实现
- 数据结构-->队列的链式实现 ADT
- 抽象数据类型ADT之队列的构建
- 3.4 表、栈和队列_队列ADT
- 4. Median of Two Sorted Arrays
- [python3.6 flask web学习]web服务器基本结构
- recyclere 点击事件
- Bootstrap 写的webview 在安卓里图片失真,变形
- java数据库初探
- 队列ADT
- Android知识体系梳理笔记二:AIDL进阶之Binder机制
- 设计模式之访问者模式
- ACM暑假集训日记 17.8.7
- HDU 4819 Mosaic 二维线段树
- 二叉树ADT
- Docker下实战zabbix三部曲之一:极速体验
- 对于解决光标消失的一个解决方法
- codeforces 838A Binary Blocks