顺序表和链表详解

目录

文章目录

一、线性表

二、顺序表

2.1 顺序表的概念

2.2 顺序表的分类

2.3 动态顺序表的实现

三、链表

3.1 链表的概念

3.2 链表的分类

3.3 链表的实现

3.3.1 无头单向非循环链表的实现

3.3.2 带头双向循环链表的实现

四、顺序表和链表的区别

总结


一、线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串...

线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储

二、顺序表

2.1 顺序表的概念

概念:顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

2.2 顺序表的分类

顺序表一般可以分为:

1. 静态顺序表:使用定长数组存储元素。

2. 动态顺序表:使用动态开辟的数组存储。

        静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组导致N定大了,空 、间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间 大小,所以下面我们实现动态顺序表。

2.3 动态顺序表的实现

下面是动态顺序表结构和各个操作函数的声明:

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* a;
	int sz;       //有效数据
	int capacity; //空间容量
}SL;

//初始化和销毁
void SLInit(SL* psl);
void SLDestory(SL* psl);

//检查和打印
void SLCheckCapacity(SL* psl);
void SLPrint(SL* psl);

//头尾插入删除
void SLPushBack(SL* psl, SLDataType x);
void SLPushFront(SL* psl, SLDataType x);
void SLPopBack(SL* psl);
void SLPopFront(SL* psl);

//任意下标位置的插入删除
void SLInsert(SL* psl, int pos, SLDataType x);
void SLErase(SL* psl, int pos);

//查找
int SLFind(SL* psl, SLDataType x);

下面是各个操作函数的实现:

void SLInit(SL* psl)
{
	assert(psl);

	psl->a = NULL;
	psl->sz = 0;
	psl->capacity = 0;
}

void SLDestory(SL* psl)
{
	assert(psl);

	if (psl->a)
	{
		free(psl->a);
		psl->a = NULL;
		psl->sz = 0;
		psl->capacity = 0;
	}
}

void SLCheckCapacity(SL* psl)
{
	assert(psl);

	if (psl->sz == psl->capacity)
	{
		int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(psl->a, newCapacity * sizeof(SLDataType*));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		psl->a = tmp;
		psl->capacity = newCapacity;
	}
}

void SLPrint(SL* psl)
{
	assert(psl);

	for (int i = 0; i < psl->sz; i++)
	{
		printf("%d ", psl->a[i]);
	}
	puts("");
}

void SLPushBack(SL* psl, SLDataType x)
{
	assert(psl);

	SLCheckCapacity(psl);

	psl->a[psl->sz] = x;
	psl->sz++;
}

void SLPushFront(SL* psl, SLDataType x)
{
	assert(psl);

	SLCheckCapacity(psl);

	int end = psl->sz;
	while (end)
	{
		psl->a[end] = psl->a[end - 1];
		end--;
	}
	psl->a[0] = x;
	psl->sz++;
}

void SLPopBack(SL* psl)
{
	assert(psl);

	// 空
	// 温柔的检查
	/*if (psl->size == 0)
	{
		return;
	}*/

	// 暴力检查
	assert(psl->sz > 0);

	psl->sz--;
}

void SLPopFront(SL* psl)
{
	assert(psl);

	assert(psl->sz > 0);

	int begin = 1;
	while (begin < psl->sz)
	{
		psl->a[begin - 1] = psl->a[begin];
		begin++;
	}
	psl->sz--;
}

//注意pos是下标
//size是数据个数,看做下标的话,他是最后一个数据的下一个位置
void SLInsert(SL* psl, int pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->sz);

	int end = psl->sz - 1;
	while (end >= pos)
	{
		psl->a[end + 1] = psl->a[end];
		end--;
	}

	psl->a[pos] = x;
	psl->sz++;
}

void SLErase(SL* psl, int pos)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->sz);

	int begin = pos;
	while (begin < psl->sz - 1)
	{
		psl->a[begin] = psl->a[begin + 1];
		begin++;
	}

	psl->sz--;
}

