SList.h
typedef int DataType;
typedef struct SListNode
{
struct SListNode* _next;
DataType _data;
}SListNode;
SListNode* BuySListNode(DataType x);
void SListPrint(SListNode* pHead);
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 SListInsert(SListNode** ppHead, SListNode* pos, DataType x);
void SListErase(SListNode** pHead,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)
{
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* tail = *ppHead;
while (tail->_next)
{
tail = tail->_next;
}
tail->_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* tail = *ppHead;
while (tail->_next)
{
prev = tail;
tail = tail->_next;
}
prev->_next = NULL;
free(tail);
}
}
void Test1()
{
SListNode* list = NULL;
SListPushBack(&list, 1);
SListPushBack(&list, 2);
SListPushBack(&list, 3);
SListPushBack(&list, 4);
SListPrint(list);
SListPopBack(&list);
SListPopBack(&list);
SListPopBack(&list);
SListPopBack(&list);
SListPrint(list);
}
//头插
void SListPushFront(SListNode** ppHead, DataType x)
{
//空
if (*ppHead == NULL)
{
*ppHead = BuySListNode(x);
}
else//非空
{
SListNode* newNode = BuySListNode(x);
newNode->_next = *ppHead;
*ppHead = newNode;
}
}
//头删
void SListPopFront(SListNode** ppHead)
{
//空
if (*ppHead == NULL)
{
return;
}
//一个
else if ((*ppHead)->_next == NULL)
{
free(*ppHead);
*ppHead = NULL;
}
//一个及以上
else
{
SListNode* next = (*ppHead)->_next;
free(*ppHead);
*ppHead = next;
}
}
//查找
SListNode* SListFind(SListNode* pHead, DataType x)
{
SListNode* cur = pHead;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void Test2()
{
SListNode* list = NULL;
SListPushFront(&list, 4);
SListPushFront(&list, 3);
SListPushFront(&list, 2);
SListPushFront(&list, 1);
SListPrint(list);
//SListPopFront(&list);
//SListPopFront(&list);
//SListPopFront(&list);
//SListPopFront(&list);
//SListPrint(list);
SListNode* node = SListFind(list, 4);
printf("%d\n", node->_data);
}
//任意位置插入,在pos的前面插入
void SListInsert(SListNode** ppHead, SListNode* pos, DataType x)
{
assert(*ppHead&&pos);
//一个结点
if (pos == *ppHead)
{
SListPushFront(ppHead, x);
}
else
{
//任意位置
SListNode* newnode = BuySListNode(x);
SListNode* prev = *ppHead;
while (prev->_next != pos)
{
prev = prev->_next;
}
prev->_next = newnode;
newnode->_next = pos;
}
}
//任意位置删除
void SListErase(SListNode** ppHead, SListNode* pos)
{
assert(ppHead && pos);
if (*ppHead == pos)
{
SListPopFront(ppHead);
}
else
{
SListNode* prev = *ppHead;
while (prev->_next != pos)
{
prev = prev->_next;
}
prev->_next = pos->_next;
free(pos);//如果free(pos),会导致找不到pos的下一节点
}
}
void Test3()
{
SListNode* list = NULL;
SListNode* pos = NULL;
SListPushFront(&list, 4);
SListPushFront(&list, 3);
SListPushFront(&list, 2);
SListPushFront(&list, 1);
SListPrint(list);
pos = SListFind(list, 4);
SListInsert(&list, pos, 6);
SListPrint(list);
pos = SListFind(list, 4);
SListErase(&list, pos);
SListPrint(list);
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "SList.h"
int main()
{
//Test1(); //尾插/删
//Test2();//头插/删,查找
//Test3();//任意位置插入,删除
system("pause");
return 0;
}