个人笔记2(数据结构)

目录

1.线性表

2.顺序表

3.单链表

4.双向链表


1.线性表

后面还会学二叉树、哈希表等非线性结构。

线性结构:数据是挨着放的,但在内存不一定是挨着放的,是指在结构上是挨着放的,顺序表在内存中就是挨着放的,链表不是。


2.顺序表

顺序表可以理解为数组。顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。数据必须从第一个位置开始连续存储

顺序表一般可分为:

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

在实际当中不实用,对它进行改进:

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

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

接口实现:

在每个函数起始可以加一个 assert(ps != NULL); 或 assert(ps); 是防止传一个结构体空指针,称之为防御式编程。

初始化顺序表:

void SeqListInit(SL* ps)
{
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

检查:

检查容量空间,满了要扩容

void SeqListCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
}

尾插:

void SeqListPushBack(SL* ps, SLDataType x)
{
	SeqListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

也可以复用“在任意位置插入”函数:

void SeqListPushBack(SL* ps, SLDataType x)
{
    SeqListInsert(ps, size, x);
}

尾删:

越界是不一定报错的,系统对越界的检查是设岗抽查

void SeqListPopBack(SL* ps)
{
	//assert(ps->size > 0); 暴力检查
	//可以直接知道哪行出问题
	if (ps->size == 0)
	{
		printf("SeqList is empty\n");
		return;
	}//温柔检查
	ps->size--;
}

也可以复用“删除任意位置”函数:

void SeqListPopBack(SL* ps)
{
	eqListErase(ps, ps->size - 1);
}

头插:

void SeqListPushFront(SL* ps, SLDataType x)
{
	seqlistcheckcapacity(ps);
	//挪动数据
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

 也可以复用“在任意位置插入”函数:

void SeqListPushFront(SL* ps, SLDataType x)
{
	SeqListInsert(ps, 0, x);
}

头删:

void SeqListPopFront(SL* ps)
{
	assert(ps->size > 0);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

 也可以复用“删除任意位置”函数:

void SeqListPopFront(SL* ps)
{
    SeqListErase(ps, 0);
}

打印顺序表:

void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
		printf("%d ", ps->a[i]);
	printf("\n");
}

销毁顺序表:

void SeqListDestory(SL* ps)
{
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->capacity = ps->size = 0;
	}
}

在任意位置插入:

void SeqListInsert(SL* ps, int pos, SLDataType x)
{
	assert(pos >= 0 && pos <= ps->size);
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

删除任意位置:

void SeqListErase(SL* ps, int pos)
{
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size-1)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size++;
}

查找:

int SeqListFind(SL* ps, SLDataType x)
{
	for (int i = 0; i < ps->size; i++)
		if (ps->a[i] == x)
			return i;
	return -1;
}

修改:

void SeqListModify(SL* ps, int pos, SLDataType x)
{
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}

3.单链表

顺序表优点:物理空间连续,下标随机访问

顺序表缺点:1.空间不够需要扩容,扩容有一定性能消耗,其次一般扩容2倍,存在一些空间浪费。2.头部或者中间位置插入删除效率低下。

改善方案:1.按需申请释放空间。2.头部或者中间位置插入删除不需要挪动数据。

创建结构体:

 

接口实现:

所有传二级指针的函数都应该断言一下assert(pphead); 防止误用

创建新结点:

SLTNode* BuySListNode(SListDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

尾插:

void SListPushBack(SLTNode** pphead, SListDataType x)
{
	SLTNode* newnode = BuySListNode(x);
	if (*pphead == NULL)
		*pphead = newnode;
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
			tail = tail->next;
		tail->next = newnode;
	}
}

头插:

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

尾删:

void SListPopBack(SLTNode** pphead)
{
	assert(*pphead);
	if ((*pphead)->next == NULL) //只有一个结点
	{
		free(*pphead);
		*pphead = NULL;
	}
	else //多个结点
	{
		/*SLTNode* tailPrev = NULL;
		SLTNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tailPrev = tail;
			tail = tail->next;
		}
		free(tail);
		tailPrev->next = NULL;*/
		SLTNode* tail = *pphead;
		while (tail->next->next != NULL)
			tail = tail->next;
		free(tail->next);
		tail->next = NULL;
	}
}

头删:

void SListPopFront(SLTNode** pphead)
{
	//assert(*pphead != NULL);
	if (*pphead == NULL)
		return;
	SLTNode* next = (*pphead)->next;
	free(*pphead);
	*pphead = next;
}

查找:

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

在pos位置之前插入:

void SListInsert(SLTNode** pphead, SLTNode* pos, SListDataType x)
{
	assert(pos); //如果pos等于空,就相当于尾插
	if (pos == *pphead)//头插
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SLTNode* newnode = BuySListNode(x);
		prev->next = newnode;
		newnode->next = pos;
	}
}

