嵌入式自学第二十二天(5.15)

顺序表和链表 优缺点

存储方式:

顺序表是一段连续的存储单元

链表是逻辑结构连续物理结构(在内存中的表现形式)不连续

时间性能,

查找顺序表O(1):下标直接查找

链表 O(n):从头指针往后遍历才能找到

插入和删除:

顺序表 O(n):需要整体元素移动后插入

链表 O(1):只需改指定位置指针指向即可。

空间性能

顺序表 需要预先分配空间,大小固定

链表, 不需要预先分配,大小可变,动态分配

循环链表

简单的来说,就是将原来单链表中最有一个元素的next指针指向第一个元素或头结点,链表就成了一个环,头尾相连,就成了循环链表。circultlar linker list。

双向链表:

如图:每个节点都包含三部分,数据,指向下个节点的指针,指向上个节点的指针。

Makefile:工程管理工具。

预处理、编译、汇编生产obj、链接

a.out:main.c ./doubleLinklist.c

gcc main.c doubleLinklist.c

Clean:

rm a.out.

保存后按make命令默认走第一条规则生成a.out

按make clean清除a.out

#代表源文件

SRC += main.c

SRC +=doulink.c

DST = app

CC = gcc

FLAG = -g

LIB=-lm

(DST):(SRC)

(CC) (SRC) (FLAG) (LIB) -o $(DST)

clean

rm $(DST)

指定文件:make -f makefile2

今天写了双向链表相关操作的函数:

#include "doulink.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct DouLinkList* CreateDouLinkList()//创建双向链表

{

struct DouLinkList* dl = (struct DouLinkList*) malloc(sizeof(struct DouLinkList));

if(NULL == dl)

{

fprintf(stderr,"CreateDouLinkList malloc\n");

return NULL;

}

dl->head = NULL;

dl->clen = 0;

return dl;

}

int InsertHeadDouLinkList(struct DouLinkList* dl, struct DATATYPE* data)//双向链表头查

{

struct DouNode * newnode = (struct DouNode*)malloc(sizeof(struct DouNode));

if(NULL == newnode)

{

fprintf(stderr,"inserthead malloc\n");

return 1;

}

memcpy(&newnode->data,data,sizeof(struct DATATYPE));

newnode->next = NULL;

newnode->prev = NULL;

newnode->next = dl->head;

if(dl->head)

{

dl->head->prev = newnode;

}

dl->head = newnode;

dl->clen++;

return 0;

}

int ShowDouLinkList(struct DouLinkList* dl,DIR dir)//双向链表遍历

{

struct DouNode* tmp = dl->head;

if( FORWADR==dir)

{

while(tmp)

{

printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);

tmp=tmp->next;

}

}

else if(BACKWADR == dir)

{

while(tmp->next)

{

tmp=tmp->next;

} // end node

while(tmp)

{

printf("%s %c %d %d\n",tmp->data.name,tmp->data.sex,tmp->data.age,tmp->data.score);

tmp=tmp->prev;

}

}

return 0;

}

int InsertTailDouLinkList(struct DouLinkList*dl,struct DATATYPE*data)//双向链表尾插

{

if(IsEmptyDouLinkList(dl))

{

return InsertHeadDouLinkList(dl,data);

}

else

{

struct DouNode* tmp = dl->head ;

while(tmp->next)

{

tmp= tmp->next;

}

struct DouNode* newnode = (struct DouNode*)malloc(sizeof(struct DouNode));

if(NULL == newnode)

{

fprintf(stderr,"InsertTailDouLinkList malloc\n");

return 1;

}

//初始化节点

memcpy(&newnode->data,data,sizeof(struct DATATYPE));

newnode->next = NULL;

newnode->prev = NULL;

// 连接链表

newnode->prev = tmp;

tmp->next = newnode;

dl->clen++;

}

return 0;

}

int InsertPosDouLinkList(struct DouLinkList*dl,struct DATATYPE*data,int pos)//双向链表指定位置插入元素

{

int len = GetSizeDouLinkList(dl);

if(pos<0 || pos>len)

{

return 1;

}

if(0 == pos)

{

return InsertHeadDouLinkList(dl,data);

}

else if(pos == len)

{

return InsertTailDouLinkList(dl,data);

}

else

{

struct DouNode* tmp = dl->head;

int i = 0 ;

for(i = 0 ;i<pos;++i)

{

tmp=tmp->next;

}

struct DouNode* newnode = (struct DouNode*)malloc(sizeof(struct DouNode));

if(NULL == newnode)

{

fprintf(stderr,"InsertposDouLinkList malloc\n");

return 1;

}

memcpy(&newnode->data , data,sizeof(struct DATATYPE));

newnode->next = NULL;

newnode->prev = NULL;

newnode->next = tmp;

newnode->prev = tmp->prev;

tmp->prev = newnode;

newnode->prev->next = newnode;

dl->clen++;

}

return 0;

}

int IsEmptyDouLinkList(struct DouLinkList*dl)//双向链表是否为空

{

return 0 == dl->clen;

}

int GetSizeDouLinkList(struct DouLinkList*dl)//双向链表长度获取

{

return dl->clen;

}

struct DouNode* FindDouLinkList(struct DouLinkList* dl,char *name)//查找元素

{

if(IsEmptyDouLinkList(dl))

{

return NULL;

}

struct DouNode* tmp = dl->head;

int len = GetSizeDouLinkList(dl);

int i = 0 ;

for(i = 0 ;i< len; ++i)

{

if(0 == strcmp(tmp->data.name,name))

{

return tmp;

}

tmp= tmp->next;

}

return NULL;

}

int ModifyDouLinkList(struct DouLinkList* dl,char *name,struct DATATYPE* data)//修改元素

{

struct DouNode* ret = FindDouLinkList(dl,name);

if(NULL == ret)

{

return 1;

}

memcpy(&ret->data,data,sizeof(struct DATATYPE));

return 0;

}

相关推荐
福尔摩斯张5 小时前
Axios源码深度解析:前端请求库设计精髓
c语言·开发语言·前端·数据结构·游戏·排序算法
在繁华处6 小时前
C语言经典算法:汉诺塔问题
c语言·算法
Bona Sun7 小时前
单片机手搓掌上游戏机(十一)—esp8266运行gameboy模拟器之硬件连接
c语言·c++·单片机·游戏机
酸钠鈀7 小时前
模拟IIC通讯 基于状态机
c语言
橘子真甜~9 小时前
C/C++ Linux网络编程6 - poll解决客户端并发连接问题
服务器·c语言·开发语言·网络·c++·poll
小年糕是糕手10 小时前
【C++】C++入门 -- 输入&输出、缺省参数
c语言·开发语言·数据结构·c++·算法·leetcode·排序算法
Star在努力12 小时前
C语言复习八(2025.11.18)
c语言·算法·排序算法
赖small强12 小时前
【Linux C/C++开发】第26章:系统级综合项目理论
linux·c语言·c++
仟濹13 小时前
【C/C++】经典高精度算法 5道题 加减乘除「复习」
c语言·c++·算法
车端域控测试工程师14 小时前
Autosar网络管理测试用例 - TC003
c语言·开发语言·学习·汽车·测试用例·capl·canoe