前言: 此代码可动态的构建内存队列,当队列长度达到最大长度(MAXLEN ),ac_adjust_queue函数会删除队列中无效的内存。

1、头文件 linkqueue.h

#ifndef _MEM_LINK_H
#define _MEM_LINK_H
#include <stdio.h>
#include <stdlib.h>#define OK 1
#define ERROR -1#define TRUE 1
#define FALSE 0#define OVERMEM -2
#define MAXLEN  3typedef struct FrameInfo
{unsigned int width;unsigned int height;unsigned int istate;unsigned char* memry;
}FrameInfo;typedef FrameInfo ElemType;typedef struct LinkNode {ElemType data;struct LinkNode *next;
}LinkNode, *LinkNodePtr;typedef struct {LinkNodePtr front;LinkNodePtr rear;
}LinkQueue;int ac_init_queue(LinkQueue *Q);
int av_clear_queue(LinkQueue *Q);
int ac_destroy_queue(LinkQueue *Q);
int ac_queue_empty(LinkQueue Q);
int ac_queue_lenth(LinkQueue Q);
int ac_adjust_queue(LinkQueue *Q);
int ac_get_head(LinkQueue Q, ElemType *e);
int ac_get_ready(LinkQueue Q, ElemType **e);
int ac_entry_queue(LinkQueue *Q, ElemType *e);
int ac_out_queue(LinkQueue *Q, ElemType *e);#endif

2、相应的实现文件 linkqueue.c

