前言: 此代码可动态的构建内存队列,当队列长度达到最大长度(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. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2
  2. FTP,SFTP,FTPS三个文件传输协议的区别
  3. PAT甲级1025 PAT Ranking:[C++题解]排序、结构体、排名
  4. BUU--[MRCTF2020]PixelShooter
  5. python自学到大牛_开始学习Python+一位大牛整理的Python资源
  6. python数值类型的操作_Python学习笔记,数值类型及操作
  7. 理解允许定位,音频,网络电话..
  8. python爬虫难度排行榜_无聊写了一个python爬虫程序,用来爬夕阳试炼场排行榜的...
  9. 大数据时代亟需消除八大“疑云”
  10. Java函数replaceAll 的使用
  11. 计算机共享怎么ip设置,如何设置网络打印机共享
  12. nested exception is java.lang.NumberFormatException: For input string: NaN
  13. 数据库系统概述--关系数据库标准语言SQL
  14. 物联网开发 8 MQTT 协议开发入门
  15. 计算机设计辅助 CAD 试题汇编,计算机辅助设计试题汇编-第二单元
  16. c语言写拼图游戏算法,[原创]拼图游戏移动算法,简单易懂
  17. 基于Javaweb的问卷调查系统
  18. Android 学习论坛博客及网站推荐 1
  19. fmri|SPM contrast manager
  20. SpringBoot 中使用HikariPool 报错Possibly consider using a shorter maxLifetime value.

热门文章

  1. python timer模块_Python timeit模块的使用实践
  2. 服务器java 客户端c_Java客户端和C ++服务器通过TCP套接字发送和接收
  3. Linux中的docker login 与docker logout 命令
  4. MySQL的常用SQL脚本
  5. 使用Dwr时出现java.lang.SecurityException: Access to debug pages is denied
  6. 理解Dubbo的调用流程与Dubbo多协议解析
  7. java工具类与集合类_JAVA学习---集合和工具类
  8. 集群虚拟服务器,Nginx集群 -LVS(Linux虚拟服务器)简介
  9. java图片框架_Java图片处理开源框架
  10. RasbbitMQ 交换机、路由键与队列绑定