数据结构:2_顺序表和链表

顺序表和链表

一.线性表

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

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

逻辑上是依次储存的但物理上不一定是

在这里插入图片描述

二.顺序表

1. 概念及结构

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

顺序表一般可以分为:

  • 1.静态顺序表:使用定长数组存储元素。
    在这里插入图片描述

  • 2.动态顺序表:使用动态开辟的数组存储。
    在这里插入图片描述

2. 接口实现

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

typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
   
  SLDataType* array;  // 指向动态开辟的数组
  size_t size ;       // 有效数据个数
  size_t capicity ;   // 容量空间的大小
}SeqList;
// 基本增删查改接口
// 顺序表初始化
void SeqListInit(SeqList* psl);
// 检查空间,如果满了,进行增容
void CheckCapacity(SeqList* psl);
// 顺序表尾插
void SeqListPushBack(SeqList* psl, SLDataType x);
// 顺序表尾删
void SeqListPopBack(SeqList* psl);
// 顺序表头插
void SeqListPushFront(SeqList* psl, SLDataType x);
// 顺序表头删
void SeqListPopFront(SeqList* psl);
// 顺序表查找
int SeqListFind(SeqList* psl, SLDataType x); 
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* psl, size_t pos);
// 顺序表销毁
void SeqListDestory(SeqList* psl);
// 顺序表打印
void SeqListPrint(SeqList* psl);

实现函数

typedef int SLDataType;

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

//顺序表初始化
void SLInit(SL* psl)
{
   
	assert(psl);
	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}