#include <string.h>
#include "linkqueue.h"/*初始化队列(链表)*/
int ac_init_queue(LinkQueue *Q) {//initiate an empty queueQ->front = Q->rear = (LinkNodePtr)malloc(sizeof(LinkNode));if (!Q->front)return OVERMEM;Q->front->next = NULL;return OK;
}/*清空队列*/
int av_clear_queue(LinkQueue *Q) {//clear an exitstd queueif (Q->front == NULL)return ERROR;LinkNodePtr p, q;Q->rear = Q->front;p = Q->front->next;Q->front->next = NULL;while (p) {q = p;p = p->next;free(q);}return OK;
}/*销毁队列*/
int ac_destroy_queue(LinkQueue *Q) {//destroy an existed queueif (Q->front == NULL)return ERROR;while (Q->front) {Q->rear = Q->front->next;free(Q->front);Q->front = Q->rear;}return OK;
}/*判断队列是否为空*/
int ac_queue_empty(LinkQueue Q) {//whether the queue is emptyif (Q.front == NULL)return ERROR;else if (Q.front->next == NULL)return TRUE;elsereturn FALSE;
}/*获取队列的长度*/
int ac_queue_lenth(LinkQueue Q) {//get the length of the queueif (Q.front == NULL)return ERROR;int len = 0;LinkNodePtr p;p = Q.front;while (p != Q.rear) {len++;p = p->next;}return len;
}/*调整队列,删除队列中不合要求的元素*/
int ac_adjust_queue(LinkQueue *Q){LinkNodePtr ptr;LinkNodePtr curptr, preptr;int iwidth, iheight, idx, ix, noest;int imaxc = 0, imaxidxco = 0;int iPw[MAXLEN] = { 0, 0 }, iPh[MAXLEN] = { 0, 0 }, iPc[MAXLEN] = {0, 0};ptr = Q->front;if (ac_queue_lenth(*Q) >= MAXLEN){//统计开辟的内存中,宽与高组合的概率ix = 0, noest = 0;while (ptr != Q->rear){ptr = ptr->next;if (ptr != NULL){iwidth = ptr->data.width;iheight = ptr->data.height;for (idx = 0; idx < MAXLEN; idx++){if (iPw[idx] == iwidth && iPh[idx] == iheight){iPc[idx]++;noest = 0;break;}else{noest = 1;}}if (idx == MAXLEN && noest == 1){iPw[ix] = iwidth;iPh[ix] = iheight;iPc[ix]++;ix++;}}}//寻找出现次数最多的宽高组合for (idx = 0; idx < MAXLEN; idx++){if (iPc[idx] > imaxc){imaxc = iPc[idx];imaxidxco = idx;}}//调整队列链表,    删除不合要求的内存//LinkNodePtr curptr, preptr;/*curptr = Q.front->next;if (curptr->data.width != iPw[imaxidxco] || ptr->data.height != iPh[imaxidxco]){Q.front->next = curptr->next;free(curptr);}*/preptr = Q->front;curptr = preptr->next;while (curptr != NULL){if (curptr->data.width != iPw[imaxidxco] || curptr->data.height != iPh[imaxidxco]){preptr->next = curptr->next;free(curptr->data.memry);free(curptr);curptr = preptr->next;}else{preptr = curptr;curptr = preptr->next;}}}    return 0;
}/*获取队列头元素*/
int ac_get_head(LinkQueue Q, ElemType *e) {//get the head element of the queueif (Q.front == Q.rear)return ERROR;*e = Q.front->next->data;return OK;
}/*获取队列中可用的元素*/
int ac_get_ready(LinkQueue Q, ElemType **e){LinkNodePtr ptr;if (Q.front == NULL)return ERROR;ptr = Q.front;while (ptr != Q.rear ){ptr = ptr->next;if (ptr->data.istate == 0)break;}if (ptr != Q.rear || ptr->data.istate == 0){*e = &ptr->data;return OK;}else{return FALSE;}
}/*元素入队列*/
int ac_entry_queue(LinkQueue *Q, ElemType *e) {//input an element e as the new rear of the queueLinkNodePtr p;p = (LinkNodePtr)malloc(sizeof(LinkNode));if (!p)return OVERMEM;p->data.memry = e->memry;//(unsigned char*)malloc(e.width * e.height * sizeof(unsigned char));p->data.width = e->width;p->data.height = e->height;p->data.istate = e->istate;p->next = NULL;Q->rear->next = p;Q->rear = p;return OK;
}/*元素出队列*/
int ac_out_queue(LinkQueue *Q, ElemType *e) {//delete an element which was the head of the queueif (Q->front == Q->rear)return ERROR;LinkNodePtr p;p = Q->front->next;*e = p->data;Q->front->next = p->next;if (Q->rear == p)Q->rear = Q->front;free(p);return OK;
}

3、主函数main.c

#include "linkqueue.h"int main() {ElemType in;ElemType *ot = NULL;LinkQueue *q1 = (LinkQueue*)malloc(sizeof(LinkQueue));ac_init_queue(q1);GETMEM:if (ac_queue_empty(*q1) == 1){in.width = 1281;in.height = 720;in.istate = 1;in.memry = (unsigned char*)malloc(in.width * in.height * sizeof(unsigned char));memset(in.memry, 0, in.width * in.height);ac_entry_queue(q1, &in);}else{if (ac_get_ready(*q1, &ot) != 1)//队列中无可用内存{in.width = 1280;in.height = 720;in.istate = 1;in.memry = (unsigned char*)malloc(in.width * in.height * sizeof(unsigned char));ac_entry_queue(q1, &in);}}in.width = 1280;in.height = 721;in.istate = 0;in.memry = (unsigned char*)malloc(in.width * in.height * sizeof(unsigned char));ac_entry_queue(q1, &in);in.width = 1280;in.height = 720;in.istate = 0;in.memry = (unsigned char*)malloc(in.width * in.height * sizeof(unsigned char));ac_entry_queue(q1, &in);ac_adjust_queue(q1);if (ac_get_ready(*q1, &ot) != 1){printf("can not get memory!\n");return ERROR;}if (ot->width != 1280 || ot->height != 720){ot->istate = 2;goto GETMEM;}return 0;
}

参考网址:http://blog.csdn.net/went2011/article/details/6929509

