??????????????????????????????????????????β?壬??β?????α????????????м????????????????x?????????????????β????????????????????£?
#include "SLinkList.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
void PrintList(SListNode* pHead)//?????λ????????
{
while (pHead)
{
printf("->%d"?? pHead->data);
pHead = pHead->next;
}
printf(" ");
}
static SListNode* _BuyNode(DataType x)//?????μ???
{
SListNode*ret = (SListNode*)malloc(sizeof(SListNode));
ret->next = NULL;
ret->data = x;
if (ret)
{
return ret;
}
printf("?????????? ");//?????????????
return NULL;
}
void PushBack(SListNode* & pHead?? DataType x)//β??
{
if (pHead == NULL)
{
pHead = _BuyNode(x);
}
else
{
SListNode*tail = pHead;
while (tail->next)
{
tail = tail->next;
}
tail->next = _BuyNode(x);
}
}
void PopBack(SListNode* & pHead)//β?
{
if (pHead == NULL)
{
return;
}
else if (pHead->next->next == NULL)
{
free(pHead->next);
pHead = NULL;
}
else
{
SListNode* tail = pHead;
while (tail->next->next)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void PushFront(SListNode* & pHead?? DataType x)//???
{
if (pHead == NULL)
{
pHead = _BuyNode(x);
}
else
{
SListNode*tmp = _BuyNode(x);
tmp->next = pHead;
pHead = tmp;
}
}
void PopFront(SListNode* & pHead)//t??
{
if (pHead == NULL)
{
return;
}
else
{
SListNode*tail = pHead;
pHead = pHead->next;
tail->next = NULL;
free(tail);
}
}
SListNode* Find(SListNode *pHead?? DataType x)//??x?????????????????????????????
{
if (pHead == NULL)
{
return NULL;
}
SListNode* tail = pHead;
while (tail)
{
if (tail->data == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
void Insert(SListNode* & pos?? DataType x)//????λ?ú????????
{
SListNode*tmp = _BuyNode(x);
if (pos == NULL)
{
pos = tmp;
}
else
{
tmp->next = pos->next;
pos->next = tmp;
}
}
void Erase(SListNode * &pos)//????????????β???
{
if (pos == NULL)
{
return;
}
else if (pos->next == NULL)
{
printf("?y???β????????? ");
}
else
{
SListNode*tmp = pos->next;
pos->data = pos->next->data;
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}
}
void ReveseList(SListNode * &pHead)//?????????
{
SListNode*tail = pHead;
while (tail->next)
{
SListNode*tmp=NULL;
tmp = tail->next;
tail->next = tail->next->next;
tmp->next = pHead;
pHead = tmp;
}
}
SListNode* FindminList(SListNode*pHead)//??α??????????????м???
{
assert(pHead);
SListNode *quick=pHead;
SListNode *slow=pHead;
while (quick)
{
slow = slow->next;
if (quick->next)
quick = quick->next->next;
else
return slow;
}
return slow;
}
SListNode* FindListPosList(SListNode*pHead?? int lastpos)//?????????????lastpos?????
{
SListNode *quick = pHead;
SListNode *slow = pHead;
while (quick&&lastpos--)
{
quick = quick->next;
}
if (quick == NULL)
{
return slow;
}
while (quick)
{
quick = quick->next;
slow = slow->next;
}
return slow;
}