C语言学习--链式结构

结构体的应用:

//数据结构与算法

数据结构 ---- 指的是 数据的组织形式

数组 --- 数据结构

数组特点

连续性,有序性,单一性

数据操作(访问)时的特点


数组:

数据结构体---操作时候的特点, //特点决定他应用的场合

优势, s[i] 随机访问(随机存取)---> 存 拿数据很方便

不足: 插入数据 删除数据 不方便

链表

链式数据结构;

struct stu s1;

struct stu s2;

struct stu s3;

s1->s2->s3

特点:

优势:增加和删除数据 很方便

不足:存和取数据的时候不方便,没有数组方便

数据

指针

节点

struct Node

{

int data; //数据域 --存储要处理的数据

struct Node*next; //保存地址--指向下一个节点

}; //节点类型

struct Node n1;

struct Node n2;

struct Node n3;

链式存储的样子:

数据n1\] \[数据n2\] \[数据n3

指针\&n2\] \[指针\&n3\] \[指针 xxx

n1 n2 n3


有头链表和无头链表

无头--第一个节点数据与为随机值或者无效值

有头--第一个节点不存有效数据 //作用:为了更方便的操作链表

C语言中 ---> 主要研究 "有头单项链表"

头节点 ---数据域值随机

首节点 ---第一个保存有效数据的节点

尾节点 ---链表的最后一个有效节点 NULL。

操作: //数据结构体 数据的处理 --> 增删改查

1、创建一个链表

//空链表 -- 只有头节点 ,但没有有效的数据节点

struct Node *createEmptyLinklist(struct Node **head)

{

//struct Node *head = malloc(sizeof(struct Node));

*head = malloc(sizeof(struct Node));

(*head)->next = NULL;

return *head;

}

头加

void pushFront(struct Node *head,int dt)

{

struct Node *new = malloc(sizeof(struct Node ));

new->data = dt;

//s2

new->next = head->next;

//s3

head->next = new;

}

尾加

void pushBack(struct Node *head,int dt)

{

//s1

struct Node *new = malloc(sizeof(struct Node));

new->data = dt;

//s2

struct Node *p = head;

while (p->next!=NULL)

{

p = p->next;

}

//s3

p->next = new;

new->next = NULL;

}

头删

void popFront(struct Node *head)

{

struct Node *p = head->next;

head->next = p->next;

free(p);

}

尾删

void pushBack(struct Node *head,int dt)

{

//s1

struct Node *new = malloc(sizeof(struct Node));

new->data = dt;

//s2

struct Node *p = head;

while (p->next!=NULL)

{

p = p->next;

}

//s3

p->next = new;

new->next = NULL;

}

销毁链表:

void destroyLinklist(struct Node **head)

{

while (isEmpty(*head) == 0)

{

popFront(*head);

}

free(*head);

*head=NULL;

}

计算链表长度:

int length(struct Node *head)

{

struct Node *p = head->next;

int cnt = 0;

while (p != NULL)

{

cnt++;

p = p->next;

}

return cnt;

}

输出链表数据:

void printLinklist(struct Node *head)

{

struct Node *p = head->next; //

while ( p!= NULL)

{

printf("%d\n",p->data);

p = p->next;

}

}

相关推荐
咸甜适中7 分钟前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Magnetic_h1 小时前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
hdsoft_huge1 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘2 小时前
39.网络流入门
开发语言·网络·c++·算法
研梦非凡2 小时前
ICCV 2025|从粗到细:用于高效3D高斯溅射的可学习离散小波变换
人工智能·深度学习·学习·3d
未来之窗软件服务2 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
小冯记录编程3 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
limengshi1383923 小时前
机器学习面试:请介绍几种常用的学习率衰减方式
人工智能·学习·机器学习
1uther3 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
知识分享小能手3 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue