外贸招聘网站,北京的招聘网站有哪些,网站开发待遇如何,html5设计网页代码目录
单链表
主函数test.c
test1
test2
test3
test4
头文件函数声明SList.h
函数实现SList.c
打印SLPrint
创建节点CreateNode
尾插SLPushBack
头插SLPushFront
头删SLPopBck
尾删SLPopFront
易错点 本篇开始链表学习。今天主要是单链表OJ题目。
单链…目录
单链表
主函数test.c
test1
test2
test3
test4
头文件函数声明SList.h
函数实现SList.c
打印SLPrint
创建节点CreateNode
尾插SLPushBack
头插SLPushFront
头删SLPopBck
尾删SLPopFront
易错点 本篇开始链表学习。今天主要是单链表OJ题目。
单链表
前面的博文我们讲了顺序表。顺序表的优势就是【物理空间的连续】就只需要一个指针指向开始位置用数组下标去访问即可。但是这也是它的劣势。当插入和删除数据需要挪动数据。
无论是【顺序表】还是【链表】里的数据任何类型都可。所以用typedef。
在开始阶段线性表可能是物理空间上连续【顺序表】可能是逻辑顺序上连续【链表】。链表的优势就是删除和插入数据不需要挪动空间可以一块一块的释放不会影响其他节点。链表每个节点都是独立的。
【链表】的种类很多今天先介绍【无头单项不循环链表】----【单链表】。 主函数test.c
#includeSList.h
int main()
{SLNode* phead NULL;//结构体指针变量存放结构体的地址 头节点test1(phead);//测试尾插test2(phead);//测试头插test3(phead);//测试尾删test4(phead);//测试头删return 0;
}
test1
void test1(SLNode** pphead)//测试尾插
{SLPushBack(pphead, 10);SLPushBack(pphead, 20);SLPushBack(pphead, 30);SLPushBack(pphead, 40);SLPrint(*pphead);
}
test2
void test2(SLNode** pphead)//测试头插
{SLPushFront(pphead, 77);SLPushFront(pphead, 66);SLPushFront(pphead, 55);SLPushFront(pphead, 33);SLPrint(*pphead);
}
test3
void test3(SLNode** pphead)//测试头删
{SLPopFront(pphead);SLPopFront(pphead);SLPopFront(pphead);SLPrint(*pphead);
}
test4
void test4(SLNode** pphead)//测试尾删
{SLPopBack(pphead);SLPopBack(pphead);SLPrint(*pphead);
}
头文件函数声明SList.h
#pragma once
#includestdio.h
#includeassert.h
#includestdlib.h
创建单链表
//创建单链表
typedef int SLNDataType;//单链表节点数据类型typedef struct SListNode//创建节点
{SLNDataType val;struct SListNode* next;
}SLNode;
为什么 SListNode 还未创建好就可以在结构体内部使用这个 SListNode 了
因为next是一个结构体指针变量主体是指针变量无影响。但是如果是 struct SListNode next;不可以结构体嵌套结构体是不可以的。 打印数据
//打印数据
void SLPrint(SLNode* phead);
尾插
//尾插
void SLPushBack(SLNode** pphead, SLNDataType x);
头插
//头插
void SLPushFront(SLNode** pphead, SLNDataType x);
头删
//头删
void SLPopFront(SLNode** pphead);
尾删
//尾删
void SLPopBack(SLNode** pphead);
函数实现SList.c
#includeSList.h
打印SLPrint
不要让phead移动
void SLPrint(SLNode* phead)
{assert(phead);SLNode* tail phead;printf(phead-);while (tail-next ! NULL){printf(%d-, tail-val);tail tail-next;}printf(NULL);printf(\n);
} 创建节点CreateNode
//创建链表的节点---结构体
SLNode* CreateNode(SLNDataType x)
{SLNode* newnode (SLNode*)malloc(sizeof(SLNode));if (newnode NULL){perror(malloc);exit(-1);//直接终止程序//return;}newnode-val x;newnode-next NULL;return newnode;
} 尾插SLPushBack
二级指针的使用不然就会链接不起来出了函数栈帧局部变量就销毁了。改变外部的变量一定有一个解引用的操作多情况的考虑
//尾插
void SLPushBack(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;}} 头插SLPushFront
代码书写的先后顺序二级指针
//头插
void SLPushFront(SLNode** pphead, SLNDataType x)
{//assert(*pphead);SLNode* newnode CreateNode(x);newnode-next *pphead;*pphead newnode;
} 头删SLPopBck
代码书写的先后顺序二级指针
//头删
void SLPopFront(SLNode** pphead)
{assert(*pphead);SLNode* tail *pphead;*pphead (*pphead)-next;free(tail);tail NULL;
} 尾删SLPopFront
多种情况的考虑
//尾删
void SLPopBack(SLNode** pphead)
{assert(*pphead);//一个节点if ((*pphead)-next NULL){free(*pphead);*pphead NULL;}else{SLNode* tail *pphead;SLNode* prve tail;while (tail-next ! NULL){prve tail;tail tail-next;}prve-next NULL;free(tail);tail NULL;}
}易错点
断言❌无节点/一个节点/多节点的考虑❌传值调用/传址调用二级指针使用❌记住要修改头节点头节点是结构体指针变量的指向必须用二级指针❌空间的释放不是释放指针变量释放的是指针指向的空间❌*pphead*pphead-next辨析❌野指针的诞生❌
代码---------→【唐棣棣 (TSQXG) - Gitee.com】
联系---------→【邮箱2784139418qq.com】