头文件声明
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <windows.h>
typedef int DataType;
typedef struct Node
{
struct Node * _pNext;
DataType _data;
}Node, *PNode;
void SListInit(PNode * pHead);
void SListPushBlack(PNode* pHead, DataType data);
void SListPopBlack(PNode *pHead);
void SListPushFront(PNode *pHead, DataType data);
void SListPopFront(PNode *pHead);
void SListInsert(PNode pHead,PNode pos, DataType data);
void SListErase(PNode* pHead, PNode pos);
PNode SListFind(PNode pHead, DataType data);
int SListSize(PNode pHead);
int SListEmpty(PNode pHead,DataType data);
void SListDestroy(PNode* pHead);
void SListPrint(PNode pHead);
PNode BuySListNode(DataType Data);
void TestPush();
void test();
函数实现
#include "SList.h"
void SListInit(PNode *pHead)
{
assert(pHead);
*pHead = NULL;
}
void SListPushBlack(PNode *pHead, DataType data)
{
PNode pCur = *pHead;
assert(pHead);
if (NULL == *pHead)
{
*pHead = BuySListNode(data);
}
else
{
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext = BuySListNode(data);
}
}
void SListPopBlack(PNode *pHead)
{
PNode pCur = *pHead;
PNode pTail = NULL;
assert(pHead);
if (NULL == *pHead)
{
return;
}
else if (NULL == (*pHead)->_pNext)
{
free(*pHead);
*pHead = NULL;
}
else
{
while (pCur->_pNext)
{
pTail = pCur;
pCur = pCur->_pNext;
}
free(pCur);
pTail->_pNext = NULL;
}
}
void SListPushFront(PNode *pHead, DataType data)
{
assert(pHead);
PNode pNewNode = BuySListNode(data);
pNewNode->_pNext = (*pHead);
*pHead = pNewNode;
}
void SListPopFront(PNode *pHead)
{
PNode pDelNode = NULL;
assert(pHead);
if (NULL == *pHead)
{
return;
}
else
{
pDelNode = *pHead;
*pHead = (*pHead)->_pNext;
free(pDelNode);
pDelNode = NULL;
}
}
void SListInsert(PNode pHead, PNode pos, DataType data)
{
PNode pNewNode = NULL;
if (NULL == pHead||pos == NULL)
{
printf("链表不存在!\n");
return;
}
else
{
pNewNode = BuySListNode(data);
pNewNode->_pNext = pos->_pNext;
pos->_pNext = pNewNode;
}
}
void SListErase(PNode *pHead, PNode pos)
{
PNode pNext = NULL;
assert(pHead);
if (NULL == *pHead||NULL == pos)
{
printf("输入有误!\n");
return;
}
else
{
pNext = pos->_pNext;
pos->_pNext = pNext->_pNext;
pos->_data = pNext->_data;
free(pNext);
pNext->_pNext = NULL;
}
}
int SListSize(PNode pHead,DataType data)
{
PNode pCur = pHead;
int count = 0;
if (NULL == pHead)
{
printf("链表为空!\n");
}
while (pCur)
{
if (pCur->_data == data)
{
count++;
}
pCur = pCur->_pNext;
}
return count;
}
int SListEmpty(PNode pHead)
{
if (NULL == pHead)
{
return 0;
}
else
{
return 1;
}
}
void SListDestroy(PNode* pHead)
{
PNode pCur = pHead;
PNode pNext = NULL;
assert(pHead);
if (NULL == *pHead)
{
return;
}
else
{
pCur = *pHead;
*pHead = NULL;
while (pCur)
{
pNext = pCur->_pNext;
free(pCur);
pCur = pNext;
}
}
}
PNode BuySListNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
perror("pNewNode");
exit(EXIT_FAILURE);
}
pNewNode->_data = data;
pNewNode->_pNext= NULL;
return pNewNode;
}
void SListPrint(PNode pHead)
{
PNode pCur = pHead;
if (NULL == pHead)
{
printf("链表为空!!\n");
return;
}
while (pCur)
{
printf("%d->", pCur->_data);
pCur = pCur->_pNext;
}
printf("NULL\n");
}
PNode SListFind(PNode pHead, DataType data)
{
PNode pCur = pHead;
if (NULL == pHead)
{
printf("链表为空!\n");
return NULL;
}
while (pCur)
{
pCur = pCur->_pNext;
if (pCur->_data == data)
{
return pCur;
}
}
return NULL;
}
void test()
{
PNode pHead,pCur;
SListInit(&pHead);
SListPushBlack(&pHead, 1);
SListPushBlack(&pHead, 3);
SListPushBlack(&pHead, 6);
SListPrint(pHead);
pCur = SListFind(pHead, 3);
SListInsert(pHead, pCur, 9);
SListPrint(pHead);
pCur = SListFind(pHead, 6);
SListErase(&pHead, pCur);
SListPrint(pHead);
int ret = SListSize(pHead,2);
SListDestroy(&pHead);
SListEmpty(pHead);
SListPrint(pHead);
}
void TestPush()
{
PNode pHead;
SListInit(&pHead);
SListPushBlack(&pHead, 1);
SListPushBlack(&pHead, 3);
SListPushBlack(&pHead, 6);
SListPushBlack(&pHead, 2);
SListPrint(pHead);
}