SList.h
#include#include #include 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);
test.c
#include"SList.h"
void testSList1()
{
SLTNode* plist = NULL;
SLTPushFront(&plist, 1);
SLTPushFront(&plist, 2);
SLTPushFront(&plist, 3);
SLTPushFront(&plist, 4);
SLTPushFront(&plist, 5);
SLTPushFront(&plist, 6);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPopBack(&plist);
SLTPopBack(&plist);
SLTPrint(plist);
}
void testSList2()
{
SLTNode* plist = NULL;
SLTPushFront(&plist, 1);
SLTPushFront(&plist, 2);
SLTPushFront(&plist, 3);
SLTPushFront(&plist, 4);
SLTPushFront(&plist, 5);
SLTPushFront(&plist, 6);
SLTPrint(plist);
SLTPopFront(&plist);
SLTPopFront(&plist);
SLTPopFront(&plist);
SLTPopFront(&plist);
SLTPrint(plist);
}
void testSList3()
{
SLTNode* plist = NULL;
SLTPushBack(&plist, 1);
SLTPushBack(&plist, 2);
SLTPushBack(&plist, 3);
SLTPushBack(&plist, 4);
SLTPushBack(&plist, 5);
SLTPushBack(&plist, 6);
SLTPrint(plist);
SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n1);
SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n2);
SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n3);
SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n4);
n1->date = 7;
n2->date = 7;
n3->date = 7;
n4->date = 7;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
SLTPrint(n1);
SLTInsert(&n1, n3, 6);
SLTInsert(&n1, n3, 6);
SLTInsert(&n1, n3, 6);
SLTInsert(&n1, n3, 6);
SLTInsert(&n1, n3, 6);
SLTPrint(n1);
SLTErase(&n1, n1->next);
SLTErase(&n1, n1->next);
SLTErase(&n1, n1->next);
SLTInsertAfter(n1,60);
SLTInsertAfter(n1, 60);
SLTInsertAfter(n1, 60);
SLTInsertAfter(n1, 60);
SLTInsertAfter(n1, 60);
SLTEraseAfter(n1);
SLTEraseAfter(n1);
SLTEraseAfter(n1);
SLTEraseAfter(n1);
SLTEraseAfter(n1);
SLTPrint(n1);
}
int main()
{
testSList3();
return 0;
}
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;
}



