#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define DateType int
typedef struct Node
{
DateType data;
struct Node* next;
}Node, *pNode;
void InitList(pNode *head)
{
assert(head);
(*head) = NULL;
}
pNode BuyNode(DateType e)
{
pNode lk = (pNode)malloc(sizeof(Node));
if (!lk)
return NULL;
lk->data = e;
return lk;
}
void PushBack(pNode *head, DateType e)
{
pNode nd, tmp; assert(head);
nd = BuyNode(e);
if (!nd)
return;
else if (*head == NULL)
{
*head = nd; nd->next = NULL;
}
else
{
tmp = *head;
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = nd;
nd->next = NULL;
}
}
void PopBack(pNode *head)
{
pNode tmp;
pNode q = NULL;
assert(head);
if (*head == NULL)
return;
tmp = *head;
while (tmp->next)
{
q = tmp;
tmp = tmp->next;
}
if (q != NULL)
{
q->next = NULL;
free(tmp);
}
else
{
free(tmp);
*head = NULL;
}
}
void PrintList(pNode head)
{
pNode tmp = head;
while (tmp)
{
printf("%d-->", tmp->data);
tmp = tmp->next;
}
printf("NULL\n");
}
void PushFront(pNode *head, DateType e)
{
pNode nd = BuyNode(e);
assert(head);
if (nd)
{
nd->next = *head;
*head = nd;
}
}
void PopFront(pNode *head)
{
pNode tmp;
assert(head);
if (!*head) return;
tmp = *head;
*head = (*head)->next;
free(tmp);
}
pNode Find(pNode head, DateType e)
{
pNode tmp;
assert(head);
tmp = head;
while (tmp->data != e)
{
tmp = tmp->next;
if (tmp == NULL)
return NULL;
}
return tmp;
}
void Inster(pNode *head, pNode pos, DateType e)
{
pNode nd;
assert(head);
if (pos && *head)
{
nd = BuyNode(e);
nd->next = pos->next;
pos->next = nd;
}
}
void Erase(pNode *head, pNode pos)
{
pNode tmp;
pNode q = NULL;
assert(head);
if (*head && pos)
{
tmp = *head;
while (tmp != pos)
{
q = tmp;
tmp = tmp->next;
}
if (q != NULL)
{
q->next = tmp->next;
free(tmp);
}
else
{
free(tmp);
*head = NULL;
}
}
}
int LenList(pNode head)
{
int count = 0;
pNode tmp = head;
while (tmp)
{
tmp = tmp->next;
++count;
}
return count;
}
void Destroy(pNode *head)
{
pNode tmp = *head;
pNode del = NULL;
while (tmp)
{
del = tmp;
tmp = tmp->next;
free(del);
del = NULL;
}
*head = NULL;
}
void deleteNotTailNode(pNode pos)
{
pNode posNext = NULL;
assert(pos);
if (pos == NULL || pos->next == NULL)
return;
posNext = pos->next;
pos->data = posNext->data;
pos->next = posNext->next;
free(posNext);
posNext = NULL;
}
void insetNothead(pNode pos, DateType d)
{
pNode pnewNode = (pNode)malloc(sizeof(Node));
assert(pos);
if (pos == NULL)
return;
pnewNode->next = pos->next;
pnewNode->data = pos->data;
pos->next = pnewNode;
pos->data = d;
}
pNode yosephcircle(pNode phead, size_t num)
{
int count = 0;
pNode pcurNext = NULL;
pNode pcur = NULL;
assert(phead);
if (phead == NULL || phead->next == NULL)
return NULL;
pcur = phead;
while (pcur->next)
{
pcur = pcur->next;
}
pcur->next = phead;
pcur = phead;
while (pcur->next != pcur)
{
count=num-1;
while (--count)
{
pcur = pcur->next;
}
pcurNext = pcur->next;
pcur->next = pcurNext->next;
free(pcurNext);
pcurNext = NULL;
pcur = pcur->next;
}
pcur->next = NULL;
return pcur;
}
void printlistlast(pNode head)
{
if (head)
{
printlistlast(head->next);
printf("%d-->",head->data);
}
}
void Destroylistlast(pNode* phead)
{
assert(phead);
if (*phead)
{
Destroylistlast((*phead)->next);
free(*phead);
*phead = NULL;
}
}
pNode findminNode(pNode phead)
{
pNode pslow = phead;
pNode pfast = phead;
while (pfast&&pfast->next)
{
pslow = pslow->next;
pfast = pfast->next->next;
}
return pslow;
}
void reverselist(pNode *phead)
{
pNode pcur = NULL;
pNode pcurNext = NULL;
pNode pPre = NULL;
assert(phead);
if (phead == NULL || (*phead)->next == NULL)
return ;
pPre=pcur = *phead;
pcurNext = pcur->next;
while (pcurNext)
{
pcur->next = pcurNext->next;
pPre = pcurNext;
pcurNext = pcurNext->next;
pPre->next = (*phead);
(*phead) = pPre;
}
}
void test1()
{
pNode list;
pNode pos;
InitList(&list);
PushBack(&list, 1);
PushBack(&list,2);
PushBack(&list,3);
PushFront(&list,0);
PopFront(&list);
PrintList(list);
pos = Find(list, 2);
Inster(&list, pos, 233);
PrintList(list);
pos = Find(list, 233);
Erase(&list, pos);
PrintList(list);
PopBack(&list);
PopBack(&list);
pos = Find(list,1);
Erase(&list, pos);
PrintList(list);
Destroy(&list);
}
void test2()
{
pNode list;
pNode pos;
pNode q;
InitList(&list);
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PrintList(list);
pos = Find(list, 2);
deleteNotTailNode(pos);
PrintList(list);
insetNothead(pos,2);
PrintList(list);
PushBack(&list, 4);
PushBack(&list, 5);
PushBack(&list, 6);
PushBack(&list, 7);
PrintList(list);
q = yosephcircle(list,3);
printf("%d\n",q->data);
Destroy(&list);
PrintList(list);
}
void test3()
{
pNode list;
pNode q;
InitList(&list);
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
printlistlast(list);
printf("\n");
PushBack(&list, 4);
PushBack(&list, 5);
PushBack(&list, 6);
PushBack(&list, 7);
PrintList(list);
q=findminNode(list);
printf("%d\n",q->data);
PushBack(&list, 8);
PrintList(list);
q = findminNode(list);
printf("%d\n", q->data);
reverselist(&list);
PrintList(list);
Destroylistlast(&list);
PrintList(list);
}
int main()
{
test3();
system("pause");
return 0;
}