删除pos位置的值:

void SListErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pos);
	if (pos == *pphead)
		SListPopFront(pphead);
	else
	{
		SLTNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

在pos位置之后插入:

void SListInsertAfter(SLTNode* pos, SListDataType x)
{
	assert(pos);
	SLTNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;

	/*SLTNode* newnode = BuySListNode(x);
	SLTNode* next = pos->next;
	pos->next = newnode;
	newnode->next = next; 不在乎链接顺序*/
}

删除pos位置之后的值:

void SListEraseAfter(SLTNode* pos)
{
	assert(pos);
	if (pos->next == NULL)
		return;
	else
	{
		SLTNode* del = pos->next;
		pos->next = del->next;
		free(del);
	}
}

打印:

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

4.双向链表

双向带头结点循环链表

创建结构体:

接口实现:

 

打印:

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

	LTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

创建新结点:

LTNode* BuyListNode(LTDateType x)
{
	LTNode* node = (LTNode*)malloc(sizeof(LTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}

初始化:

LTNode* ListInit()
{
	LTNode* phead = BuyListNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

尾插:

void ListPushBack(LTNode* phead, LTDateType x)
{
	assert(phead);

	LTNode* newnode = BuyListNode(x);
	LTNode* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}

也可以复用“在pos位置之前插入”函数:

void ListPushBack(LTNode* phead, LTDateType x)
{
	ListInsert(phead, x);
}

头插:

void ListPushFront(LTNode* phead, LTDateType x)
{
	assert(phead);

	LTNode* newnode = BuyListNode(x);
	LTNode* next = phead->next;
	phead->next = newnode;
	newnode->prev = phead;
	newnode->next = next;
	next->prev = newnode;
}

也可以复用“在pos位置之前插入”函数:

void ListPushFront(LTNode* phead, LTDateType x)
{
	ListInsert(phead->next,x);
}

判断链表是否为空:

bool ListEmpty(LTNode* phead)
{
	assert(phead);

	return phead->next == phead;
}

尾删:

void ListPopBack(LTNode* phead)
{
	assert(phead);
	assert(!ListEmpty(phead));

	LTNode* tail = phead->prev;
	LTNode* tailPrev = tail->prev;
	free(tail);
	tailPrev->next = phead;
	phead->prev = tailPrev;
}

也可以复用“删除pos位置的结点”函数:

void ListPopBack(LTNode* phead)
{
	ListErase(phead->prev);
}

头删:

void ListPopFront(LTNode* phead)
{
	assert(phead);
	assert(!ListEmpty(phead));

	LTNode* next = phead->next;
	LTNode* nextNext = next->next;
	free(next);
	phead->next = nextNext;
	nextNext->prev = phead;
}

也可以复用“删除pos位置的结点”函数:

void ListPopFront(LTNode* phead)
{
	ListErase(phead->next);
}

在pos位置之前插入:

void ListInsert(LTNode* pos, LTDateType x)
{
	assert(pos);

	LTNode* newnode = BuyListNode(x);
	LTNode* posPrev = pos->prev;
	posPrev->next = newnode;
	newnode->prev = posPrev;
	newnode->next = pos;
	pos->prev = newnode;
}

删除pos位置的结点:

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

	LTNode* posPrev = pos->prev;
	LTNode* posNext = pos->next;
	free(pos);
	posPrev->next = posNext;
	posNext->prev = posPrev;
}

求链表长度:

int ListSize(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	int size = 0;
	while (cur != phead)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

销毁链表: 

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

顺序表和(双向)链表的区别:

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

顺序表优点:下标随机访问、CPU高速缓存命中率高

顺序表缺点:头部或中间插入删除效率低、扩容有一定程度性能消耗(异地扩容需要拷贝完整顺序表)、可能存在一定程度空间浪费

链表优点:任意位置插入删除是O(1)、按需申请释放

链表缺点:不支持下标随机访问

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安慕蜥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值