队列 —— 链队列
2017-02-09 21:54
62 查看
队列
代码
/* function:queue created by : xilong date: 2017.2.9 */ #include "iostream" using namespace std; #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Elemtype; typedef int Status; typedef struct QNode { Elemtype data; struct QNode* next; } QNode, *QueuePrt; typedef struct { QueuePrt front, rear; } LinkQueue; /* initialize the queue */ Status Init_Queue(LinkQueue *q) { q->front = q->rear = (QueuePrt)malloc(sizeof(QNode)); if (!q->front) exit(0); q->front->next = NULL; return OK; } /* Insert an element into the queue, an element can only be inserted into the end of the queue */ Status Inster_Queue(LinkQueue *q, Elemtype e) { QueuePrt p; p = (QueuePrt)malloc(sizeof(QNode)); if (p == NULL) exit(0); p->data = e; p->next = NULL; q->rear->next = p; q->rear = p; return OK; } /* Delete an element in the queue, an element which is in the front can only be deleted */ Status Delete_Queue(LinkQueue *q, Elemtype *e) { QueuePrt p; if (q->front == q->rear) { cout << "空队!" << endl; return FALSE; } p = q->front->next; q->front->next = p->next; if (q->rear == p) { q->rear = q->front; } *e = p->data; free(p); return TRUE; } /* length of the queue */ int Length_Queue(LinkQueue *q) { QueuePrt p; int len = 0; p = q->front->next; if (q->front == q->rear) cout << "空队!" << endl; while (p) { len++; p = p->next; } return len; } void main() { LinkQueue q; Elemtype e; int i, len; Init_Queue(&q); Inster_Queue(&q, 1); Inster_Queue(&q, 2); Inster_Queue(&q, 3); Inster_Queue(&q, 4); Inster_Queue(&q, 5); Inster_Queue(&q, 6); Inster_Queue(&q, 7); Inster_Queue(&q, 8); len = Length_Queue(&q); cout << "队列的长度为:"; cout << len << endl; cout << "出对列的顺序为:"; for (i = 0; i < len; i++) { Delete_Queue(&q, &e); cout << e << " "; } cout << endl; system("pause"); }
截图
相关文章推荐
- PHP双向队列
- 队列
- 消息队列
- 数据结构_链队列实验——火车车厢重排问题
- poj 3253 优先队列 堆
- 关于POSIX消息队列mq_open提示错误信息:Invalid argument的问题
- 用消息队列和消息应用状态表来消除分布式事务
- 停车场项目链表入库队列进出库带收费优化升级版
- 单调队列
- 数据结构之链队列
- 并发理解(堵塞队列)
- 解题笔记(12)——用两个栈实现队列
- 栈和队列&前缀,中缀,后缀
- Java数据结构——表、栈和队列
- GCD网络多线程---同步运行,异步运行,串行队列,并行队列
- 【多线程】C++自实现适用于多线程的原子消息队列
- 优先队列
- 队列的数组和链表实现
- 数据结构之队列
- poj 3162 Walking Race (dfs+尺取法单调队列)★