头文件函数
#ifndef __SListNode_H__
#define __SListNode_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);
void test1();//头插
void test2();//头删
void test3();//尾插
void test4();//尾删
void test5();//查找
void test6();//插入
void test7();//删除
void test7();//销毁
#endif//__SListNode_H__
功能函数
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<Windows.h>
#include"SListNode.h"
SListNode* BuySListNode(DataType x)//申请节点 已测
{
SListNode* newNode = (SListNode*)malloc(sizeof(SListNode)*2+3);
newNode->_data = x;
newNode->_next = NULL;
return newNode;
}
void SListPrint(SListNode* pHead)//打印链表 已测
{
if (pHead == NULL)
{
printf("这是一个空链表!!!\n");
return;
}
SListNode* prev = pHead;
while (prev != NULL)
{
printf("%d ", prev->_data);
prev = prev->_next;
}
printf("\n");
}
void SListDestory(SListNode** ppHead)//销毁链表 已测
{
assert(ppHead);
SListNode* prev = *ppHead;
while ((*ppHead) != NULL)
{
prev = prev->_next;
free(*ppHead);
*ppHead = prev;
}
}
void SListPushBack(SListNode** ppHead, DataType x)//尾插 已测
{
assert(ppHead);
SListNode* newNode;
if (*ppHead == NULL)
{
*ppHead = BuySListNode(x);
return;
}
SListNode* prev = *ppHead;
while (prev->_next != NULL)
{
prev = prev->_next;
}
newNode = BuySListNode(x);
prev->_next = newNode;
}
void SListPopBack(SListNode** ppHead)//尾删 已测
{
assert(ppHead);
if ((*ppHead)->_next == NULL)
{
free(*ppHead);
*ppHead = NULL;
return;
}
SListNode* prac = *ppHead;
SListNode* prev = *ppHead;
while (prac->_next != NULL)
{
prev = prac;
prac = prac->_next;
}
prev->_next = NULL;
free(prac);
}
void SListPushFront(SListNode** ppHead, DataType x)//头插 已测
{
assert(ppHead);
if (*ppHead == NULL)
{
*ppHead = BuySListNode(x);
return;
}
SListNode* prev = BuySListNode(x);
prev->_next = *ppHead;
*ppHead = prev;
}
void SListPopFront(SListNode** ppHead)//头删 已测
{
assert(ppHead);
if ((*ppHead)->_next==NULL)
{
free(*ppHead);
*ppHead = NULL;
return;
}
SListNode* prev = *ppHead;
*ppHead = prev->_next;
free(prev);
}
SListNode* SListFind(SListNode* pHead, DataType x)//查找 已测
{
assert(pHead);
SListNode* prev = pHead;
while (x != prev->_data)
{
if (prev->_next == NULL)
{
printf("找不到\n");
return -1;
}
else
{
prev = prev->_next;
}
}
return prev;
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)//插入 已测
{
assert(ppHead);
SListNode* newNode;
SListNode* prev = *ppHead;
if (pos == *ppHead)
{
SListNode* prev = BuySListNode(x);
prev->_next = *ppHead;
*ppHead = prev;
return;
}
while (prev->_next != pos)
{
prev = prev->_next;
}
newNode = BuySListNode(x);
prev->_next = newNode;
newNode->_next = pos;
}
void SListErase(SListNode** ppHead, SListNode* pos)//删除 已测
{
assert(*ppHead&&pos);
if (pos == *ppHead)
{
if ((*ppHead)->_next == NULL)
{
free(*ppHead);
*ppHead = NULL;
return;
}
SListNode* prev = *ppHead;
*ppHead = prev->_next;
free(prev);
return;
}
if (pos->_next == NULL)
{
if ((*ppHead)->_next == NULL)
{
free(*ppHead);
*ppHead = NULL;
return;
}
SListNode* prac = *ppHead;
SListNode* prev = *ppHead;
while (prac->_next != NULL)
{
prev = prac;
prac = prac->_next;
}
prev->_next = NULL;
free(prac);
return;
}
SListNode* prev = *ppHead;
while ((*ppHead)->_next != pos)
{
prev = *ppHead;
*ppHead = (*ppHead)->_next;
}
prev->_next = pos->_next;
free(pos);
}
测试用例
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"SListNode.h"
void test1()//头插
{
SListNode *list = NULL;
SListPushFront(&list, 1);
SListPrint(list);
SListPushFront(&list, 2);
SListPrint(list);
SListPushFront(&list, 3);
SListPrint(list);
SListPushFront(&list, 4);
SListPrint(list);
SListPushFront(&list, 5);
SListPrint(list);
}
void test2()//头删
{
SListNode *list = NULL;
SListPushFront(&list, 1);
SListPrint(list);
SListPushFront(&list, 2);
SListPrint(list);
SListPushFront(&list, 3);
SListPrint(list);
SListPushFront(&list, 4);
SListPrint(list);
SListPushFront(&list, 5);
SListPrint(list);
printf("\n");
SListPopFront(&list);
SListPrint(list);
SListPopFront(&list);
SListPrint(list);
SListPopFront(&list);
SListPrint(list);
SListPopFront(&list);
SListPrint(list);
SListPopFront(&list);
SListPrint(list);
}
void test3()//尾插
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPrint(list);
SListPushBack(&list, 2);
SListPrint(list);
SListPushBack(&list, 3);
SListPrint(list);
SListPushBack(&list, 4);
SListPrint(list);
SListPushBack(&list, 5);
SListPrint(list);
}
void test4()//尾删
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPrint(list);
SListPushBack(&list, 2);
SListPrint(list);
SListPushBack(&list, 3);
SListPrint(list);
SListPushBack(&list, 4);
SListPrint(list);
SListPushBack(&list, 5);
SListPrint(list);
printf("\n");
SListPopBack(&list);
SListPrint(list);
SListPopBack(&list);
SListPrint(list);
SListPopBack(&list);
SListPrint(list);
SListPopBack(&list);
SListPrint(list);
SListPopBack(&list);
SListPrint(list);
}
void test5()//查找
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPushBack(&list, 2);
SListPushBack(&list, 3);
SListPushBack(&list, 4);
SListPushBack(&list, 5);
SListPrint(list);
printf("\n");
printf("%d\n", SListFind(list, 1)->_data );
printf("%d\n", SListFind(list, 2)->_data );
printf("%d\n", SListFind(list, 3)->_data );
printf("%d\n", SListFind(list, 4)->_data );
printf("%d\n", SListFind(list, 5)->_data );
printf("%d\n", SListFind(list, 6));
}
void test6()//插入
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPushBack(&list, 2);
SListPushBack(&list, 3);
SListPushBack(&list, 4);
SListPushBack(&list, 5);
SListPrint(list);
printf("\n");
SListNode *pos = SListFind(list, 3);
SListInsest(&list, pos, 50);
SListPrint(list);
pos = SListFind(list, 5);
SListInsest(&list, pos, 49);
SListPrint(list);
SListInsest(&list, NULL, 48);//尾插
SListPrint(list);
SListInsest(&list, list, 47);//头插
SListPrint(list);
}
void test7()//删除
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPushBack(&list, 2);
SListPushBack(&list, 3);
SListPushBack(&list, 4);
SListPushBack(&list, 5);
SListPrint(list);
printf("\n");
SListNode *pos = SListFind(list, 1);//头删
SListErase(&list, pos);
SListPrint(list);
pos = SListFind(list, 5);//尾删
SListErase(&list, pos);
SListPrint(list);
pos = SListFind(list, 3);
SListErase(&list, pos);
SListPrint(list);
}
void test8()//销毁
{
SListNode *list = NULL;
SListPushBack(&list, 1);
SListPushBack(&list, 2);
SListPushBack(&list, 3);
SListPushBack(&list, 4);
SListPushBack(&list, 5);
SListPrint(list);
SListDestory(&list);
SListPrint(list);
}
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"SListNode.h"
#include<Windows.h>
int main()
{
printf("头插测试:>\n");
test1();
printf("\n头删测试:>\n");
test2();
printf("\n尾插测试:>\n");
test3();
printf("\n尾删测试:>\n");
test4();
printf("\n查找测试:>\n");
test5();
printf("\n插入测试:>\n");
test6();
printf("\n删除测试:>\n");
test7();
printf("\n销毁测试:>\n");
test8();
system("pause");
return 0;
}