既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
if (\*pphead == NULL)
{
\*pphead = newNode; //头结点即为新结点
}
else
{
//找尾
SListNode\* tail = \*pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newNode;
}
}
##### 尾删
**分三种情况**:
1. 链表为空
2. 只有一个节点
3. 有一个以上结点
对于第三种情况,当把结点free掉后,该结点的前一个节点未能指向NULL,因此该节点还能被访问,造成错误,要解决这种情况,不仅要找到尾结点,还要找到尾结点的前一个结点,那就要再定义多一个指针,在对尾结点进行删除之前,用于保存tail的的前一个结点
![在这里插入图片描述](https://img-blog.csdnimg.cn/443a91e372a24976b426c717714479d6.gif#pic_center)
###### 尾删完整代码如下
void SListPopBack(SListNode** pphead)
{
if (*pphead == NULL) //链表为空
{
return;
}
else if((*pphead)->next==NULL) //只有一个结点
{
free(*pphead); //删除结点
*pphead = NULL; //头指针置空
}
else //有一个以上结点
{
SListNode* prev = NULL;
SListNode* tail = *pphead; //找尾
while (tail->next != NULL)
{
prev = tail; //用于保存tail指向的上一个结点
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
##### 头插
基本思路:
1. 开辟一个新节点并指向原来头节点的地址
2. 再让头指针指向新的头结点
![在这里插入图片描述](https://img-blog.csdnimg.cn/b22927a2fc3f4199a92408e924ab4657.gif#pic_center)
##### 头删
分类:
1. 链表空
2. 只有一个结点
3. 一个以上结点
当第一个结点的内存释放后,第二个结点的地址也就找不到了,此时头指针就无法指向第二个结点,因此要定义一个新的指针来事先保存第二个结点的地址
![在这里插入图片描述](https://img-blog.csdnimg.cn/cefd142f748c4cfb82f43f22c320f187.gif#pic_center)
###### 头删完整代码如下
void SListPopFront(SListNode** pphead)
{
//链表空
if (*pphead == NULL)
{
return;
}
//只有一个结点和一个以上结点方法相同
else
{
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead=next;
}
}
#### 查找&修改
SListNode* SListFind(SListNode* phead, SListDataType x)
{
SListNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
查找到后,可对该值进行更改
SListNode* pList = NULL;
SListPushBack(&pList, 1);
SListPushBack(&pList, 2);
SListPushBack(&pList, 3);
SListPushBack(&pList, 4);
SListPrint(pList);
SListNode* pos = SListFind(pList, 3);
if (pos)
{
pos->data = 30;
}
SListPrint(pList);
#### 在指定位置结点后面插入
例如在第二个结点后插入(pos=2)
基本思路
1. 找到选定位置的结点
2. 创建新结点
3. 新结点先保存选定位置节点的下个结点的地址,选定位置结点再保存新结点的地址,顺序不能调换,否则新结点无法保存后面一个结点点的地址
正确代码:
newNode->next=pos->next;
pos->next=newNode;
![在这里插入图片描述](https://img-blog.csdnimg.cn/c010ea6326774e87827f22eafe4eef22.gif#pic_center)
###### 完整代码:
void SListInsertAfter(SListNode* pos, SListDataType x)
{
assert(pos); //断言
SListNode\* newNode = BuySListNode(x);
newNode->next = pos->next; //新结点指向后一个结点
pos->next = newNode; //前一个结点指向新结点
}
#### 删除指定位置结点后面的一个结点
例如删除第二个结点(pos=1):
基本思路
1. 找到选定位置的结点
2. 当前结点保存将要删除的结点后一个结点的地址
3. 删除结点
![在这里插入图片描述](https://img-blog.csdnimg.cn/739bc47ea6a847dbaddb5fad28b75d49.gif#pic_center)
###### 完整代码:
void SListEraseAfter(SListNode* pos)
{
assert(pos);
if (pos->next) //要删除结点不为空
{
SListNode* next = pos->next; //pos的下一个结点
SListNode* nextnext = next->next; //pos的下一个的下一个结点
pos->next = nextnext; //上面的定义只是为了方便理解,其实也可以直接写pos->next = pos->next->next
free(next);
}
}
### 单链表完整代码
SList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int SListDataType;
//结点
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;
}SListNode;
void SListPushBack(SListNode** pphead, SListDataType x);
void SListPopBack(SListNode** pphead);
void SListPushFront(SListNode** pphead, SListDataType x);
void SListPopFront(SListNode** pphead);
void SListInsertAfter(SListNode* pos, SListDataType x);
SListNode* SListFind(SListNode* phead, SListDataType x);
void SListPrint(SListNode* phead);
SListNode* BuySListNode(SListDataType x);
SList.c
#include “SList.h”
//申请结点
SListNode* BuySListNode(SListDataType x)
{
SListNode* newNode = (SListNode*)malloc(sizeof(SListNode));
if (newNode == NULL)
{
printf(“申请结点失败\n”);
exit(-1);
}
newNode->data = x;
newNode->next = NULL; //新结点指向NULL
return newNode;
}
//尾插
void SListPushBack(SListNode* *pphead, SListDataType x)//传入指针的地址
{
SListNode* newNode = BuySListNode(x); //申请结点
if (\*pphead == NULL)
{
\*pphead = newNode; //头结点即为新结点
}
else
{
//找尾
SListNode\* tail = \*pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newNode;
}
}
//尾删
void SListPopBack(SListNode** pphead)
{
if (*pphead == NULL) //链表为空
{
return;
}
else if((*pphead)->next==NULL) //只有一个结点
{
free(*pphead); //删除结点
*pphead = NULL; //头指针置空
}
else //有一个以上结点
{
SListNode* prev = NULL;
SListNode* tail = *pphead; //找尾
while (tail->next != NULL)
{
prev = tail; //用于保存tail指向的上一个结点
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
//头插
void SListPushFront(SListNode** pphead,SListDataType x)
{
SListNode* newNode = BuySListNode(x);
newNode->next = *pphead;
*pphead = newNode;
}
//头删
void SListPopFront(SListNode** pphead)
{
//链表空
if (*pphead == NULL)
{
return;
}
//只有一个结点和一个以上结点方法相同
else
{
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead=next;
}
}
//查找
SListNode* SListFind(SListNode* phead, SListDataType x)
{
SListNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//在指定位置结点后面插入
void SListInsertAfter(SListNode* pos, SListDataType x)
{
assert(pos);
SListNode\* newNode = BuySListNode(x);
newNode->next = pos->next;
pos->next = newNode;
}
//删除指定位置结点后面的一个结点
void SListEraseAfter(SListNode* pos)
{
assert(pos);
if (pos->next) //要删除结点不为空
{
SListNode* next = pos->next; //pos的下一个结点
SListNode* nextnext = next->next; //pos的下一个的下一个结点
pos->next = nextnext; //上面的定义只是为了方便理解,其实也可以直接写pos->next = pos->next->next
free(next);
}
}
//打印
void SListPrint(SListNode* phead)
{
SListNode* cur = phead; //再定义一个指针指向头结点
while (cur != NULL)
{
printf(“%d->”, cur->data); //cur指向结构体中的数字域
cur = cur->next; //cur指向下一个结点
}
printf(“NULL”);
printf(“\n”);
}
text.c
#include “SList.h”
void TestSList1()
{
SListNode* pList = NULL;
SListPushBack(&pList, 1);
SListPushBack(&pList, 2);
SListPushBack(&pList, 3);
SListPushBack(&pList, 4);
SListPrint(pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPopBack(&pList);
SListPrint(pList);
SListPushFront(&pList, 1);
SListPushFront(&pList, 2);
SListPushFront(&pList, 3);
SListPushFront(&pList, 4);
SListPushFront(&pList, 5);
SListPrint(pList);
SListPopFront(&pList);
SListPopFront(&pList);
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
pList, 3);
SListPushFront(&pList, 4);
SListPushFront(&pList, 5);
SListPrint(pList);
SListPopFront(&pList);
SListPopFront(&pList);
[外链图片转存中…(img-OpOMYY4o-1715730295338)]
[外链图片转存中…(img-6xThY0sQ-1715730295338)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!