嵌入式自学第二十二天(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;

}

相关推荐
liulilittle1 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
V我五十买鸡腿3 小时前
顺序栈和链式栈
c语言·数据结构·笔记·算法
森焱森11 小时前
水下航行器外形分类详解
c语言·单片机·算法·架构·无人机
small_wh1te_coder15 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
黑听人19 小时前
【力扣 简单 C】70. 爬楼梯
c语言·leetcode
杜子不疼.19 小时前
二分查找,乘法口诀表,判断闰年,判断素数,使用函数实现数组操作
c语言
呜喵王阿尔萨斯1 天前
编程中的英语
c语言·c++
only-lucky1 天前
C语言socket编程-补充
服务器·c语言·php
JeffersonZU1 天前
Linux/Unix进程概念及基本操作(PID、内存布局、虚拟内存、环境变量、fork、exit、wait、exec、system)
linux·c语言·unix·gnu