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

链表——C语言

一、单项不带头

#pragma once
#include<stdio.h>
#include<stdlib.h>typedef int data;
typedef struct list
{data a;struct list* next;
}list;list* buynode(data x);void pushback(list** phead,data x);void popback(list** phead);void pushfront(list** phead, data x);void popfront(list** phead);list* find(list* head,data x);void print(list* head);//在pos之前插入
void insert1(list** pphead, list* pos,data x);//在pos之后插入
void insert2(list** ppos,data x);void erase(list** phead, list* pos);void delete(list** phead);
#include"list.h"list* buynode(data x)
{list* newnode = (list*)malloc(sizeof(list));if (newnode == NULL){perror("mallpc failure");return -1;}newnode->a = x;newnode->next = NULL;return newnode;
}void pushback(list** phead, data x)
{list* cur = *(phead);while (cur->next != NULL){cur = cur->next;}list* newnode = buynode(x);cur->next = newnode;
}void print(list* head)
{list* cur = head;while (cur != NULL){printf("%d ", cur->a);cur = cur->next;}printf("\n");
}void popback(list** phead)
{list* cur = *(phead);while (cur->next->next != NULL){cur = cur->next;}free(cur->next);cur->next = NULL;
}void pushfront(list** phead, data x)
{list* cur = *(phead);list* newnode = buynode(x);newnode->next = cur;(*phead) = newnode;
}void popfront(list** phead)
{list* cur = *(phead);(*phead) = cur->next;free(cur);
}list* find(list* head,data x)
{list* cur = head;while ((cur->next) != NULL){if (cur->a == x){return cur;}cur = cur->next;}return NULL;
}void insert2(list** ppos, data x)
{list* newnode = buynode(x);list* cur = (*ppos)->next;(*ppos)->next = newnode;newnode->next = cur;
}void erase(list** phead, list* pos)
{list* cur = *(phead);while (cur->next != pos){cur = cur->next;}list* tmp = cur->next;cur->next = cur->next->next;free(tmp);
}void insert1(list** pphead, list* pos, data x)
{list* cur = *(pphead);while (cur->next == pos){cur = cur->next;}list* newnode = buynode(x);cur->next = newnode;newnode->next = pos;
}void delete(list** phead)
{list* cur = (*phead);list* tmp = NULL;while (cur->next != NULL){tmp = cur;cur = cur->next;free(tmp);}free(cur);
}
#include"list.h"int main()
{list* head = buynode(1);list** phead = &head;pushback(phead, 2);pushback(phead, 3);pushback(phead, 4);pushback(phead, 5);print(head);list* pos = find(head, 3);insert1(phead, pos, 10);insert2(phead, 20);print(head);delete(phead);return 0;
}

二、双向带头

#pragma once
#include<stdio.h>
#include<stdlib.h>typedef int data;
typedef struct list
{data a;struct list* prev;struct list* next;
}list;list* buynode(data x);list* init();void destory(list** pphead);void pushback(list** pphead, data x);void popback(list** pphead);void pushfront(list** pphead, data x);void popfront(list** pphead);list* find(list* phead, data x);void insert1(list** pphead, data x,list** pos);void insert2(list** pphead, data x,list** pos);void erase(list** pphead, list** pos);int size(list* phead);void print(list* phead);

 