int SLFind(SL* psl, SLDataType x)
{
	assert(psl);

	for (int i = 0; i < psl->sz; i++)
	{
		if (psl->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}

三、链表

3.1 链表的概念

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表 中的指针链接次序实现的 。

现实中:

数据结构中:

3.2 链表的分类

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

1.单向或者双向

2.带头(哨兵位)或者不带头

3.循环或者非循环

虽然有这么多的链表的结构,但是我们实际中最常用的还是两种结构:

1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。

3.3 链表的实现

3.3.1 无头单向非循环链表的实现

下面是无头单向非循环链表结构和各个操作函数的声明:

typedef int SListDataType;

//Single List
typedef struct SListNode
{
	SListDataType val;
	struct SListNode* next;
}SLNode;

void SListPrint(SLNode* phead);

void SListPushBack(SLNode** pphead, SListDataType x);
void SListPushFront(SLNode** pphead, SListDataType x);

void SListPopBack(SLNode** pphead);
void SListPopFront(SLNode** pphead);

SLNode* SListFind(SLNode* phead, SListDataType x);

//因为单链表在pos位置之后插入x和删除pos位置之后的值较为简单,
//所以我们就实现较为复杂的在pos的前面插入和删除pos位置的值。

//在pos的前面插入
void SListInsert(SLNode** pphead, SLNode* pos, SListDataType x);

//删除pos位置的值
void SListErase(SLNode** pphead, SLNode* pos);

void SListDestroy(SLNode** pphead);

下面是各个操作函数的实现:

void SListPrint(SLNode* phead)
{
	SLNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}

SLNode* CreateNode(SListDataType x)
{
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	
	newnode->next = NULL;
	newnode->val = x;
	return newnode;
}

void SListPushBack(SLNode** pphead, SListDataType x)
{
	SLNode* newnode = CreateNode(x);

	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}

		tail->next = newnode;
	}
}

void SListPushFront(SLNode** pphead, SListDataType x)
{
	SLNode* newnode = CreateNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

void SListPopBack(SLNode** pphead)
{
	//温柔的检查
	//if (*pphead == NULL)
	//	return;
	// 
	// 空
	assert(*pphead);

	//1、一个节点
	//2、一个以上节点
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		//方法一
		/*SLNode* prev = NULL;
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		prev->next = NULL;*/
		
		//方法二
		SLNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}

void SListPopFront(SLNode** pphead)
{
	assert(*pphead);

	SLNode* tmp = (*pphead)->next;
	free(*pphead);
	*pphead = tmp;
}

SLNode* SListFind(SLNode* phead, SListDataType x)
{
	SLNode* cur = phead;
	while (cur != NULL)
	{
		if (cur->val == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void SListInsert(SLNode** pphead, SLNode* pos, SListDataType x)
{
	assert(*pphead);

	SLNode* newnode = CreateNode(x);
	newnode->next = pos;

	if (*pphead == pos)
	{
		*pphead = newnode;
	}
	else
	{
		SLNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
	}
}

void SListErase(SLNode** pphead, SLNode* pos)
{
	assert(*pphead);

	if (*pphead == pos)
	{
		free(pos);
		*pphead = NULL;
	}
	else
	{
		SLNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
	}
}

void SListDestroy(SLNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	SLNode* cur = *pphead;
	while (cur)
	{
		SLNode* tmp = cur->next;
		free(cur);
		cur = tmp;
	}
	
	*pphead = NULL;
}

3.3.2 带头双向循环链表的实现

下面是带头双向循环链表结构和各个操作函数的声明:

typedef int LTValType;

typedef struct ListNode
{
	struct ListNode* next;
	struct ListNode* prev;
	LTValType val;
}LTNode;

LTNode* LTInit();

void LTPrint(LTNode* phead);

void LTPushBack(LTNode* phead, LTValType x);

void LTPopBack(LTNode* phead);

void LTPushFront(LTNode* phead, LTValType x);

void LTPopFront(LTNode* phead);

//双向链表查找
LTNode* LTFind(LTNode* phead, LTValType x);

//双向链表在pos的前面进行插入
void LTInsert(LTNode* pos, LTValType x);

//双向链表删除pos位置的节点
void LTErase(LTNode* pos);

void LTDestroy(LTNode* pHead);

下面是各个操作函数的实现:

LTNode* CreatLTNode(LTValType x)
{
	LTNode* newNode = (LTNode*)malloc(sizeof(LTNode));
	if (newNode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newNode->val = x;
	newNode->next = NULL;
	newNode->prev = NULL;

	return newNode;
}

LTNode* LTInit()
{
	LTNode* phead = CreatLTNode(-1);
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

void LTPrint(LTNode* phead)
{
	assert(phead);
	
	LTNode* cur = phead->next;
	
	while (cur != phead)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	puts("");
}

void LTPushBack(LTNode* phead, LTValType x)
{
	assert(phead);

	/*LTNode* newNode = CreatLTNode(x);
	LTNode* tail = phead->prev;

	tail->next = newNode;
	newNode->prev = tail;
	phead->prev = newNode;
	newNode->next = phead;*/

	LTInsert(phead, x);
}

void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	/*LTNode* tailPrev = phead->prev->prev;

	free(phead->prev);
	tailPrev->next = phead;
	phead->prev = tailPrev;*/

	LTErase(phead->prev);
}

void LTPushFront(LTNode* phead, LTValType x)
{
	assert(phead);
	
	//LTNode* newNode = CreatLTNode(x);
	LTNode* headNext = phead->next; //不使用额外的变量也行

	//phead->next->prev = newNode;
	//newNode->next = phead->next;
	//newNode->prev = phead;
	//phead->next = newNode;

	LTInsert(phead->next, x);
}

void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	/*LTNode* headNext = phead->next;

	phead->next = headNext->next;
	headNext->next->prev = phead;
	free(headNext);*/

	LTErase(phead->next);
}

LTNode* LTFind(LTNode* phead, LTValType x)
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->val == x)
		{
			return cur;
		}
		cur = cur->next;
	}

	return NULL;
}

void LTInsert(LTNode* pos, LTValType x)
{
	assert(pos);

	LTNode* newNode = CreatLTNode(x);
	LTNode* posPrev = pos->prev;

	posPrev->next = newNode;
	newNode->prev = posPrev;
	newNode->next = pos;
	pos->prev = newNode;
}

void LTErase(LTNode* pos)
{
	assert(pos);

	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
}

void LTDestroy(LTNode* phead)
{
	assert(phead);

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* next = cur->next;
		free(cur);
		cur = next;
	}

	free(phead);
}

 

