单链表
这边是指,无头单向非循环链表的各个接口函数的实现
它一般不会单独拿来存储数据,更多拿来作为其他数据结构的子结构
代码实现
Node.h
相关头文件引用
结构体定义
函数接口的声明
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SListDataType;
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;
}SListNode;
void SListPrint(SListNode* phead);//打印
void SListPushFront(SListNode** pphead, SListDataType x);//头插
SListNode* BuySLTNode(SListDataType x);//开节点
void SListPushBck(SListNode** pphead, SListDataType x);//尾插
void SListPopFront(SListNode** pphead);//头删
void SListPopBack(SListNode** pphead);//尾删
void SListDestory(SListNode** pphead);//销毁
SListNode* SListFind(SListNode* phead, SListDataType x);//查找
void SListInsert(SListNode** pphead, SListDataType x, SListNode* pos);//在pos这个位置前面插入数据
void SListInsertAfter( SListDataType x, SListNode* pos);//在pos这个位置后面插入数据
void SListErase(SListNode** pphead,SListNode* pos);//删除pos这个位置
void SListEraseAfter(SListNode* pos);//删除pos后一个位置.那就不用考虑头删了
Node.c
函数定义
#include"Node.h"
void SListPrint(SListNode* phead)//打印
{
while (phead != NULL)
{
printf("%d->", phead->data);
phead = phead->next;
}
printf("NULL\n");
}
SListNode* BuySLTNode(SListDataType x)//开节点
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
perror("malloc fail:");//开空间失败
exit(-1);//直接退出程序
}
newnode->data = x;//存入数据
newnode->next = NULL;//先置空
return newnode;
}
void SListPushFront(SListNode** pphead, SListDataType x)//头插
{
assert(pphead);
SListNode* newnode = BuySLTNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopFront(SListNode** pphead)//头删
{
assert(pphead);
assert(*pphead);//本来就为空了,还删个啥
SListNode* del = *pphead;//先存放一下头节点,等下要释放
*pphead = (*pphead)->next;
free(del);
del = NULL;// 这步可省略,因为del是个局部变量,出了函数,指向原来空间的变量也会不在
}
void SListPushBck(SListNode** pphead, SListDataType x)//尾插
{ //尾插要分情况,原本没有节点和有节点两种情况
assert(pphead);
SListNode* newnode = BuySLTNode(x);
SListNode* tail = *pphead;
if (*pphead == NULL)//plist所指向的是NULL
{
*pphead = newnode;//考虑还没有节点的情况
}
else
{
while (tail->next != NULL)//有节点的时候,找尾 为什么不写tail!=NULL?因为找到最后NULL,找不到前一个节点地址了,连接不上去了。
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPopBack(SListNode** pphead)//尾删
{
assert(*pphead);//要是为空,没有节点就不用删了
assert(pphead);
if ((*pphead)->next==NULL)//考虑只有一个节点的情况, 不止一个的,要去找尾,不然不好对pre去重新指向空
{
free(*pphead);
*pphead = NULL;//这样置空是有意义的,有一个解引用的操作,就会影响实参
}
else
{
//找尾
SListNode* tail = *pphead;
SListNode* pre = NULL;
while (tail->next != NULL)
{
pre = tail;//记录倒数第二个,即将要成为尾节点
tail = tail->next;
}
free(tail);
tail = NULL;
pre->next = NULL;
}
}
void SListDestory(SListNode** pphead)//销毁
{
assert(pphead);
SListNode* prev = NULL;
while ((*pphead)->next != NULL)
{
prev = *pphead;
*pphead = (*pphead)->next;
free(prev);
prev = NULL;
}
free(*pphead);
*pphead = NULL;
}
SListNode* SListFind(SListNode* phead, SListDataType x)//查找
{
SListNode* tail = phead;
while (tail != NULL)
{
if (tail->data == x)
return tail;
else
tail = tail->next;
}
return NULL;//没找到
}
void SListInsert(SListNode** pphead, SListDataType x, SListNode* pos)//在pos这个位置前面插入数据
{
assert(pphead);
assert(pos);
if (pos = *pphead)
{
SListPushFront(pphead,x);
}
else
{
SListNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
assert(cur);
}
SListNode* newnode = BuySLTNode(x);
cur->next = newnode;
newnode->next = pos;
}
}
void SListInsertAfter( SListDataType x, SListNode* pos) // 在pos这个位置后面插入数据
{
assert(pos);
SListNode* newnode = BuySLTNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListErase(SListNode** pphead,SListNode* pos)//pos这个位置删除
{
aaser(pphead);
assert(pos);
if (*pphead == pos)
{
SListPopFront(pphead);
}
else
{
SListNode* pre = NULL;
while (pre->next!=pos)
{
pre = pre->next;
assert(pre);//pre 都走到空了,还没有找到说明pos 这个地址传错了
}
pre->next = pos->next;
free(pos);
}
}
void SListEraseAfter(SListNode* pos)//删除pos后一个位置
{
assert(pos);
if (pos->next == NULL)//就一个节点,怎么往后删?
{
return;//到最后了,没得删
}
else
{
pos->next = pos->next->next;
free(pos->next);
}
}
test.c
函数的测试应用实现
#include"Node.h"
void test1()
{
SListNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPrint(plist);
printf("\n");
SListPushBck(&plist, 5);
SListPushBck(&plist, 6);
SListPushBck(&plist, 7);
SListPrint(plist);
printf("\n");
SListPopFront(&plist);
SListPrint(plist);
printf("\n");
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPrint(plist);
}
int main()
{
test1();
return 0;
}
注意
1.单向链表没有什么要初始化的,给一个指针为NULL
2.所给传参的指针是作为链表的第一个节点记录的,所以实现增删查改中,若要对第一个节点有变动,需要传第一个节点的地址,即,要改变指针(地址),就传它的地址,即为传二级指针。
!!!如果不用二级指针去改变
方法一:在调用函数的时候用一个一级指针来接收。
方法二:设计一个带哨兵位的头节点(头节点与第一个节点连着,所进行的增删查改不会影响哨兵位的头节点的地址)
3.单向链表最方便的就是它的头删,头插功能。
对于尾插,尾删功能不太友好。
尾删:要考虑只有一个节点的情况。如果不止一个,就要去遍历找尾
尾插:要考虑没有节点的情况。如果有节点,还是要去遍历找尾