小伙伴可以自行调试哦
一、头文件(STlist.h)
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SListDataType;//定义int类型
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;//存储下一个节点的信息
}SListNode;
//打印节点信息
void SListPrint(SListNode* phead);
//创建一个节点
SListNode* BuySListNode(SListDataType x);
//尾插一个节点
void SListPushBack(SListNode** pphead, SListDataType x);
//头插一个节点
void SListPushFront(SListNode** pphead, SListDataType x);
//尾删一个节点
void SListPopBack(SListNode** pphead);
//头删一个节点
void SListPopFront(SListNode** pphead);
//单链表查找
SListNode* SListFind(SListNode* phead, SListDataType x);
//在单链表pos之前插入x
void SListInsertBefore(SListNode** pphead, SListNode* pos, SListDataType x);
//在单链表pos之后插入x
void SListInsertAfter(SListNode* pos, SListDataType x);
//单链表删除pos位置之前的值
void SListEraseBefore(SListNode** pphead, SListNode* pos);
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);
//销毁单链表
void SListDestory(SListNode** pphead);
二、实现函数(STlist.c)
assert:断言
#include "STlist.h"
void SListPrint(SListNode* phead)
{
SListNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
//创建一个新节点
SListNode* BuySListNode(SListDataType x)
{
SListNode* newnode = malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
}
return newnode;
}
void SListPushBack(SListNode** pphead, SListDataType x)
{
assert(pphead);
SListNode* newnode = BuySListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SListNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SListNode** pphead, SListDataType x)
{
assert(pphead);
SListNode* newnode = BuySListNode(x);
SListNode* cur = *pphead;
newnode->next = cur;
*pphead = newnode;
}
void SListPopBack(SListNode** pphead)
{
assert(pphead);
if (*pphead == NULL)
{
return;
}
else if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SListNode* tail = *pphead;
SListNode* prev = NULL;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SListPopFront(SListNode** pphead)
{
assert(pphead);
if (*pphead == NULL)
{
return;
}
else
{
SListNode* cur = *pphead;
*pphead = cur->next;
free(cur);
cur = NULL;
}
}
SListNode* SListFind(SListNode* phead, SListDataType x)
{
assert(phead);
SListNode* cur = phead;
while (cur->next != NULL)
{
if (x == cur->data)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsertBefore(SListNode** pphead, SListNode* pos, SListDataType x)
{
assert(pphead);
assert(pos);
if (*pphead == pos)
{
SListPushFront(pphead, x);
}
else
{
SListNode* newnode = BuySListNode(x);
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
newnode->next = pos;
prev->next = newnode;
}
}
void SListInsertAfter(SListNode* pos, SListDataType x)
{
//推荐方法1
assert(pos);
SListNode* next = pos->next;
SListNode* newnode = BuySListNode(x);
pos->next = newnode;
newnode->next = next;
}
//方法2
//void SListEraseAfter(SListNode* pos)
//{
// assert(pos);
// SListNode* next = pos->next;
// pos->next = pos->next->next;
// free(next);
//}
void SListEraseAfter(SListNode* pos)
{
assert(pos);
SListNode* next = pos->next;
if (next != NULL)
{
SListNode* nextnext = next->next;
free(next);
next = NULL;
pos->next = nextnext;
}
}
void SListEraseBefore(SListNode** pphead, SListNode* pos)
{
assert(pphead);
assert(pos);
if (*pphead == pos)
{
SListPopFront(pphead);
}
else
{
SListNode* prev = *pphead;
SListNode* cur = *pphead;
while (prev->next != pos)
{
cur = prev;
prev = prev->next;
}
free(prev);
prev = NULL;
cur->next = pos;
}
}
void SListDestory(SListNode** pphead)
{
assert(pphead);
SListNode* cur = *pphead;
while (cur)
{
SListNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
三、main函数
#include "STlist.h"
void TestSList1()
{
SListNode* slist = NULL;
SListNode* n1 = malloc(sizeof(SListNode));
SListNode* n2 = malloc(sizeof(SListNode));
SListNode* n3 = malloc(sizeof(SListNode));
slist = n1;
n1->data = 1;
n2->data = 2;
n3->data = 3;
n1->next = n2;
n2->next = n3;
n3->next = NULL;
SListPrint(slist);
SListPushBack(&slist, 4);
SListPrint(slist);
SListPushFront(&slist, 0);
SListPrint(slist);
SListPopBack(&slist);
SListPrint(slist);
SListPopFront(&slist);
SListPrint(slist);
SListNode* find = SListFind(slist, 2);
if (find == NULL)
{
printf("没找到\n");
}
else
{
SListInsertAfter(find, 6);
SListEraseAfter(find);
SListInsertBefore(&slist, find, 30);
SListEraseBefore(&slist, find);
}
SListPrint(slist);
SListDestory(&slist);
}
int main()
{
TestSList1();
return 0;
}