当前位置: 首页 > article >正文

数据结构 ——链式队列

数据结构 ——链式队列

一、链式队列的定义
链式队列是通过链表实现的一种队列,它将队列的元素通过指针连接起来。链式队列不需要预先分配固定大小的存储空间,因此可以动态增长,更加灵活。
二、基本操作实现
下面是基于变长结构体的双循环链表库进行封装实现链式队列的基本操作,以工程的形式记录

//queueList.h
#ifndef QUEUELIST_H_
#define QUEUELIST_H_
#include "../change/llist_change.h"

typedef LLIST QUEUELIST;
QUEUELIST *queueList_create(int );
int queueList_en(QUEUELIST *,const void *);
int queueList_de(QUEUELIST *,void *);
void queueList_destroy(QUEUELIST *);

#endif
//queueList.c
#include<stdio.h>
#include<stdlib.h>
#include "queueList.h"
//创建
QUEUELIST *queueList_create(int size)
{
    return llist_create(size);
}
//入队
int queueList_en(QUEUELIST *ptr,const void *data)
{
    //尾插方式入队,达到先进先出效果
    return llist_insert(ptr,data,LLIST_BACKWARD);
}
static int always_match(const void *p1,const void *p2)
{
    //返回0表示永远匹配,即拿到删除的该节点
    return 0;
}
//出队
int queueList_de(QUEUELIST *ptr,void *data)
{
    //头删方式出队
    return llist_fetch(ptr,(void *)8,always_match,data);
}
//销毁
void queueList_destroy(QUEUELIST *ptr)
{
    llist_destroy(ptr);
}
//main.c
#include<stdio.h>
#include<stdlib.h>
#include "queueList.h"
#define NAMESIZE 32
struct score_st
{
    int id;
    char name[NAMESIZE];
    int math;
    int chinese;
};
static void  printf_s2(void *record)
{
    struct score_st *r=record;
    printf("%d %s %d %d\n",r->id,r->name,r->math,r->chinese);
}
int main()
{
    QUEUELIST *qu;
    struct score_st tmp;
    qu=queueList_create(sizeof(struct score_st));
    if(qu==NULL)
      return -1;
      //入队测试
    for(int i=0;i<6;i++)
    {
        tmp.id=i;
        sprintf(tmp.name,"stu%d",i);
        tmp.math=rand()%100;
        tmp.chinese=rand()%100;
        if(queueList_en(qu,&tmp)!=0)
          break;
    }
    //出队测试
    while (1)
    {
        int ret=queueList_de(qu,&tmp);
        if(ret!=0)
          break;
        printf_s2(&tmp);
    }
    queueList_destroy(qu);
    return 0;
}

http://www.kler.cn/a/428007.html

相关文章:

  • 富文本 tinyMCE Vue2 组件使用简易教程
  • SpringBoot+Vue的理解(含axios/ajax)-前后端交互前端篇
  • Git进阶之旅:.gitignore 文件
  • ROS2---基础操作
  • [HOT 100] 0003. 无重复字符的最长子串
  • 蓝桥杯之c++入门(一)【C++入门】
  • Wordpress ElementorPageBuilder插件存在文件读取漏洞(CVE-2024-9935)
  • gdb逆向调试功能太强了~
  • pycharm报错
  • centos7.9编译升级openssl和openssh的记录
  • 【JVM】JVM基础教程(一)
  • 阳光电脑公司的维修服务微信小程序ssm+论文源码调试讲解
  • 数据库原理实验实验二 SQL SERVER查询分析器的使用
  • Unity在运行状态下,当物体Mesh网格发生变化时,如何让MeshCollider碰撞体也随之实时同步变化?
  • HTTP 网络技术学习:缓存;为什么有时候出现问题要清除浏览器缓存?客户端缓存和服务端缓存是什么。
  • LLMs之Agent之Lares:Lares的简介、安装和使用方法、案例应用之详细攻略
  • PyTorch 切片运算 (Slice Operator)
  • 【人工智能】用Python构建高效的自动化数据标注工具:从理论到实现
  • MySQL 存储引擎详解
  • 负载均衡OJ项目中遇到的问题
  • 我的“ai学伴”助力“程序”迭代
  • 应用案例 | 船舶海洋: 水下无人航行器数字样机功能模型构建
  • RK3568平台(内存篇)DDR定频修改
  • OD C卷【热点网站统计】
  • 漫画之家Spring Boot应用:打造您的数字漫画馆
  • 如何从命令行和用户输入收集输入