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

C语言链表2

#include<stdio.h> #include<stdlib.h> struct node{ int date; struct node* next; }; struct node* creat(int info){ //创建一个节点 struct node* newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL){ printf("error\n"); exit(1); } newnode->date=info; newnode->next=NULL; return newnode; } void add(struct node** head, int info){ //在链尾接上节点 struct node* newnode=creat(info); if(*head==NULL){ *head =newnode; return; } struct node* now=*head;//当前指针不为空 while(now->next!=NULL){ now=now->next; } now->next=newnode; } void coutout(struct node* head){ //遍历链表输出 struct node* now=head; printf("following is the list:\n"); while(now!=NULL){ printf("%d ",now->date); now=now->next; } printf("\n"); } void freelist(struct node* head){ //释放内存 struct node* t; while(head!=NULL){ t=head; head=head->next; free(t); } } void insert(struct node* head,int k,int date){ //插入节点 if(head==NULL){ printf("empty\n"); return; } struct node* now=head; for(int i=1;i<k&&now!=NULL;i++){ now=now->next; } if(now==NULL){ printf("over the list\n"); return; } struct node* newnode=creat(date); newnode->next=now->next; now->next=newnode; } void deletenode(struct node** head,int k){ if(*head==NULL){ printf("the list is empty\n"); return; } struct node* now=*head; struct node* prev=NULL; for(int i=1;i<k&&now!=NULL;i++){ prev=now; now=now->next; } if(now==NULL){ printf("over the list\n"); return; } if(prev==NULL){ *head=now->next; }else{ prev->next=now->next; } free(now); } int main(){ struct node* head=NULL; int num; scanf("%d",&num); while(num--){ int t; scanf("%d",&t); add(&head,t); } coutout(head); freelist(head); }
http://www.cnnetsun.cn/news/127327.html

相关文章:

  • 蜣螂优化(DBO)算法在工程实际中求目标函数最小值的例子:压力容器设计成本最小化的4变量4约束...
  • 12、游戏内存中常见数据结构解析
  • 21、游戏响应式黑客技术全解析
  • 26、游戏隐藏与反检测技术全解析
  • Kotaemon网络安全问答:CVE漏洞快速查询
  • Kotaemon能否自动识别问题紧急程度?
  • 复杂时序场景的突围:金仓数据库是凭借什么超越InfluxDB?
  • 特价股票投资中的跨境投资策略与风险管理
  • 为分析经理制定全面的仪表板策略
  • MATLAB实现神经网络的模式识别
  • 17、在 Linux 系统中运行 Windows 程序及优化工作流
  • Kotaemon索引构建优化:FAISS vs HNSW性能对比
  • Kotaemon在低资源环境下的轻量化改造方案
  • 16、企业 Linux 桌面迁移与后台基础设施搭建指南
  • 19、数据迁移与备份:从 Windows 到 Linux 的全面指南
  • Kotaemon销售谈判策略建议:促成交易技巧
  • 特征工程中的特征构造技巧:大数据分析的创新实践
  • 32、Linux在不同场景下的应用优势与案例分析
  • 26、深入了解GNU Lesser General Public License
  • Hive实战任务 - 9.2 统计总分与平均分
  • Hive实战任务 - 9.3 实现学生信息排序和统计
  • 1、深入解析 Windows 2000 终端服务与 Citrix MetaFrame 配置
  • 10个降AI率工具推荐,本科生高效降AIGC指南
  • 8个降AI率工具推荐,本科生高效避坑指南
  • 10 个高效降AI率工具,继续教育学生必备!
  • 19、Windows 应用数据管理全解析
  • Kotaemon税务咨询助手知识图谱构建
  • linux下执行pg数据的sql文件,报错error:permission denied for schema plat
  • Kotaemon能否识别图片中的文字并进行问答?
  • Kotaemon能否支持PDF/PPT等格式直接解析?