顺序表和链表知识点

1 顺序表

顺序表是指用一段物理地址连续的空间去存储数据的线性结构。

顺序表有两种:静态顺序表,动态顺序表。

1.1 静态顺序表结构体定义

cpp 复制代码
typedef int ElemDataSL;

typedef struct SequeList {
	ElemDataSL arr[100];
	int size;
}SL;

静态顺序表在创建结构体的时候就已经把容量固定,后期空间不够,不允许增加新空间,所以并不方便实际使用。

例子:通讯录(C语言实现)-CSDN博客

1.2 动态顺序表结构体定义

cpp 复制代码
typedef int ElemDataSL;

typedef struct SequeList {
	ElemDataSL* arr;
	int size;
	int capatity;
}SL;

动态顺序表在创建结构体的时候定义了arr的指针作为一段连续内存空间的首地址,如果有需要可以用realloc开辟新空间进行扩容处理。

1.2.1 接口声明

cpp 复制代码
//初始化
void InitSequeList(SL* s);
//尾插
void SequeListPushBack(SL* s,ElemDataSL num);
//尾删
void SequeListPopBack(SL* s);
//头插
void SequeListPushFront(SL* s, ElemDataSL num);
//头删
void SequeListPopFront(SL* s);
//pos处插
void SequeListPushInsert(SL* s,ElemDataSL num,int pos);
//pos处删
void SequeListPopInsert(SL* s,int pos);
//显示
void SequeListShowData(SL* s);
// 顺序表删除pos位置的值
void SequeListErase(SL*s, size_t pos);
// 顺序表销毁
void SequeListDestory(SL* s);

1.2.2 初始化

cpp 复制代码
//初始化
void InitSequeList(SL* s) {
	assert(s);
	s->arr = (ElemDataSL*)malloc(DefaultData * sizeof(ElemDataSL));
	s->capatity = DefaultData;
	s->size = 0;
}

1.2.3 尾插

cpp 复制代码
//尾插
void SequeListPushBack(SL* s,ElemDataSL num) {
	assert(s);
	AddCapatity(s);
	s->arr[s->size] = num;
	s->size++;
}

1.2.4 尾删

cpp 复制代码
//尾删
void SequeListPopBack(SL* s)
{
    assert(s);
	s->size--;
}

1.2.5 头插

cpp 复制代码
//头插
void SequeListPushFront(SL* s,ElemDataSL num) {
	AddCapatity(s);
	for (int i = s->size-1; i >=0; i--)
	{
		s->arr[i + 1] = s->arr[i];
	}
	s->arr[0] = num;
	s->size++;
}

1.2.6 头删

cpp 复制代码
//头删
void SequeListPopFront(SL* s)
{
	for (int i = 0; i <= s->size - 1; i++)
	{
		s->arr[i] = s->arr[i + 1];
	}
	s->size--;
}

1.2.7 中间插入

cpp 复制代码
//pos处插
void SequeListPushInsert(SL* s,ElemDataSL num,int pos)
{
	AddCapatity(s);
	for (int i = s->size - 1; i >= pos-1; i--)
	{
		s->arr[i + 1] = s->arr[i];
	}
	s->arr[pos-1] = num;
	s->size++;
}

1.2.8 中间删除

cpp 复制代码
//pos处删
void SequeListPopInsert(SL* s, int pos) {
	for (int i = pos-1; i <= s->size - 1; i++)
	{
		s->arr[i] = s->arr[i + 1];
	}
	s->size--;
}

1.2.9 打印顺序表

cpp 复制代码
//显示
void SequeListShowData(SL* s)
{
	assert(s);
	for (int i = 0; i < s->size; i++)
	{
		printf("%d ", s->arr[i]);
	}
	printf("\n");
}

1.3 顺序表销毁

cpp 复制代码
// 顺序表销毁
void SequeListDestory(SL* s) {
	free(s->arr);
	s->arr = NULL;
}

顺序表优点:可以利用随机访问。

缺点:开辟空间可能会导致空间浪费,增删改查的时间复杂度为O(N)。

2 链表

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

注意:

1、链式结构在逻辑上是连续的,但在物理上非连续。

2、在堆上开辟空间。

3、在堆上申请空间,按照一定策略进行的,两次开辟的空间可能连续也可能不连续。

2.1 单链表

无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。

2.2 接口声明