#include"list.h"list* buynode(data x)
{list* newnode = (list*)malloc(sizeof(list));if (newnode == NULL){perror("malloc failure");return -1;}newnode->prev = NULL;newnode->next = NULL;newnode->a = x;return newnode;
}list* init()
{list* phead = (list*)malloc(sizeof(list));if (phead == NULL){perror("malloc failure");return -1;}phead->next = phead;phead->prev = phead;phead->a = 0;return phead;
}void destory(list** pphead)
{}void pushback(list** pphead, data x)
{list* phead = *pphead;//只含头节点if (phead->next == phead && phead->prev == phead){list* newnode = buynode(x);phead->prev = newnode;newnode->next = phead;phead->next = newnode;newnode->prev = phead;(*pphead)->a++;}//含有其他节点else{list* tail = phead->prev;list* newnode = buynode(x);tail->next = newnode;newnode->prev = tail;newnode->next = phead;phead->prev = newnode;(*pphead)->a++;}
}void print(list* phead)
{list* cur = phead->next;while (cur != phead){printf("%d ", cur->a);cur = cur->next;}printf("\n");
}int size(list* phead)
{return phead->a;
}void popback(list** pphead)
{list* phead = *pphead;list* tail = phead->prev;tail->prev->next = phead;phead->prev = tail->prev;free(tail);(*pphead)->a--;
}void pushfront(list** pphead, data x)
{list* phead = *pphead;list* newnode = buynode(x);list* tmp = phead->next;phead->next = newnode;newnode->prev = phead;newnode->next = tmp;tmp->prev = newnode;(*pphead)->a++;
}void popfront(list** pphead)
{list* phead = *pphead;list* tmp = phead->next;phead->next = tmp->next;tmp->next->prev = phead;free(tmp);(*pphead)->a--;
}list* find(list* phead, data x)
{list* cur = phead->next;while (cur != phead){if (cur->a == x){return cur;}cur = cur->next;}return NULL;
}void insert1(list** pphead,data x, list** pos)
{list* newnode = buynode(x);list* pre = (*pos)->prev;pre->next = newnode;newnode->prev = pre;newnode->next = (*pos);(*pos)->prev = newnode;(*pphead)->a++;
}void insert2(list** pphead, data x, list** pos)
{list* newnode = buynode(x);list* nex = (*pos)->next;nex->prev = newnode;newnode->next = nex;newnode->prev = (*pos);(*pos)->next = newnode;(*pphead)->a++;
}void erase(list** pphead,list** pos)
{list* pre = (*pos)->prev;list* nex = (*pos)->next;pre->next = nex;nex->prev = pre;(*pphead)->a--;
}
#include"list.h"void test1()
{list* phead = init();list** pphead = &phead;pushback(pphead, 1);pushback(pphead, 2);pushback(pphead, 3);pushback(pphead, 4);pushback(pphead, 5);pushfront(pphead, 0);list* pos = find(phead, 3);erase(pphead, &pos);print(phead);printf("%d", size(phead));
}int main()
{test1();return 0;
}

相关文章:

  • Nacos源码—5.Nacos配置中心实现分析一
  • 多功能气体检测报警系统,精准监测,守护安全
  • 数据结构——排序(万字解说)初阶数据结构完
  • Java学习手册:ORM 框架性能优化
  • Unity WebGL、js发布交互
  • Oracle OCP认证考试考点详解083系列11
  • 什么是先验?(CVPR25)Detail-Preserving Latent Diffusion for Stable Shadow Removal论文阅读
  • 面试题 03.06 动物收容所
  • Node.js入门指南:开启JavaScript全栈开发之旅
  • C++从入门到实战(十三)C++函数模板与类模板初阶讲解
  • CentOS 7 基础环境安装脚本
  • Milvus 向量数据库详解与实践指南
  • linux(centos)联网情况下部署
  • 【Prometheus】业务指标与基础指标的标签来源差异及设计解析
  • 堆与二叉树——C语言
  • 鸿蒙开发——3.ArkTS声明式开发:构建第一个ArkTS应用
  • Python爬虫(20)Python爬虫数据存储技巧:二进制格式(Pickle/Parquet)性能优化实战
  • 康养休闲旅游住宿服务实训室:构建产教融合新标杆
  • Flowable7.x学习笔记(二十一)查看我的发起
  • 【ArcGIS Pro微课1000例】0068:Pro原来可以制作演示文稿(PPT)
  • 解放军仪仗分队参加白俄罗斯纪念苏联伟大卫国战争胜利80周年阅兵活动
  • 国际足联女子世界杯再次扩军,2031年起增至48支球队
  • 5天完成1000多万元交易额,“一张手机膜”畅销海内外的启示
  • 拿出压箱底作品,北京交响乐团让上海观众享受音乐盛宴
  • 外交部:习近平主席同普京总统达成许多新的重要共识
  • 75岁亚当·费舍尔坐镇,再现80分钟马勒《第九交响曲》