函数声明:
#include <stdio.h>
#include <Windows.h>
#include <assert.h>
typedef int DataType;
typedef struct SListNode
{
struct SListNode* next;
DataType data;
}SListNode;
SListNode* BuySListNode(DataType x);//创建节点
void SListPrint(SListNode* pHead); //打印链表
void SListDestory(SListNode** ppHead); //释放链表
void SListPushBack(SListNode** ppHead, DataType x);//尾插
void SListPopBack(SListNode** ppHead); //尾删
void SListPushFront(SListNode** ppHead, DataType x);//头插
void SListPopFront(SListNode** ppHead);//头删
SListNode* SListFind(SListNode* pHead, DataType x);//寻找一个节点
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);//在某个位置插入
void SListErase(SListNode** ppHead, SListNode* pos); //删除某个位置
函数定义:
SListNode* BuySListNode(DataType x)
{
SListNode *node=(SListNode*)malloc(sizeof(SListNode));
node->data=x;
node->next=NULL;
return node;
}
void SListPrint(SListNode* pHead)
{
SListNode* cur=pHead;
while (cur!=NULL)
{
printf("%d ",cur->data);
cur=cur->next;
}
printf("\n");
}
void SListPushBack(SListNode** ppHead, DataType x)
{
assert(ppHead);
if (*ppHead==NULL)
{
*ppHead=BuySListNode(x);
}
else
{
SListNode *cur=*ppHead;
while(cur->next)
{
cur=cur->next;
}
cur->next=BuySListNode(x);
}
}
void SListPopBack(SListNode** ppHead)
{
assert(ppHead);
if (*ppHead==NULL)
{
return;
}
else if ((*ppHead)->next==NULL)
{
free(*ppHead);
*ppHead=NULL;
}
else
{
SListNode *prev=NULL;
SListNode *cur=*ppHead;
while (cur->next)
{
prev=cur;
cur=cur->next;
}
prev->next=NULL;
free(cur);
}
}
void SListPushFront(SListNode** ppHead, DataType x)
{
assert(ppHead);
if (*ppHead==NULL)
{
*ppHead=BuySListNode(x);
}
else
{
SListNode *cur=BuySListNode(x);
cur->next=*ppHead;
*ppHead=cur;
}
}
void SListPopFront(SListNode** ppHead)
{
assert(ppHead);
if (*ppHead==NULL)
{
return;
}
else if ((*ppHead)->next==NULL)
{
free(*ppHead);
*ppHead=NULL;
}
else
{
SListNode *cur=*ppHead;
cur=cur->next;
free(*ppHead);
*ppHead=cur;
}
}
SListNode* SListFind(SListNode* pHead, DataType x)
{ SListNode *cur=pHead;
assert(pHead);
while(cur)
{
if (cur->data==x)
{
return cur;
}
cur=cur->next;
}
return NULL;
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)
{
assert(ppHead&&pos);
if (pos==*ppHead)
{
SListPushFront(ppHead,x);
}
else
{
SListNode *cur=BuySListNode(x);
SListNode *prev=*ppHead;
while (prev->next!=pos)
{
prev=prev->next;
}
prev->next=cur;
cur->next=pos;
}
}
void SListErase(SListNode** ppHead, SListNode* pos)
{
assert(ppHead&&pos);
if (pos==*ppHead)
{
SListPopFront(ppHead);
}
else
{
SListNode *cur=*ppHead;
while (cur->next!=pos)
{
cur=cur->next;
}
cur->next=pos->next;
free(pos);
}
}
void SListDestory(SListNode** ppHead)
{
assert(ppHead);
if ((*ppHead)->next==NULL)
{
free(ppHead);
*ppHead=NULL;
}
else
{
SListNode *cur=*ppHead;
while (cur->next!=NULL)
{
SListPopBack(ppHead);
}
}
}
测试代码:
void Test1()//测试尾插头删尾删
{
SListNode *a=NULL;
SListPushBack(&a,1);
SListPushBack(&a,2);
SListPushBack(&a,3);
SListPopBack(&a);
SListPopFront(&a);
SListPrint(a);
}
void Test2()//测试头插头删尾删
{
SListNode *a=NULL;
SListPushFront(&a,1);
SListPushFront(&a,2);
SListPushFront(&a,3);
SListPopBack(&a);
SListPopFront(&a);
SListPrint(a);
}
void Test3()//测试查找,某个位置删,插
{
SListNode *a=NULL;
SListNode *pos=NULL;
SListPushFront(&a,1);
SListPushFront(&a,2);
SListPushFront(&a,3);
pos=SListFind(a,2);
SListInsest(&a,pos,0);
SListPrint(a);
SListErase(&a,pos);
SListPrint(a);
}
主函数:
int main()
{
Test1();
Test2();
Test3();
system("pause");
}