队列-C语言-链表的实现方式相关推荐

  1. 链表模拟队列quene---C语言

    1.链表分为带头结点.不带头结点两种: 2.头结点的数据域不存放数据: 3.链表增加头结点的原因:使往第一个位置插入和删除元素的操作和其他位置一样: 4.常见的会考到: ①手写链表. ②实现链表的创建 ...

  2. 如何建立队列c语言_什么是优先队列

    前言 我们之前已经介绍过队列-C语言实现,它们是先入先出的,这很容易用平常的排队来理解.但是如果这个队列要支持有紧急情况的人先出队呢?原先那种队列就不再适用了,我们需要使用本文所提到的特殊队列--优先 ...

  3. 循环队列–C语言实现–数据结构

    循环队列–C语言实现–数据结构 目录 循环队列C语言实现数据结构 目录 一 要求 二 循环队列 三 循环队列的算法设计 1 建立循环队列 2 置空队列 3 入队 4 出队 5 打印队 四 程序 1 程 ...

  4. 是栈还是队列c语言实验报告怎么写,队列和栈(C语言)

    栈和队列的基本性质 栈是先进后出的结构(弹夹) 队列是先进先出的(排队) 栈和队列在实现结构上可以有数组和链表两种方式 栈结构的基本操作: 1.弹栈 2.访问栈顶元素 3.压栈操作 4.返回当前栈中的 ...

  5. 创建队列 c语言_在C中创建队列

    创建队列 c语言 A queue in C is basically a linear data structure to store and manipulate the data elements ...

  6. 一步一步教你从零开始写C语言链表(超详细)

    STM32 HAL开发完全指南 写文章 一步一步教你从零开始写C语言链表(超详细) 杨源鑫 嵌入式系统工程师.物联网创业合伙人,业务经理兼产品经理 285 人赞同了该文章 为什么要学习链表? 链表主要 ...

  7. c语言链表ppt,C语言链表ppt课件.ppt

    C语言链表ppt课件.ppt 第十一章 链表,1,例跳马.依下图将每一步跳马之后的位置x,y放到一个"结点"里,再用"链子穿起来",形成一条链,相邻两结点间用一 ...

  8. 循环队列c语言的实现,循环队列的C语言实现

    生活中有很多队列的影子,比如打饭排队,买火车票排队问题等,可以说与时间相关的问题,一般都会涉及到队列问题:从生活中,可以抽象出队列的概念,队列就是一个能够实现"先进先出"的存储结构 ...

  9. [转载 整理]C语言链表实例

    C语言链表有单链表.双向链表.循环链表.单链表由数据域和指针域组成,数据域存放数据,指针域存放该数据类型的指针便于找到下一个节点.双链表则含有头指针域.数据域和尾指针域,域单链表不同,双链表可以从后一 ...

最新文章

  1. 大数据的“平民化”、“流动化”、“商业化”推动企业升级与转型
  2. IE10访问apache 2.4会奇慢的解决办法
  3. 量子计算机到底神在哪里说明文,“九章”量子计算机到底有多神!
  4. ZOJ18th省赛 Lucky 7
  5. CCNA 第二学期答案
  6. Javascript 学习笔记 2: 标识语句
  7. Docker+Redis镜像的原理以及部署安装(超详解附截图)
  8. IBASE category 设置为01的情况下 IBASE自动创建情况
  9. Python多线程豆瓣影评API接口爬虫
  10. 清洁单元测试图案–演示幻灯片
  11. SiameseRPN详解
  12. 常用Flex IOC框架比较分析【转载】
  13. AD19一键同时修改PCB各元器件黄色标志字符的尺寸(大了太占空间改小一点
  14. 20155319 2016-2017-2 《Java程序设计》第5周学习总结
  15. linux 内核 内存管理 bootmem alloctor 申请内存
  16. vue播放报警音实现过程
  17. 微分方程中解、特解、通解的区别
  18. Python基础知识——字符串:format() 字符串的格式化
  19. 《The Elder Scrolls V: Skyrim》百般冷门却强力职业
  20. 百钱买百鸡,公鸡五元一只,母鸡三元一只,小鸡一元三只

热门文章

  1. python调用metasploit自动攻击_Python脚本与Metasploit交互进行自动永恒之蓝攻击-Go语言中文社区...
  2. 介绍计算机的英文文章,计算机方面的英语资料,介绍一些计算机的英语短文,有兴趣的可以看...
  3. P2617 Dynamic Rankings
  4. KubeSphere配置应用路由
  5. java 内存泄露 书籍_[Java教程]一次艰难的内存泄露排查,BeanUtils 的锅
  6. 运行jar中某个类的main方法
  7. vue ---- ref
  8. 学习笔记之lvm基本应用及其扩展和缩减实现
  9. 蓝宝石显卡bios_狼神矿卡烤机89°C!强刷蓝宝石RX570超白金显卡BIOS降温75°教程...
  10. HTTP 如何传输大文件