SList.h
#pragma once
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
typedef int SDataType;
typedef struct SListNode
{
SDataType data;
struct SListNode* _pNext;
}Node;
typedef struct SList
{
Node* _pHead;
}SList;
void SListInit(SList* pl);
void SListPushBack(SList* pl,SDataType data);
void SListPopBack(SList* pl);
void SListDestroy(SList* pl);
void PrintSList(SList* pl);
void TestList();
void SListPushFront(SList* pl, SDataType data);
void SListPopFront(SList* pl);
void SListInsert(Node* pos, SDataType data);
void SListErase(SList* pl,Node* pos);
Node* SListFind(SList* pl, SDataType data);
int SListSize(SList* pl);
int SListEmpty(SList* pl);
Node* SListFront(SList* pl);
Node* SListBack(SList* pl);
SList.c
#include"单链表.h"
void SListInit(SList* pl)
{
assert(pl);
pl->_pHead = NULL;
}
Node* BuySListNode(SDataType data)
{
Node* pNode = (Node*)malloc(sizeof(Node));
if (NULL == pNode)
{
assert(0);
return NULL;
}
pNode->data = data;
pNode->_pNext = NULL;
return pNode;
}
void SListPushBack(SList* pl, SDataType data)
{
Node* pCur = NULL;
Node* pNewNode = NULL;
assert(pl);
pNewNode = BuySListNode(data);
pCur = pl->_pHead;
if (NULL == pl->_pHead)
pl->_pHead = pNewNode;
else
{
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext = pNewNode;
pNewNode->_pNext = NULL;
}
}
void PrintSList(SList* pl)
{
Node* pCur = NULL;
assert(pl);
pCur = pl->_pHead;
while (pCur)
{
printf("%d--->", pCur->data);
pCur = pCur->_pNext;
}
printf("NULL\n");
}
void SListDestroy(SList* pl)
{
Node* pCur = NULL;
assert(pl);
pCur = pl->_pHead;
while (pCur)
{
pl->_pHead = pCur->_pNext;
free(pCur);
pCur = pl->_pHead;
}
pl->_pHead = NULL;
}
void SListPopBack(SList* pl)
{
Node* pCur = NULL;
assert(pl);
if (NULL == pl->_pHead)
{
return;
}
else if (NULL == pl->_pHead->_pNext)
{
free(pl->_pHead);
pl->_pHead = NULL;
}
else
{
Node* pTailNode = pl->_pHead;
while (pTailNode->_pNext->_pNext)
{
pTailNode = pTailNode->_pNext;
}
free(pTailNode->_pNext);
pTailNode->_pNext = NULL;
}
}
void SListPushFront(SList* pl, SDataType data)
{
Node* pNewNode = NULL;
assert(pl);
pNewNode = BuySListNode(data);
pNewNode->_pNext = pl->_pHead;
pl->_pHead = pNewNode;
}
void SListPopFront(SList* pl)
{
assert(pl);
if (NULL == pl->_pHead)
return;
else
{
Node* pDelNode = pl->_pHead;
pl->_pHead = pDelNode->_pNext;
free(pDelNode);
}
}
void SListInsert(Node* pos, SDataType data)
{
Node* pNewNode = NULL;
if (NULL == pos)
return;
pNewNode = BuySListNode(data);
pNewNode->_pNext = pos->_pNext;
pos->_pNext = pNewNode;
}
void SListErase(SList* pl,Node* pos)
{
Node* pPrePos = NULL;
assert(pl);
if (NULL == pl->_pHead || NULL == pos)
return;
if (pos == pl->_pHead)
{
pl->_pHead = pos->_pNext;
free(pos);
return;
}
pPrePos = pl->_pHead;
while (pPrePos->_pNext != pos)
{
pPrePos = pPrePos->_pNext;
}
pPrePos->_pNext = pos->_pNext;
free(pos);
}
Node* SListFind(SList* pl, SDataType data)
{
Node* pCur = pl->_pHead;
assert(pl);
while (pCur)
{
if (pCur->data == data)
return pCur;
pCur = pCur->_pNext;
}
return NULL;
}
int SListSize(SList* pl)
{
int count = 0;
Node* pCur = NULL;
assert(pl);
pCur = pl->_pHead;
while (pCur)
{
count++;
pCur = pCur->_pNext;
}
return count;
}
int SListEmpty(SList* pl)
{
assert(pl);
retrun NULL == pl->_pHead;
}
Node* SListFront(SList* pl)
{
assert(pl);
return pl->_pHead;
}
Node* SListBack(SList* pl)
{
Node* pCur = NULL;
assert(pl);
pCur = pl->_pHead;
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
return pCur;
}
main.c
#include"单链表.h"
void TestList1()
{
SList s;
SListInit(&s);
SListPushBack(&s, 1);
SListPushBack(&s, 2);
SListPushBack(&s, 3);
SListPushBack(&s, 4);
SListPushBack(&s, 5);
PrintSList(&s);
SListPopBack(&s);
PrintSList(&s);
SListPushFront(&s, 10);
SListPushFront(&s, 20);
SListPushFront(&s, 30);
SListPushFront(&s, 40);
SListPushFront(&s, 50);
PrintSList(&s);
SListPopFront(&s);
SListPopFront(&s);
PrintSList(&s);
SListInsert(SListFind(&s, 2),66);
PrintSList(&s);
SListErase(&s, SListFind(&s, 66));
PrintSList(&s);
SListDestroy(&s);
}
void TestList()
{
TestList1();
}
int main()
{
TestList();
return 0;
}