cpp 复制代码
typedef int SLDATATYPE;
typedef struct SListNode {
	int data;
	struct SListNode* next;
}SListNode;
//尾插
void SListPushBack(SListNode** pphead, SLDATATYPE num);
//尾删
void SListPopBack(SListNode* phead);
//头插
void SListPushFront(SListNode** pphead, SLDATATYPE num);
//头删
void SListPopFront(SListNode* phead);
//查找
SListNode* SListFind(SListNode* phead, SLDATATYPE num);
//中间后插
void SListInsertAfter(SListNode* pos, SLDATATYPE num);
//中间删
void SListEraseAfter(SListNode* pos);
//打印
void SListPrint(SListNode* phead);
//销毁链表
void SListDistory(SListNode** phead);

2.3 尾插

cpp 复制代码
//尾插
void SListPushBack(SListNode* *pphead, SLDATATYPE num) {

	
	SListNode* NewNode = BuySListNode(num);
	if (*pphead == NULL)
	{
		*pphead= NewNode;
	}
	else
	{	SListNode* tail = *pphead;
		while (tail->next!=NULL)
		{
			tail = tail->next;
		}
		tail->next= NewNode;
	}
}

2.4 尾删

cpp 复制代码
//尾删
void SListPopBack(SListNode** phead)
{
	SListNode* tail = *phead;
	SListNode* prev = tail;
	if (*phead == NULL)
	{
		return;
	}
	else if ((*phead)->next == NULL)
	{
		free(*phead);
		*phead = NULL;
	}
	else
	{
		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		free(tail);
		prev->next = NULL;
	}
	

}

2.5 头插

cpp 复制代码
//头插
void SListPushFront(SListNode** pphead, SLDATATYPE num)
{
	SListNode* newNode = BuySListNode(num);

	if ((*pphead) == NULL)
	{
		*pphead = newNode;
	}
	else
	{
		newNode->next = *pphead;
	}
	*pphead = newNode;
}

2.6 头删

cpp 复制代码
//头删
void SListPopFront(SListNode* *pphead)
{	
	SListNode* head = *pphead;
	if (head == NULL)
	{
		return;
	}
	else if (head->next == NULL)
	{
		*pphead = NULL;
	}
	else
	{

		(*pphead) = (*pphead)->next;
		free(head);
	}
	
	
}

2.7 查找

cpp 复制代码
SListNode* SListFind(SListNode* phead, SLDATATYPE num) {
	SListNode* ptr = phead;
	while (ptr)
	{
		if (ptr->data == num)
		{
			printf("%d找到了!\n",ptr->data);
			return  ptr;
		}
		ptr = ptr->next;
	}
	printf("%d没找到!\n",num);
	return NULL;
}

2.8 中间后插

cpp 复制代码
void SListInsertAfter(SListNode* *pos, SLDATATYPE num)
{
	SListNode* newNode = BuySListNode(num);
	newNode->next = (*pos)->next;
	(*pos)->next = newNode;
}

2.9 中间后删

cpp 复制代码
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	assert(pos->next);
	SListNode* next = pos->next;
	pos->next = next->next;
	free(next);
}

3 打印

cpp 复制代码
//打印
void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

3.1 销毁链表

cpp 复制代码
void SListDistory(SListNode** phead)
{
	assert(*phead);
	SListNode* next = (*phead)->next;
	while (next)
	{
		free(*phead);
		*phead = next;
		next = next->next;
	}
	free(*phead);
	*phead = NULL;
}

单链表优点:需要增加多少个元素就开辟多少空间不会导致空间浪费,插入时间复杂度低。

缺点:不能随机访问,只能依次往后访问,无法实现从后往前。

2.2 双链表

头节点不存储有效数据。

2.2.1 接口声明

