#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
typedef int DataType;
typedef struct SListNode
{
struct SListNode *pNext;//指向下一空间的地址
DataType data;
} SListNode;
SListNode *__CreatNewNode(DataType data);//申请空间存放新链表
void SListNodePrint(SListNode **ppfirst);//打印
void SListInit(SListNode **ppfirst);//初始化
void SListPushBack(SListNode **ppfirst, DataType data);//尾插
void SListPushFront(SListNode **ppfirst, DataType data);//头插
void SListPopBack(SListNode **ppfirst);//尾删
void SListPopFront(SListNode **ppfirst);//头删
SListNode *SListFind(SListNode *ppfirst, DataType data);//依照数据找结点
void SListInsert(SListNode **ppfirst, SListNode *pos, DataType data);//按给定结点插入
void SListErase(SListNode **ppfirst, SListNode *pos);//给定结点删除
void SListRemove(SListNode **ppfirst, DataType data);//按值删除
void SListremoveAll(SListNode **ppfirst, DataType data);//按值删所有
void SListDestroy(SListNode **ppfirst);//销毁
SListNode *__CreatNewNode(DataType data)//申请空间存放新链表
{
SListNode *pNewNode = (SListNode *)malloc(sizeof(SListNode));
assert(pNewNode);
pNewNode->data = data;
pNewNode->pNext = NULL;
return pNewNode;
}
void SListNodePrint(SListNode **ppfirst)
{
SListNode *pNode;
for (pNode = *ppfirst; pNode != NULL; pNode = pNode->pNext)
{
printf("%d->", pNode->data);
}
printf("NULL\n");
}
void SListInit(SListNode **ppfirst)//初始化
{
*ppfirst = NULL;
}
void SListPushBack(SListNode **ppfirst, DataType data)//尾插
{
SListNode *pNewNode = __CreatNewNode(data);
SListNode *pNode;
if (*ppfirst == NULL)
{
*ppfirst = pNewNode;
return;
}
for (pNode = *ppfirst; pNode->pNext != NULL; pNode = pNode->pNext)
{
}
pNode->pNext = pNewNode;
}
void SListPushFront(SListNode **ppfirst, DataType data)//头插
{
SListNode *pNewNode = __CreatNewNode(data);
if (*ppfirst == NULL)
{
*ppfirst = pNewNode;
return;
}
pNewNode->pNext = *ppfirst;
*ppfirst = pNewNode;
}
void SListPopBack(SListNode **ppfirst)//尾删
{
SListNode *pNode;
SListNode *pDel;
if (*ppfirst == NULL)
return;
if ((*ppfirst)->pNext == NULL)
{
free(ppfirst);
*ppfirst = NULL;
return;
}
for (pNode = *ppfirst; pNode->pNext->pNext != NULL; pNode = pNode->pNext)
{
}
pDel = pNode->pNext;
pNode->pNext = NULL;
free(pDel);
}
void SListPopFront(SListNode **ppfirst)//头删
{
SListNode *pDel;
if (*ppfirst == NULL)
return;
pDel = *ppfirst;
*ppfirst = pDel->pNext;
free(pDel);
}
SListNode *SListFind(SListNode *ppfirst, DataType data)//依照数据找结点
{
SListNode *pNode;
assert(ppfirst);
for (pNode = ppfirst; pNode != NULL; pNode = pNode->pNext)
{
if (pNode->data == data)
return pNode;
}
return NULL;
}
void SListInsert(SListNode **ppfirst, SListNode *pos, DataType data)//按给定结点插入
{
SListNode *pNewNode = __CreatNewNode(data);
SListNode *pNode;
assert(*ppfirst);
if (*ppfirst == pos)
{
SListPushFront(ppfirst, data);
return;
}
for (pNode = *ppfirst; pNode->pNext != pos; pNode = pNode->pNext)
{
}
pNode->pNext = pNewNode;
pNewNode->pNext = pos;
}
void SListErase(SListNode **ppfirst, SListNode *pos)//给定结点删除
{
SListNode *pNode;
if (*ppfirst == pos)
{
SListPopFront(ppfirst);
return;
}
for (pNode = *ppfirst; pNode->pNext != pos; pNode = pNode->pNext)
{
}
pNode->pNext = pos->pNext;
free(pos);
}
void SListRemove(SListNode **ppfirst, DataType data)//按值删除
{
SListNode *pNode = SListFind(*ppfirst, data);
assert(pNode);
SListErase(ppfirst, pNode);
}
void SListremoveAll(SListNode **ppfirst, DataType data)//按值删所有
{
SListNode *pNode = *ppfirst;
SListNode *pCmp;
while (pNode->pNext != NULL)
{
pCmp = pNode->pNext;
if (pCmp->data == data)
{
pNode->pNext = pCmp->pNext;
free(pCmp);
}
else
{
pNode = pNode->pNext;
}
}
if (pNode->data == data)
{
*ppfirst = pNode->pNext;
free(pNode);
}
}
void SListDestroy(SListNode **ppfirst)
{
SListNode *pNode, *pN;
for (pNode = *ppfirst; pNode != NULL;)
{
pN = pNode;
pNode = pNode->pNext;
free(pN);
}
}