//顺序表摧毁
void SLDestory(SL* psl)
{
   
	assert(psl);
	if (psl != NULL)
	{
   
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

//打印
void SLPrint(SL* psl)
{
   
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
   
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

//扩容
void SLCheckCapacity(SL* psl)
{
   
	assert(psl);
	if (psl->size == psl->capacity)
	{
   
		int newcapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * newcapacity);
		if (tmp == NULL)
		{
   
			perror("realloc fail");
			return;
		}
		psl->a = tmp;
		psl->capacity = newcapacity;
	}
}

//尾插
void SLPushBack(SL* psl, SLDataType x)
{
   
	assert(psl);
	SLCheckCapacity(psl);
	psl->a[psl->size] = x;
	psl->size++;

}

//头插
void SLPushFront(SL* psl, SLDataType x)
{
   
	assert(psl);
	SLCheckCapacity(psl);
	//挪动数据
	int end = psl->size - 1;
	while (end >= 0)
	{
   
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[0] = x;
	psl->size++;
}

//尾删
void SLPopBack(SL* psl)
{
   
	assert(psl);
	//温柔的检查
	if (psl->size == 0)
	{
   
		return;
	}
	//暴力检查
	//assert(psl->size > 0);

	psl->size--;
}

//头删
void SLPopFront(SL* psl)
{
   
	assert(psl);
	//暴力检查
	assert(psl->size > 0);
	int begin = 1;
	while (begin < psl->size)
	{
   
		psl->a[begin - 1] = psl->a[begin];
		begin++;
	}
	psl->size--;
}

// 顺序表在pos下标位置插入x
void SLInsert(SL* psl, int pos, SLDataType x)
{
   
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);
	SLCheckCapacity(psl);
	//挪动数据
	int end = psl->size - 1;
	while (end >= pos)
	{
   
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[pos] = x;
	psl->size++;
}

// 顺序表删除pos下标位置的值
void SLErase(SL* psl, int pos)
{
   
	assert(psl);
	assert(pos >= 0 && pos < psl->size);
	int begin=pos+1;
	while (begin < psl->size)
	{
   
		psl->a[begin-1] = psl->a[begin];
		begin++;
	}
	psl->size--;
}

// 顺序表查找
//找到返回下标
//没有找到返回-1
int SLFind(SL* psl, SLDataType x)
{
   
	assert(psl);
	for (int i = 0; i < psl->size; i++)
	{
   
		if (psl->a[i] == x)
		{
   
			return i;
		}
	}
	return -1;
}

注意:
在这里插入图片描述

三.链表

1. 链表的概念及结构

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

在这里插入图片描述

2. 链表的分类

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

  • 1.单向或者双向
    在这里插入图片描述

  • 2.带头或者不带头
    在这里插入图片描述

  • 3.循环或者非循环
    在这里插入图片描述

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

在这里插入图片描述

  1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
  2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。

3. 单链表的实现

//Single List 单链表只适合头插
// 1、无头+单向+非循环链表增删查改实现
typedef int SLTDateType;
typedef struct SListNode
{
   
 SLTDateType data;
 struct SListNode* next;
}SListNode;
// 动态申请一个结点
SListNode* BuySListNode(SLTDateType x);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
//需要找上一个节点的地址
void SListEraseAfter(SListNode* pos);

实现函数

typedef int SLNDataType;

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

// 1、无头+单向+非循环链表增删查改实现
//打印
void SLTPrint(SLNode* phead)
{
   
	SLNode* cur = phead;
	while (cur != NULL)
	{
   
		printf("%d->", cur->val);
		cur = cur->next;
	}
	printf("NULL\n");
}

//申请节点
SLNode* CreateNode(SLNDataType x)
{
   
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
   
		perror("malloc fail");
		exit(-1);//非0表示异常退出
	}
	newnode->val = x;
	newnode->next = NULL;
	return newnode;
}

//尾插
void SLTPushBack(SLNode** pphead, SLNDataType x)
{
   
	assert(pphead);
	SLNode* newnode = CreateNode(x);
	if (*pphead == NULL)
	{
   
		*pphead = newnode;
	}
	else
	{
   
		//找尾
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
   
			tail = tail->next;
		}
		tail->next = newnode;
	}
}


//头插
void SLTPushFront(SLNode** pphead, SLNDataType x)
{
   
	assert(pphead);
	SLNode* newnode = CreateNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
//写法一
void SLTPopBack(SLNode** pphead)
{
   
	assert(*pphead);
	
	//找尾
	SLNode* prev = NULL;
	SLNode* tail = *pphead;
	while (tail->next != NULL)
	{
   
		prev = tail;
		tail = tail->next;
	}
	free(tail);
	tail = NULL;

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

//写法二
//void SLTPopBack(SLNode** pphead)
//{
   
//	assert(*pphead);
//	//1.一个节点
//	if ((*pphead)->next == NULL)
//	{
   
//		free(*pphead);
//		*pphead = NULL;
//	}
//	//2.多个节点
//	else 
//	{
   
//		//找尾
//		SLNode* prev = NULL;
//		SLNode* tail = *pphead;
//		while (tail->next != NULL)
//		{
   
//			prev = tail;
//			tail = tail->next;
//		}
//		free(tail);
//		tail = NULL;
//		prev->next = NULL;
//	}
//}
//写法三
void SLTPopBack(SLNode** pphead)
{
   
	assert(pphead);
	assert(*pphead);
	//1.一个节点
	if ((*pphead)->next == NULL)
	{
   
		free(*pphead);
		*pphead = NULL;
	}
	//2.多个节点
	else
	{
   
		//找尾
		SLNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
   
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}


//头删
void SLTPopFront(SLNode** pphead)
{
   
	assert(pphead);
	assert(*pphead);
	SLNode* tmp = *pphead;
	*pphead = (*pphead)->next;
	free(tmp);
}

//查找
SLNode* SLTFind(SLNode* phead, SLNDataType x)
{
   
	SLNode* cur = phead;
	while (cur)
	{
   
		if (cur->val == x)
		{
   
			return cur;
		}
		else 
		{
   
			cur = cur->next;
		}
	}
	return NULL;
}

//在pos的前面插入
void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x)
{
   
	//pos可为空
	//assert(pphead);
	//assert((!pos && !(*pphead)) || (pos && *pphead));//要么都是空,要么都不是空
	
	//或者严格限定pos一定是链表里面的一个有效节点
	assert(pphead);
	assert(pos);
	assert(*pphead);
	if (*pphead == pos)
	{
   
		//头插
		SLTPushFront(pphead, x);
	}
	else
	{
   
		SLNode* prev = *pphead;
		while (prev->next != pos)
		{
   
			prev = prev->next;
		}
		SLNode* newnode = CreateNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}

//删除pos位置
void SLTErase(SLNode** pphead, SLNode* pos)
{
   
	assert(pphead);
	assert(*pphead);
	assert(pos);
	if (*pphead == pos)
	{
   
		//头删
		SLTPopFront(pphead);
	}
	else
	{
   
		SLNode* prev = *pphead;
		while (prev->next != pos)
		{
   
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;
	}
}

// 单链表在pos位置之后插入x
void SLTInsertAfter(SLNode* pos, SLNDataType x)
{
   
	assert(pos);
	SLNode* newnode = CreateNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

// 单链表删除pos位置之后的值
void SLTEraseAfter(SLNode* pos)
{
   
	assert(pos);
	assert(pos->next);
	SLNode* tmp = pos->next;
	pos->next = pos->next->next;
	free(tmp);
	tmp = NULL;
}

//销毁链表
void SLNDestory(SLNode** pphead)
{
   
	assert(pphead);
	SLNode* cur = *pphead;
	while (cur)
	{
   
		SLNode* next = cur->next;
		free(cur);
		cur = next;
	}
	*pphead = NULL;
}

4. 带头双向循环链表的实现

// 2、带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
   
 LTDataType _data;
 struct ListNode* next;
 struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表销毁
void ListDestory(ListNode* plist);
// 双向链表打印
void ListPrint(ListNode* plist);
// 双向链表尾插
void ListPushBack(ListNode* plist, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* plist);
// 双向链表头插
void ListPushFront(ListNode* plist, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* plist);
// 双向链表查找
ListNode* ListFind(ListNode* plist, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的结点
void ListErase(ListNode* pos);
#include "List.h"
//创建节点
LTNode* CreatLTNode(LTDataType 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);
	printf("哨兵位<=>");
	LTNode* cur = phead->next;
	while (cur != phead)
	{
   
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}


//尾插
//写法1
//void LTPushBack(LTNode* phead, LTDataType x)
//{
   
//	assert(phead);
//	LTNode* tail = phead->prev;
//	LTNode* newnode = CreatLTNode(x);
//	tail->next = newnode;
//	newnode->prev = tail;
//	newnode->next = phead;
//	phead->prev = newnode;
//}
//写法2
void LTPushBack(LTNode* phead, LTDataType x)
{
   
	assert(phead);
	LTInsert(phead, x);
}


//尾删
//写法1
//void LTPopBack(LTNode* phead)
//{
   
//	assert(phead);
//	assert(phead->next != phead);
//	LTNode* del = phead->prev;
//	LTNode* tail = del->prev;
//	tail->next = phead;
//	phead->prev = tail;
//	free(del);
//}
//写法2
void LTPopBack(LTNode* phead)
{
   
	assert(phead);
	assert(phead->next != phead);
	LTErase(phead->prev);
}


//头插
//写法1
//void LTPushFront(LTNode* phead, LTDataType x)
//{
   
//	assert(phead);
//	LTNode* Next = phead->next;
//	LTNode* newnode = CreatLTNode(x);
//	newnode->next = Next;
//	newnode->prev = phead;
//	phead->next = newnode;
//	Next->prev = newnode;
//}
//写法2
void LTPushFront(LTNode* phead, LTDataType x)
{
   
	assert(phead);
	LTInsert(phead->next, x);
}


//头删
//写法1
//void LTPopFront(LTNode* phead)
//{
   
//	assert(phead);
//	assert(phead->next != phead);
//	LTNode* del = phead->next;
//	LTNode* Next = del->next;
//	phead->next = Next;
//	Next->prev = phead;
//	free(del);
//}
//写法2
void LTPopFront(LTNode* phead)
{
   
	assert(phead);
	assert(phead->next != phead);
	LTErase(phead->next);
}


//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
   
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
   
		if (cur->val == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}


//在pos位置前插入
void LTInsert(LTNode* pos, LTDataType x)
{
   
	assert(pos);
	LTNode* Prev = pos->prev;
	LTNode* newnode = CreatLTNode(x);
	Prev->next = newnode;
	newnode->prev = Prev;
	newnode->next = pos;
	pos->prev = newnode;
}


//删除pos位置
void LTErase(LTNode* pos)
{
   
	assert(pos);
	LTNode* Prev = pos->prev;
	LTNode* Next = pos->next;
	Prev->next = Next;
	Next->prev = Prev;
	free(pos);
}


//销毁链表
void LTDestory(LTNode* phead)
{
   
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
   
		LTNode* Next = cur->next;
		free(cur);
		cur = Next;
	}
	free(phead);
}

四.顺序表和链表的区别

在这里插入图片描述

在这里插入图片描述

  • 补充:单链表的CPU高速命中率比较高

在这里插入图片描述

在这里插入图片描述

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 09:12:01       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 09:12:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 09:12:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 09:12:01       18 阅读

热门阅读

  1. 大模型学习笔记07——大模型之Adaptation

    2024-01-22 09:12:01       37 阅读
  2. 20.包装类

    2024-01-22 09:12:01       26 阅读
  3. 计算矩阵边缘元素之和(c++)

    2024-01-22 09:12:01       35 阅读
  4. 泛型..

    泛型..

    2024-01-22 09:12:01      33 阅读
  5. 编程羔才生-前端访问后端出现read ECONNRESET

    2024-01-22 09:12:01       38 阅读
  6. Oracle pagesize 和 linesize区别

    2024-01-22 09:12:01       31 阅读