头文件
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct MyStruct
{
int data;
struct MyStruct* next;
}SL;
void listprint(SL** phead);//打印链表
void listpushback(SL** phead, int x);//尾插链表
void listpopback(SL** phead);//尾删链表
void listpushfront(SL** phead, int x);//头插链表
void listpopfront(SL** phead);//头删链表
SL* listfind(SL** phead, int x);//查找某个数
void listinsert(SL** phead, SL* pos,int x);//插入一个数
void listerase(SL** phead, SL* pos);//任意位置删除目标
void listdestory(SL** phead);//销毁链表
主函数
#include"list.h"
void listprint(SL **phead)//打印链表
{
SL* cur = *phead;
while(cur != NULL)
{
printf("%d", cur->data);
cur = cur->next;
}
}
void listpushback(SL**phead,int x)//尾插链表
{
SL* newnode = (SL*) malloc(sizeof(SL));
if (newnode == NULL)
{
printf("malloc,error");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (*phead == NULL)
{
*phead = newnode;
}
else
{
SL* tail = *phead;
while(tail->next!=NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void listpopback(SL** phead) // 尾删链表
{
assert(*phead);
SL* aheadtail = *phead;
SL* tail = aheadtail->next;
while (tail->next != NULL)
{
tail = tail->next;
aheadtail = aheadtail->next;
}
free(tail);
tail = NULL;
aheadtail->next = NULL;
}
void listpushfront(SL** phead, int x)
{
SL* newnode = (SL*)malloc(sizeof(SL));
if (newnode == NULL)
{
printf("malloc,error");
exit(-1);
}
SL* fronthead;
newnode->data = x;
if (*phead == NULL)
*phead = newnode;
else
{
fronthead = *phead;
newnode->next = fronthead;
*phead = newnode;
}
}
void listpopfront(SL** phead) {
assert(*phead);
SL* delphead= *phead;
delphead = delphead->next;
free(*phead);
*phead = delphead;
}
SL* listfind(SL** phead, int x)
{
SL* newnode = *phead;
assert(*phead);
while (*phead)
if (newnode->data == x)
return newnode;
else
{
newnode = newnode->next;
if (newnode == NULL)
return newnode;
}
}
void listinsert(SL** phead, SL* pos, int x)
{
assert(*phead);
SL* find=NULL;
SL* newnode = (SL*)malloc(sizeof(SL));
if (newnode == NULL)
{
printf("malloc,error");
exit(-1);
}
newnode->data = x;
if (*phead == pos) // 使用比较运算符
{
listpushfront(phead, x);
}
else
{
find = *phead;
while (find->next != pos)
{
find = find->next;
}
newnode->next = find->next;
find->next = newnode;
}
}
void listerase(SL** phead, SL* pos)//任意位置删除目标
{
if (*phead == pos)
{
listpopfront(phead);
}
else
{
SL* prev = *phead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void listdestory(SL** phead)//销毁链表
{
assert(*phead);
SL* cur = *phead;
while (cur)
{
*phead = cur->next;
free(cur);
cur = *phead;
}
cur = NULL;
*phead = NULL;
printf("销毁成功");
}
测试
#include"list.h"
void test()
{
SL* plist = NULL;
listpushback(&plist, 1);
listpushback(&plist, 3);
listpushback(&plist, 2);
listpushfront(&plist, 5);
SL* pos = listfind(&plist, 5);
listinsert(&plist, pos, 8);
listprint(&plist);
listdestory(&plist);
}
int main()
{
test();
}
标签:单链,void,next,phead,查改,SL,增删,newnode,NULL
From: https://blog.csdn.net/m0_74217856/article/details/141072599