链队列
2015-06-15 21:54
357 查看
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef int QElemType; typedef struct QNode { QElemType data; struct QNode *next; }QNode, *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; //构造一个空队列 Status InitQueue(LinkQueue &Q) { Q.front = Q.rear = (QueuePtr)malloc(sizeof(QueuePtr)); if (!Q.front) exit(OVERFLOW); Q.front->next = NULL; return OK; } //销毁队列 Status DestroyQueue(LinkQueue &Q) { while (Q.front) { Q.rear = Q.front->next; free(Q.front); Q.front = Q.rear; } return OK; } //插入元素e为Q的新元素 Status EnQueue(LinkQueue &Q, QElemType e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if (!p) exit(OVERFLOW); p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } //若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK; Status DeQueue(LinkQueue &Q, QElemType &e) { if (Q.front == Q.rear) return ERROR; QueuePtr p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p) Q.rear = Q.front; free(p); return OK; }
相关文章推荐
- Single Number
- 忍——做事的理智行为
- 剑指Offer--面试题17:合并两个排序的链表--Java实现
- Looksery Cup 2015 A. Face Detection
- ubuntu下使用codeblocks
- iOS方法类:CGAffineTransform的使用大概
- 【jQuery】鼠标接触按钮后改变图片
- Macbook下Android studio的butterknife的应用
- 【一步一步的积累】SuperPixel
- 在Entity Framework 7中进行数据迁移
- 【C语言】指针是一个变量
- MySQL按照汉字拼音字母排序
- Git配置笔记
- MySQL按照汉字拼音字母排序
- MySQL按照汉字拼音字母排序
- 非ARC环境下快速创建数组的注意点
- C++中const与指针、引用的分析
- java之弱引用
- Maven-生命周期
- WEB服务器与应用服务器