功能实现部分
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
//malloc一个新节点
SLTNode* BuyListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//打印单链表
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
//头插
void SListPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode=BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
//尾插
void SListPushBack(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode = BuyListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLTNode* tail = *pphead;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
//头删
void SListPopFront(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
SLTNode* next = (*pphead)->next;//注意这里有运算符号优先级的问题,要加上括号,不能写成 *pphead->next
free(*pphead);
*pphead = next;
}
//尾删
void SListPopBack(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
SLTNode* tail =* pphead;
while (tail->next->next)
{
tail = tail->next;
}
tail->next = NULL;
}
//寻找单链表中某一数据并返回其地址
SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{
SLTNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
//在目标节点前插入一个新的节点
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
assert(*pphead);
assert(pos);
SLTNode* newnode = BuyListNode(x);
if (*pphead == pos)
{
newnode->next = *pphead;
*pphead = newnode;
}
else
{
SLTNode* posPrev = *pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newnode;
newnode->next = pos;
}
}
//在目标节点后面插入一个节点
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuyListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
//删除目标节点
void SListErase(SLTNode** pphead, SLTNode* pos)
{
assert(*pphead);
assert(pos);
if (*pphead = pos)
{
SListPopFront(pphead);
}
else
{
SLTNode* posPrev =* pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = pos->next;
free(pos);
pos = NULL;
}
}
//删除目标节点后的节点
void SListEraseAfter(SLTNode* pos)
{
assert(pos);
assert(pos->next);
SLTNode* next = pos->next;
pos->next = next->next;
free(next);
next = NULL;
}
//销毁单链表
void SListDestory(SLTNode** pphead)
{
assert(*pphead);
SLTNode* cur = *pphead;
while (cur)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
头文件部分
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);
void SListPushBack(SLTNode** pphead, SLTDataType x);
void SListPushFront(SLTNode** pphead, SLTDataType x);
void SListPopBack(SLTNode** pphead);
void SListPopFront(SLTNode** pphead);
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
void SListInsertAfter(SLTNode* pos, SLTDataType x);
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
void SListErase(SLTNode** pphead, SLTNode* pos);
void SListEraseAfter(SLTNode* pos);
void SListDestory(SLTNode** pphead);
测试部分
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
void TestSList1()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 1);
SListPushFront(&plist, 1);
SListPushFront(&plist, 1);
SListPushFront(&plist, 1);
SListPrint(plist);
SListPushBack(&plist, 5);
SListPushBack(&plist, 4);
SListPushBack(&plist, 3);
SListPushBack(&plist, 2);
SListPrint(plist);
SListPopFront(&plist);
SListPopFront(&plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPrint(plist);
//使用寻找特定数据的方法可以将数据更改(只更改第一个目标值数据)
SLTNode* pos = SListFind(plist, 1);
if (pos)
{
pos->data = 5;
}
SListPrint(plist);
SListDestory(&plist);
}
void TestSList2()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPrint(plist);
SLTNode* pos = SListFind(plist, 3);
if (pos)
{
SListInsert(&plist, pos, 30);
}
SListPrint(plist);
pos = SListFind(plist, 1);
if (pos)
{
SListInsert(&plist, pos, 10);
}
SListPrint(plist);
pos = SListFind(plist, 4);
if (pos)
{
SListInsert(&plist, pos, 40);
}
SListPrint(plist);
}
void TestSList3()
{
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 2);
SListPushFront(&plist, 4);
SListPushFront(&plist, 2);
SListPushFront(&plist, 2);
SListPushFront(&plist, 4);
SListPrint(plist);
// 找
SLTNode* pos = SListFind(plist, 2);
int i = 1;
while (pos)
{
printf("第%d个pos节点:%p->%d\n", i++, pos, pos->data);
pos = SListFind(pos->next, 2);
}
// 修改 3->30
pos = SListFind(plist, 3);
if (pos)
{
pos->data = 30;
}
SListPrint(plist);
}
int main()
{
//TestSList();
//TestSList2();
TestSList3();
return 0;
}
本文到此结束,感谢大家的阅读,欢迎大家点赞评论互关,祝大家万事如意。