目录
头文件
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLdatatype;
typedef struct SeQlist
{
SLdatatype data;
struct SeQlist* next;
}SeQlist;
void print(SeQlist* pphead);
void pushpront(SeQlist** pphead, SLdatatype x);
void pushback(SeQlist** pphead, SLdatatype x);
void poppront(SeQlist** pphead);
void popback(SeQlist** pphead);
void SLinsert(SeQlist** pphead, SeQlist*pos,SLdatatype x);
void SLinsertafter( SeQlist* pos, SLdatatype x);
void SLerase(SeQlist** pphead, SeQlist* pos);
SeQlist* SLfind(SeQlist* pphead, SLdatatype x);
void SLEraseafter( SeQlist* pos);
void SLDestroy(SeQlist* pphead);
实现文件
#include "SeQlist.h"
void print(SeQlist* pphead)
{
while (pphead)
{
printf("%d->", pphead->data);
pphead = pphead->next;
}
printf("NULL\n");
}
SeQlist* SLbuynode(SLdatatype x)
{
SeQlist* newnode = (SeQlist*)malloc(sizeof(SeQlist));
if (newnode == NULL)
{
perror("malloc fail");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void pushpront(SeQlist** pphead,SLdatatype x)
{
assert(pphead);
SeQlist* newnode = SLbuynode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void pushback(SeQlist** pphead, SLdatatype x)
{
assert(pphead);
SeQlist* newnode = SLbuynode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SeQlist* ptail = *pphead;
while (ptail->next)
{
ptail = ptail->next;
}
ptail->next = newnode;
}
}
void poppront(SeQlist** pphead)
{
assert(pphead && *pphead);
SeQlist* next = (*pphead)->next ;
free(*pphead);
*pphead = next;
}
void popback(SeQlist** pphead)
{
assert(pphead && *pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SeQlist* ptail = *pphead;
SeQlist* prv = *pphead;
while (ptail->next)
{
prv = ptail;
ptail = ptail->next;
}
free(ptail);
ptail = NULL;
prv->next = NULL;
}
}
void SLinsert(SeQlist** pphead, SeQlist* pos, SLdatatype x)
{
assert(pphead);
assert(pos);
SeQlist* newnode = SLbuynode(x);
if (pos == *pphead)
{
pushpront(pphead, x);
}
else
{
SeQlist* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
newnode->next = prev->next;
prev->next = newnode;
}
}
void SLinsertafter( SeQlist* pos, SLdatatype x)
{
assert(pos);
SeQlist* newnode = SLbuynode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SLerase(SeQlist** pphead, SeQlist* pos)
{
assert(pphead && *pphead);
assert(pos);
if (pos == *pphead)
{
poppront(pphead);
}
else
{
SeQlist* prv = *pphead;
while (prv->next !=pos)
{
prv = prv->next;
}
prv->next = pos->next;
free(pos);
pos = NULL;
}
}
SeQlist* SLfind(SeQlist* pphead, SLdatatype x)
{
assert(pphead);
while (pphead)
{
if (pphead->data == x)
{
return pphead;
}
pphead = pphead->next;
}
return NULL;
}
void SLEraseafter( SeQlist* pos)
{
assert(pos&&pos->next);
SeQlist* del = pos->next;
pos->next = del->next;
free(del);
del= NULL;
}
void SLDestroy(SeQlist** pphead)//一个一个销毁
{
assert(pphead && *pphead);
SeQlist* pcur = *pphead;
while (pcur)
{
SeQlist* next = pcur;
next = pcur->next;
free(pcur);
pcur = next;
}
*pphead = NULL;
}
测试文件
#include "SeQlist.h"
void test01()
{
SeQlist* node1 = (SeQlist*)malloc(sizeof(SeQlist));
node1->data = 1;
SeQlist* node2 = (SeQlist*)malloc(sizeof(SeQlist));
node2->data = 2;
SeQlist* node3 = (SeQlist*)malloc(sizeof(SeQlist));
node3->data = 3;
SeQlist* node4 = (SeQlist*)malloc(sizeof(SeQlist));
node4->data = 4;
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = NULL;
SeQlist* plist = node1;
print(plist);//1 2 3 4
pushpront(&plist, 7);
print(plist);//7 1 2 3 4
pushback(&plist, 9);
print(plist);//7 1 2 3 4 9
poppront(&plist);
print(plist);//1 2 3 4 9
popback(&plist);
print(plist);//1 2 3 4
SLinsert(&plist, node2, 99);
print(plist);//1 99 2 3 4
SLinsertafter(node3, 89);
print(plist);//1 99 2 3 89 4
SLerase(&plist, node4);
print(plist);//1 99 2 3 89
SeQlist*find= SLfind(plist, 2);
if (find == NULL)
{
printf("没有找到\n");
}
else
{
printf("找到了\n");
}
SLEraseafter(find);
print(plist);//1 99 2 89
}
int main()
{
test01();
return 0;
}