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

数据结构day5:单向循环链表 代码作业

一、loopLink.h

#ifndef __LOOPLINK_H__
#define __LOOPLINK_H__#include <stdio.h>
#include <stdlib.h>typedef int DataType;typedef struct node
{union{int len;DataType data;};struct node* next;
}loopLink, *loopLinkPtr;//创建
loopLinkPtr create();//判空
int empty(loopLinkPtr H);//尾插
int tail_add(loopLinkPtr H, DataType e);//遍历
void show(loopLinkPtr H);//尾删
int tail_del(loopLinkPtr H);//销毁
void my_free(loopLinkPtr H);#endif

二、loopLink.c

#include "loopLink.h"//创建
loopLinkPtr create()
{loopLinkPtr H = (loopLinkPtr)malloc(sizeof(loopLink));if(NULL == H){printf("创建失败!\n");return NULL;}H->len = 0;H->next = H;printf("创建成功!\n");return H;
}//判空
int empty(loopLinkPtr H)
{if(NULL == H){printf("判空失败!\n");return -1;}return H->len==0;
}//尾插
int tail_add(loopLinkPtr H, DataType e)
{if(NULL == H){printf("尾插失败!\n");return 0;}loopLinkPtr p = (loopLinkPtr)malloc(sizeof(loopLink));if(NULL == p){printf("申请节点失败!\n");return 0;}p->data = e;p->next = NULL;loopLinkPtr q = H;while(q->next != H){q = q->next;}q->next = p;p->next = H;H->len++;return 1;
}//遍历
void show(loopLinkPtr H)
{if(NULL == H || empty(H)){printf("遍历失败!\n");return ;}loopLinkPtr p = H;for(int i=0; i<H->len; i++){p = p->next;printf("%d ", p->data);}printf("\n");
}//尾删
int tail_del(loopLinkPtr H)
{if(NULL == H || empty(H)){printf("删除失败!\n");return 0;}loopLinkPtr q = H;for(int i=0; i<H->len-1; i++){q = q->next;}free(q->next);q->next = H;H->len--;return 1;
}//销毁
void my_free(loopLinkPtr H)
{if(NULL == H){printf("销毁失败!\n");return ;}while(H->next != H){tail_del(H);}free(H);H=NULL;printf("销毁成功!\n");
}

三、main.c

#include "loopLink.h"int main()
{loopLinkPtr H = create();tail_add(H, 10);tail_add(H, 20);tail_add(H, 30);tail_add(H, 40);tail_add(H, 50);show(H);//尾删tail_del(H);show(H);//销毁my_free(H);H=NULL;return 0;
}

四、执行结果

五、知识点思维导图

http://www.lryc.cn/news/507320.html

相关文章:

  • (OCPP服务器)SteVe编译搭建全过程
  • Mybatis分页插件的使用问题记录
  • 36. Three.js案例-创建带光照和阴影的球体与平面
  • CentOS 7 安装、测试和部署FastDFS
  • 全志H618 Android12修改doucmentsui选中图片资源详情信息
  • 【083】基于51单片机智能烘手器【Proteus仿真+Keil程序+报告+原理图】
  • uniApp使用腾讯地图提示未添加maps模块
  • 未来趋势系列 篇五:自主可控科技题材解析和股票梳理
  • Springboot 学习 之 logback-spring.xml 日志压缩 .tmp 临时文件问题
  • maven-resources-production:ratel-fast: java.lang.IndexOutOfBoundsException
  • K8s docker-compose的入门
  • 去雾Cycle-GAN损失函数
  • word实现两栏格式公式居中,编号右对齐
  • vtie项目中使用到了TailwindCSS,如何打包成一个单独的CSS文件(优化、压缩)
  • shell脚本案例
  • 完整微服务设计 功能实现
  • JWT令牌与微服务
  • C# WinForm移除非法字符的输入框
  • 智慧商城:基于请求数据动态渲染购物车列表
  • 医疗信息化浪潮下 SSM+Vue 医院预约挂号系统的崛起
  • QScreen在Qt5.15与Qt6.8版本下的区别
  • 模具生产过程中的标签使用流程图
  • Unity-URP设置单独渲染UI相机
  • 如何使用java来解析一个pdf文件呢?
  • asp.net core发布配置端口号,支持linux
  • M3D: 基于多模态大模型的新型3D医学影像分析框架,将3D医学图像分析从“看图片“提升到“理解空间“的层次,支持检索、报告生成、问答、定位和分割等8类任务
  • JavaScript中,常用crypto模块进行rsa加密,crypto-js模块进行md5算法
  • 机器学习04-为什么Relu函数
  • 基于Arduino的自动开瓶系统
  • 通过使用 contenteditable=“true“,我们彻底防止了 iOS 系统键盘的弹出