消息队列-链表实现
来源:互联网 发布:高清网络电视在线观看 编辑:程序博客网 时间:2024/06/14 14:38
#define __01QUEUE_H__
//分配动态存储区的函数
void queue_init();
//释放动态存储区的函数
void queue_deinit();
//判断队列是否满的函数
int queue_full();
//判断队列是否空的函数
int queue_empty();
//统计队列中数字个数的函数
int queue_size();
//向队列中放数字的函数
void queue_push(int );
//从队列中获得数字的函数(删除数字)
int queue_pop();
//从队列中获得下一个数字的函数(不删除数字)
int queue_front();
#endif //__01QUEUE_H__
#include <stdlib.h>
#include "01queue.h"
typedef struct node {
int num;
struct node *p_next;
} node;
static node head, tail;
//分配动态存储区的函数
void queue_init() {
head.p_next = &tail;
}
//释放动态存储区的函数
void queue_deinit() {
while (head.p_next != &tail) {
node *p_first = &head;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
}
//判断队列是否满的函数
int queue_full() {
return 0;
}
//判断队列是否空的函数
int queue_empty() {
return head.p_next == &tail;
}
//统计队列中数字个数的函数
int queue_size() {
int cnt = 0;
node *p_node = NULL;
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &tail) {
cnt++;
}
}
return cnt;
}
//向队列中放数字的函数
void queue_push(int num) {
node *p_node = NULL;
for (p_node = &head;p_node != &tail;p_node = p_node->p_next) {
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_first->p_next == &tail) {
node *p_tmp = (node *)malloc(sizeof(node));
if (p_tmp) {
p_tmp->num = num;
p_tmp->p_next = NULL;
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
}
break;
}
}
}
//从队列中获得数字的函数(删除数字)
int queue_pop() {
if (head.p_next == &tail) {
return -1;
}
else {
int tmp = head.p_next->num;
node *p_first = &head;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return tmp;
}
}
//从队列中获得下一个数字的函数(不删除数字)
int queue_front() {
if (head.p_next == &tail) {
return -1;
}
else {
return head.p_next->num;
}
}
/*
队列测试
*/
#include <stdio.h>
#include "01queue.h"
int main() {
int num = 0;
queue_init();
for (num = 1;num <= 6;num++) {
queue_push(num);
}
printf("%d ", queue_pop());
printf("%d ", queue_pop());
printf("%d ", queue_pop());
queue_push(7);
queue_push(8);
printf("%d ", queue_pop());
printf("%d ", queue_pop());
printf("%d ", queue_pop());
printf("%d ", queue_pop());
printf("\n");
printf("有%d个数字\n", queue_size());
queue_deinit();
return 0;
}
- 消息队列-链表实现
- 双向链表实现的消息队列
- 循环消息队列实现
- ActiveMQ实现消息队列
- 消息队列实现即时通讯
- php 实现消息队列
- Redis实现消息队列
- redis实现消息队列
- redis实现消息队列
- redis实现消息队列
- 异步实现消息队列
- 消息队列实现通信
- 消息队列的实现
- Redis实现消息队列
- redis实现消息队列
- Redis实现消息队列
- 利用线程消息队列,实现任务队列
- 环形消息队列的实现
- 慕课响应式小细节
- spring与hibernate整合之HibernateDAOSupport
- Suppressed: java.lang.ClassNotFoundException: android.support.v7.internal.widget.ActionBarContainer
- HDU 2602 Bone Collector(01背包)
- ArcGIS API for Android解惑:Callout不能调整弹出框大小
- 消息队列-链表实现
- 首发
- 【路径大全】iphone所有文件路径。CYDIA
- Android网络操作和优化相关
- HDU 2602 -Bone Collector
- NodeJS 和 ThinkJS 使用时的注意点 (一)
- 缓存更新的套路
- struts2+ajaxSubmit+poi导入excel
- 循环队列