实现了不带哨兵位的单链表
注意:凡是涉及到头节点需要改变的一定要传二级指针,代码如下,仅供参考
SList.h
cpp
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
//
}SLTNode;
void SLTPrint(SLTNode* phead);
//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x);
void SLTPushFront(SLTNode** pphead, SLTDataType x);
void SLTPopBack(SLTNode** pphead);
void SLTPopFront(SLTNode** pphead);
//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);
//销毁链表
void SListDesTroy(SLTNode** pphead);
SList.c
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void SLTPrint(SLTNode* phead)
{
SLTNode* pcur = phead;
while (pcur)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("NULL\n");
}
SLTNode* BuyNode(SLTDataType x)
{
SLTNode* tmp = (SLTNode*)malloc(sizeof(SLTNode));
if (tmp == NULL)
{
perror("malloc");
exit(1);
}
tmp->data = x;
tmp->next = NULL;
return tmp;
}
//头部插入删除/尾部插入删除
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* pcur = *pphead;
if (pcur == NULL)
{
*pphead = BuyNode(x);
}
else
{
while (pcur->next)
{
pcur = pcur->next;
}
pcur->next = BuyNode(x);
}
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* pcur = *pphead;
SLTNode* newnode= BuyNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLTPopBack(SLTNode** pphead)
{
assert(pphead && *pphead);
SLTNode* pcur = *pphead;
if (pcur->next == NULL)
{
free(pcur);
pcur = NULL;
*pphead = NULL;
}
else
{
while (pcur->next->next)
{
pcur = pcur->next;
}
SLTNode* del = pcur->next;
free(del);
del = NULL;
pcur->next = NULL;
}
}
void SLTPopFront(SLTNode** pphead)
{
assert(pphead && *pphead);
SLTNode* newhead = (*pphead)->next;
free(*pphead);
*pphead = newhead;
}
//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
assert(phead);
SLTNode* pcur = phead;
while (pcur)
{
if (pcur->data == x)
{
return pcur;
}
pcur = pcur->next;
}
printf("没找到%d\n", x);
return NULL;
}
//在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
assert(pphead && *pphead);
assert(pos);
SLTNode* pcur = *pphead;
SLTNode* prev = *pphead;
while (pcur != pos)
{
prev = pcur;
pcur = pcur->next;
}
SLTNode* newnode = BuyNode(x);
if (pcur == prev)
{
newnode->next = pos;
*pphead = newnode;
}
else
{
newnode->next = pos;
prev->next = newnode;
}
}
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
assert(pphead && *pphead);
assert(pos);
SLTNode* pcur = *pphead;
SLTNode* prev = *pphead;
while (pcur != pos)
{
prev = pcur;
pcur = pcur->next;
}
if (prev == pcur)
{
*pphead = pcur->next;
free(pcur);
pcur = prev = NULL;
}
else
{
prev->next = pcur->next;
free(pos);
pos = NULL;
pcur = NULL;
}
}
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuyNode(x);
SLTNode* new_next = pos->next;
pos->next = newnode;
newnode->next = new_next;
}
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos)
{
assert(pos && pos->next);
SLTNode* new_next = pos->next->next;
free(pos->next);
pos->next = new_next;
}
//销毁链表
void SListDesTroy(SLTNode** pphead)
{
assert(pphead);
SLTNode* pcur = *pphead;
while (pcur)
{
SLTNode* del = pcur;
pcur = pcur->next;
free(del);
del = NULL;
}
*pphead = NULL;
}
test.c
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void test01()
{
//SLTNode sl;
SLTNode* psl =NULL;
SLTPushBack(&psl, 1);
SLTPrint(psl);
/*SLTErase(&psl, SLTFind(psl, 1));
SLTPrint(psl);*/
SLTPushBack(&psl, 2);
SLTPrint(psl);
SLTPushBack(&psl, 3);
SLTPrint(psl);
SLTPushBack(&psl, 4);
SLTPrint(psl);
SLTPushFront(&psl, 5);
SLTPrint(psl);
SLTPushFront(&psl, 6);
SLTPrint(psl);
SLTPushFront(&psl, 7);
SLTPrint(psl);
SLTPopBack(&psl);
SLTPrint(psl);
SLTPopBack(&psl);
SLTPrint(psl);
SLTPopBack(&psl);
SLTPrint(psl);
SLTPopBack(&psl);
SLTPrint(psl);
SLTInsert(&psl, SLTFind(psl,7), 8);
SLTPrint(psl);
SLTErase(&psl, SLTFind(psl, 5));
SLTPrint(psl);
SLTInsertAfter(SLTFind(psl, 8), 9);
SLTPrint(psl);
SLTEraseAfter(SLTFind(psl, 7));
SLTPrint(psl);
SListDesTroy(&psl);
/*SLTPopFront(&psl);
SLTPrint(psl);
SLTPopFront(&psl);
SLTPrint(psl);
SLTPopFront(&psl);
SLTPrint(psl);*/
}
int main()
{
test01();
return 0;
}