//打印
void SlistPrint(SLTNode* phead)
{
SLTNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->date);
cur = cur->next;//指向下一个元素的地址
}
printf("NULL\n");
}
//创建元素结点
SLTNode* BuyListNode(SLTDateType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->date = x;
newnode->next = NULL;
return newnode;
}
//尾插
void SlistPushBack(SLTNode** phead, SLTDateType x)//phead接收SLD*指针的地址
{
assert(phead);
SLTNode* newnode =BuyListNode(x);
if (*phead == NULL)
{
*phead = newnode;//链表中没有元素时,将指针指向newnode
}
else
{
SLTNode* tail = *phead;
while (tail->next != NULL)//找到尾结点
{
tail = tail->next;
}
tail->next = newnode;//新增的元素地址赋给尾结点
}
}
//头插
void SlistPushFront(SLTNode** phead, SLTDateType x)
{
assert(phead);
SLTNode* newnode = BuyListNode(x);
newnode->next = *phead;//新结点存入phead指向的地址
*phead = newnode;//phead的地址改为newnade
}
//尾删
void SlistPopBack(SLTNode** phead)
{
assert(phead);
if ((*phead)->next == NULL)
{
free(*phead);
*phead = NULL;
}
else
{
SLTNode* tail = *phead;
SLTNode* prev = NULL;
while (tail->next != NULL)//找到尾结点
{
prev = tail;//记录尾节点上一个结点的地址
tail = tail->next;
}
free(tail);//释放尾结点空间
tail = NULL;
prev->next = NULL;//上一个结点地址置为空
}
//SLTNode* tail = *phead;
//while (tail->next->next)//二级访问,使tail停在尾结点的上一个结点
//{
// tail = tail->next;
//}
//free(tail->next);//释放上一个结点指向的尾结点空间
//tail->next = NULL;//置为空指针
}
//头删
void SlistPopFront(SLTNode** phead)
{
assert(*phead != NULL);
/*SLTNode* head = *phead;
if ((*phead)->next == NULL)
{
free(*phead);
*phead = NULL;
}
else
{
*phead = (*phead)->next;
free(head);
head = NULL;
}*/
SLTNode* cur = (*phead)->next;
free(*phead);
*phead = cur;
}
//查找
SLTNode* SlistFind(SLTNode* phead, SLTDateType x)
{
assert(phead);
SLTNode* cur = phead;
while (cur)
{
if (cur->date == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;//找不到返回NULL
}
//指定结点位置前插入
void SlistInsert(SLTNode** phead, SLTNode* pos, SLTDateType x)
{
assert(phead);
assert(pos);
SLTNode* newnode = BuyListNode(x);
if (pos == *phead)//如果pos在头部位置,进行头插
{
SlistPushFront(phead, x);
}
else
//找到pos的前一个位置
{
SLTNode* posPrev = *phead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newnode;//将newnade地址赋给pos前的结点
newnode->next = pos;//newnade结点指向pos的位置
}
}
//指定结点位置后插入
void SlistInsertAfter( SLTNode* pos, SLTDateType x)
{
assert(pos->next != NULL);
SLTNode* newnode = BuyListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
//指定结点位置删除
void SlistEase(SLTNode** phead, SLTNode* pos)
{
assert(phead);
if(*phead == pos)//头删
{
SlistPopFront(phead);
}
else
{
SLTNode* Prev = *phead;
while(Prev->next !=pos)//找到指向pos的结点
{
Prev = Prev->next;
}
Prev->next = pos->next;//将指向pos结点,改为pos指向的下一个结点
free(pos);//释放pos的空间
pos = NULL;
}
}
//删除pos的下一个结点
void SlistEaseAfter( SLTNode* pos)
{
assert(pos->next != NULL);
SLTNode* next = pos->next;
pos->next = next->next;
free(next);
next = NULL;
}
//销毁
void SlistDestory(SLTNode** phead)
{
assert(phead);
SLTNode* cur = *phead;
while (cur)
{
SLTNode* next = cur->next;
free(cur);
cur = next;
}
*phead = NULL;
}