目录
本篇开始链表学习。今天主要是单链表&OJ题目。
前面的博文我们讲了顺序表。顺序表的优势就是【物理空间的连续】,就只需要一个指针指向开始位置,用数组下标去访问即可。但是这也是它的劣势。当插入和删除数据需要挪动数据。
无论是【顺序表】还是【链表】里的数据,任何类型都可。所以用typedef。
在开始阶段,线性表可能是物理空间上连续【顺序表】,可能是逻辑顺序上连续【链表】。链表的优势就是,删除和插入数据不需要挪动,空间可以一块一块的释放,不会影响其他节点。链表每个节点都是独立的。
【链表】的种类很多,今天先介绍【无头单项不循环链表】----【单链表】。



#include"SList.h"
- int main()
- {
- SLNode* phead = NULL;//结构体指针变量存放结构体的地址 头节点
- test1(&phead);//测试尾插
- test2(&phead);//测试头插
- test3(&phead);//测试尾删
- test4(&phead);//测试头删
- return 0;
- }
- void test1(SLNode** pphead)//测试尾插
- {
- SLPushBack(pphead, 10);
- SLPushBack(pphead, 20);
- SLPushBack(pphead, 30);
- SLPushBack(pphead, 40);
- SLPrint(*pphead);
- }
- void test2(SLNode** pphead)//测试头插
- {
- SLPushFront(pphead, 77);
- SLPushFront(pphead, 66);
- SLPushFront(pphead, 55);
- SLPushFront(pphead, 33);
- SLPrint(*pphead);
- }
- void test3(SLNode** pphead)//测试头删
- {
- SLPopFront(pphead);
- SLPopFront(pphead);
- SLPopFront(pphead);
- SLPrint(*pphead);
- }
- void test4(SLNode** pphead)//测试尾删
- {
- SLPopBack(pphead);
- SLPopBack(pphead);
- SLPrint(*pphead);
- }
- #pragma once
- #include
- #include
- #include
- //创建单链表
- 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);
#include"SList.h"
- 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");
- }

- //创建链表的节点---结构体
- 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;
- }

- //尾插
- 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;
- }
-
- }


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

- //头删
- void SLPopFront(SLNode** pphead)
- {
- assert(*pphead);
- SLNode* tail = *pphead;
- *pphead = (*pphead)->next;
- free(tail);
- tail = NULL;
- }

- //尾删
- 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;
- }
- }


代码---------→【唐棣棣 (TSQXG) - Gitee.com】
联系---------→【邮箱:2784139418@qq.com】