cpp 复制代码
typedef int ListData;
typedef struct ListNode
{
	ListData data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;
//链表创建
ListNode* ListCreate();
//尾插
void ListPushBack(ListNode* phead, ListData x);
//尾删
void ListPopBack(ListNode* phead);
//头插
void ListPushFront(ListNode* phead, ListData x);
//头删
void ListPopFront(ListNode* phead);
//查找
ListNode* ListFind(ListNode* phead, ListData x);
//中间插
void ListInsert(ListNode* pos,ListData x);
//中间删
void ListErase(ListNode* pos);
//显示
void ListPrint(ListNode* phead);
//清空链表
void ListClear(ListNode** phead);
//销毁链表
void ListDestory(ListNode* phead);

2.2.2 创建新节点

cpp 复制代码
ListNode* BuyListNode(ListData x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}

2.2.3 双链表创建

cpp 复制代码
ListNode* ListCreate() {
	ListNode* phead = BuyListNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

2.2.4 尾插

cpp 复制代码
//尾插
void ListPushBack(ListNode* phead, ListData x)
{
	assert(phead);
	ListNode* tail = phead->prev;
	ListNode* newNode = BuyListNode(x);
	tail->next = newNode;
	newNode->prev = tail;
	newNode->next = phead;
	phead->prev = newNode;
}

2.2.5 尾删

cpp 复制代码
//尾删
void ListPopBack(ListNode* phead)
{
	assert(phead->next != phead);
	/*ListNode* tail = phead->prev;
	ListNode* tailPrev = tail->prev;
	tailPrev->next = phead;
	phead->prev = tailPrev;*/
	ListErase(phead->prev);
}

2.2.6 头插

cpp 复制代码
//头插
void ListPushFront(ListNode* phead, ListData x)
{
	ListNode* newNode = BuyListNode(x);
	ListNode* pheadNext = phead->next;
	phead->next = newNode;
	newNode->prev = phead;
	newNode->next = pheadNext;
	pheadNext->prev = newNode;
}

2.2.7 头删

cpp 复制代码
//头删
void ListPopFront(ListNode* phead)
{
	ListNode* pheadNext = phead->next;
	phead->next = pheadNext->next;
	pheadNext->next->prev = phead;
	free(pheadNext);
}

2.2.8 查找

cpp 复制代码
//查找
ListNode* ListFind(ListNode* phead, ListData x)
{
	ListNode* pos = NULL;
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			pos = cur;
			break;
			
		}
		cur = cur->next;
	}
	return pos;
}

2.2.9 中间插

cpp 复制代码
//中间插
void ListInsert(ListNode* pos, ListData x)
{
	assert(pos);
	/*ListNode* newNode = BuyListNode(x);
	ListNode* posPrev = pos->prev;
	newNode->next = pos;
	pos->prev = newNode;
	posPrev->next = newNode;
	newNode->prev = posPrev;*/
	ListPushFront(pos->prev, x);
}

2.3 中间删

cpp 复制代码
//中间删
void ListErase(ListNode* pos)
{
	ListNode* posPrev = pos->prev;
	ListNode* posNext = pos->next;
	posPrev->next = posNext;
	posNext->prev = posPrev;
	free(pos);
}

2.3.1 显示

cpp 复制代码
//显示
void ListPrint(ListNode* phead)
{
	assert(phead);
	ListNode* tail = phead->next;
	
	while (tail != phead)
	{
		printf("%d ", tail->data);
		tail = tail->next;
	}
	
	printf("\n");
}

2.3.2 清空链表

cpp 复制代码
void ListClear(ListNode** phead)
{
	ListNode* cur = (*phead)->next;
	while (cur != *phead)
	{
		ListNode* next = cur->next;
		free(cur);
		cur = next;
	}
	(*phead)->next = *phead;
	(*phead)->prev = *phead;
}

2.3.3 销毁链表

cpp 复制代码
//销毁链表
void ListDestory(ListNode** phead)
{
	ListClear(phead);
	free(*phead);
}

双链表优点:插入时间复杂度比单链表更低,可以前驱访问。

缺点:不能随机访问。

3 顺序表和链表的区别

|--------------|------------------|----------------|
| 不同点 | 顺序表 | 链表 |
| 存储空间上 | 物理上一定连续 | 逻辑上连续,物理上不一定连续 |
| 随机访问 | 支持O(1) | 不支持O(N) |
| 任意位置插入或者删除元素 | 可能需要搬移元素,效率低O(N) | 只需修改指针指向 |
| 插入 | 动态顺序表,空间不够时需要扩容 | 没有容量概念 |
| 应用场景 | 元素高效存储+频繁访问 | 任意位置插入和删除频繁 |
| 缓存利用率 | 高 | 低 |

相关推荐
AIAdvocate1 小时前
Pandas_数据结构详解
数据结构·python·pandas
jiao000011 小时前
数据结构——队列
c语言·数据结构·算法
kaneki_lh1 小时前
数据结构 - 栈
数据结构
铁匠匠匠1 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
C-SDN花园GGbond1 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
CV工程师小林3 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z3 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
还听珊瑚海吗3 小时前
数据结构—栈和队列
数据结构
Aic山鱼3 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
Styal_Amy4 小时前
PDF产品册营销推广利器FLBOOK
学习方法