SList.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType date;
struct SListNode* next;
}SLTNode;
void SLTPrint(SLTNode* phead);
SLTNode* BuySLTNode(SLTDateType n);
void SLTPushFront(SLTNode** phead, SLTDateType n);
void SLTPopFront(SLTNode** phead);
void SLTPushBack(SLTNode** phead,SLTDateType n);
void SLTPopBack(SLTNode** phead);
SLTNode* SLTFind(SLTNode* phead, SLTDateType n);
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDateType n);
void SLTErase(SLTNode** phead, SLTNode* pos);
void SLTInsertAfter(SLTNode* pos, SLTDateType n);
void SLTEraseAfter(SLTNode* pos);
SList.c
#include"SList.h"
void SLTPrint(SLTNode* phead)
{
assert(phead);
while (phead)
{
printf("%d->", phead->date);
phead = phead->next;
}
printf("NULL");
printf("\n");
}
SLTNode* BuySLTNode(SLTDateType n)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->date = n;
newnode->next = NULL;
return newnode;
}
void SLTPushFront(SLTNode** phead, SLTDateType n)
{
assert(phead);
SLTNode* newnode = BuySLTNode(n);
newnode->date = n;
newnode->next = *phead;
*phead = newnode;
}
void SLTPopFront(SLTNode** phead)
{
assert(phead);
assert(*phead);
SLTNode* Front = (*phead)->next;
free(*phead);
*phead = Front;
}
void SLTPushBack(SLTNode** phead, SLTDateType n)
{
if (*phead == NULL)
{
*phead = BuySLTNode(n);
return;
}
SLTNode* newnode = BuySLTNode(n);
SLTNode* tail = *phead;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
void SLTPopBack(SLTNode** phead)
{
assert(phead);
assert(*phead);
if ((*phead)->next == NULL)
{
free(*phead);
*phead = NULL;
}
else
{
SLTNode* tail = *phead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
SLTNode* SLTFind(SLTNode* phead, SLTDateType n)
{
while (phead)
{
if (phead->date == n)
{
return phead;
}
phead = phead->next;
}
return NULL;
}
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDateType n)
{
if (pos == *phead)
{
SLTPushFront(phead,n);
}
else
{
SLTNode* cur = *phead;
while (cur->next != pos)
{
cur = cur->next;
}
SLTNode* newnode = BuySLTNode(n);
cur->next = newnode;
newnode->next = pos;
}
}
void SLTErase(SLTNode** phead, SLTNode* pos)
{
if (*phead == pos)
{
SLTPopFront(phead);
}
SLTNode* cur = *phead;
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = cur->next->next;
free(pos);
}
void SLTInsertAfter(SLTNode* pos, SLTDateType n)
{
SLTNode* newnode = BuySLTNode(n);
SLTNode* tmp = pos->next;
pos->next = newnode;
newnode->next = tmp;
}
void SLTEraseAfter(SLTNode* pos)
{
SLTNode* tmp = pos->next->next;
free(pos->next);
pos->next = tmp;
}