四、顺序表和链表的区别

不同点

顺序表

链表

存储空间上

物理上一定连续

逻辑上连续,但物理上不一定连续

随机访问

支持O(1)

不支持:O(N)

任意位置插入或者删除元素

可能需要搬移元素,效率低O(N)

只需修改指针指向

插入

动态顺序表,空间不够时需要扩容

没有容量的概念

应用场景

元素高效存储+频繁访问

任意位置插入和删除频繁

缓存利用率

备注:缓存利用率参考存储体系结构 以及 局部原理性。


总结

以上就是今天要讲的内容,本文详细介绍了数据结构中的顺序表和链表,分别从概念入手,让我们先对其有一个认识,然后再介绍其分类,最后代码实现,逐步推进,层次分明。

比较基本的功能 #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct node{ char d; struct node *next; } node; node *findalpha(node *sor) /*提取字母*/ { node *nea; if (sor==NULL) { return NULL; } else if (isalpha(sor->d)) { nea=malloc(sizeof(node)); nea->d=sor->d; nea->next=findalpha(sor->next); } else return findalpha(sor->next); return nea; } node *finddigit(node *sor) /*提取数字*/ { node *nea; if (sor==NULL) { return NULL; } else if (isdigit(sor->d)) { nea=malloc(sizeof(node)); nea->d=sor->d; nea->next=finddigit(sor->next); } else return finddigit(sor->next); return nea; } node *findother(node *sor) /*提取其它字符*/ { node *nea; if (sor==NULL) { return NULL; } else if (!isdigit(sor->d)&&!isalpha(sor->d)) { nea=malloc(sizeof(node)); nea->d=sor->d; nea->next=findother(sor->next); } else return findother(sor->next); return nea; } int main(int argc, char* argv[]) { node *a=NULL,*b,*st,*alpha,*digit,*other; char c; while ((c=getchar())!='\n') /*读取字符并建表*/ { if (a==NULL) { a=malloc(sizeof(node)); st=a; a->d=c; } else{ b=malloc(sizeof(node)); b->d=c; a->next=b; a=b; } } a->next =NULL; a=st; puts("\nAll:"); while (a!=NULL) /*输出所有字符*/ { printf("%c",a->d); a=a->next ; } alpha=findalpha(st); digit=finddigit(st); other=findother(st); puts("\n\nLetter:"); while (alpha!=NULL) { printf("%c",alpha->d); alpha=alpha->next ; } puts("\n\nDigit:"); while (digit!=NULL) { printf("%c",digit->d); digit=digit->next ; } puts("\n\nOther:") ; while (other!=NULL) { printf("%c",other->d); other=other->next ; } return 0; }
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月亮有痕迹诶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值