SList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
typedef int SLDateType;
typedef struct SListNode
{
SLDateType data;
struct SListNode* next;
}SLNode;
void SLPrint(SLNode* phead);//链表的打印
void SLPushFront(SLNode**pphead,SLDateType x);//链表的头插法
void SLPushBack(SLNode** pphead, SLDateType x);//链表的尾插法
void SLPopFront(SLNode** pphead);//链表的头删
void SLPopBack(SLNode** pphead);//链表的尾删
SLNode* SLSearch(SLNode* phead, SLDateType x);//链表的查找
void SLInsert(SLNode** pphead, SLNode* pos, SLDateType x);//在pos之前插入
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLDateType x);//在pos后插入
void SLErase(SLNode** pphead, SLNode*pos);//删除pos位置的数据
void SLEraseAfter(SLNode* pos);//删除pos之后的数据
void SLDestroy(SLNode** pphead);//删除单链表
SList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void SLPrint(SLNode* phead)
{
SLNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SLNode* BuyNode(SLDateType x)
{
SLNode *newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc fail ::");
return;
}
newnode->data = x;
newnode->next = NULL;
}
void SLPushFront(SLNode** pphead, SLDateType x)
{
SLNode* newnode = BuyNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLPushBack(SLNode** pphead, SLDateType x)
{
SLNode* newnode = BuyNode(x);
//空链表
if (*pphead == NULL)
{
*pphead = newnode;
}
//非空链表
else
{
SLNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SLPopFront(SLNode** pphead)
{
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* del = *pphead;
*pphead = (*pphead)->next;
free(del);
}
}
void SLPopBack(SLNode** pphead)
{
assert(*pphead);
if ((*pphead)->next->next == NULL)
{
*pphead = NULL;
}
else
{
SLNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
SLNode* SLSearch(SLNode* phead, SLDateType x)
{
if (phead == NULL)
return NULL;
else
{
SLNode* search = phead;
while (search)
{
if (search->data == x)
{
return search;
}
search = search->next;
}
return NULL;
}
}
void SLInsert(SLNode** pphead, SLNode* pos, SLDateType x)
{
assert(pphead&&pos);
SLNode* prev = *pphead;
if (*pphead == pos)
SLPushFront(pphead, x);
else
{
while (prev->next != pos)
{
prev = prev->next;
}
SLNode* newnode = BuyNode(x);
newnode->next = pos;
prev->next = newnode;
}
}
void SLInsertAfter(SLNode** pphead, SLNode* pos, SLDateType x)
{
assert(pphead && pos);
SLNode* newnode = BuyNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SLErase(SLNode** pphead, SLNode* pos)
{
assert(pphead && pos);
if (pos == *pphead)
{
SLPopFront(pphead);
}
else
{
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SLEraseAfter(SLNode* pos)
{
assert(pos->next);
SLNode* next = pos->next;
pos->next = pos->next->next;
free(next);
}
void SLDestroy(SLNode** pphead)
{
assert(pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void TestSList1()
{
SLNode* plist = NULL;
SLPushFront(&plist, 0);
SLPushFront(&plist, 7);
SLPushFront(&plist, 2);
SLPushFront(&plist, 0);
SLPrint(plist);
}
void TestSList2()
{
SLNode* plist = NULL;
SLPushBack(&plist, 0);
SLPushBack(&plist, 7);
SLPushBack(&plist, 2);
SLPushBack(&plist, 0);
SLPrint(plist);
}
void TestSList3()
{
SLNode* plist = NULL;
SLPushBack(&plist, 0);
SLPushBack(&plist, 7);
SLPushBack(&plist, 2);
SLPushBack(&plist, 0);
SLPopFront(&plist);
SLPrint(plist);
}
void TestSList4()
{
SLNode* plist = NULL;
SLPushFront(&plist, 0);
SLPushFront(&plist, 7);
SLPushFront(&plist, 2);
SLPushFront(&plist, 0);
printf("%p\n", SLSearch(plist, 7));
}
void TestSList5()
{
SLNode* plist = NULL;
SLPushBack(&plist, 0);
SLPushBack(&plist, 7);
SLPushBack(&plist, 2);
SLPushBack(&plist, 0);
SLInsert(&plist, SLSearch(plist, 7), 6);
SLInsertAfter(&plist, SLSearch(plist, 2), 6);
SLErase(&plist, SLSearch(plist, 6));
SLEraseAfter(SLSearch(plist, 2));
SLDestroy(&plist);
}
int main()
{
TestSList5();
return 0;
}
测试函数的功能时最好写成test函数形式,不要写成菜单形式,便于调试。