//摧毁链表void DestroyLinkList(pList* pplist)
{
assert(pplist);
pNode cur =*pplist;
pNode del = cur;
while (cur)
{
del = cur;
cur = cur->next;
free(del);
del =NULL;
}
*pplist =NULL;
}
//4.单链表实现约瑟夫环(JosephCircle)
pNode JosephCircle(pNode plist, const int M)
{
int m = M;
pNode cur =NULL;
pNode del =NULL;
if (plist ==NULL)
{
returnNULL;
}
cur = plist;
while (cur->next != cur)
{
m = M;
while (--m)
{
cur = cur->next;
}
del = cur->next;
cur->next = del->next;
cur->data= del->data;
free(del);
del =NULL;
}
return cur;
}
//6.单链表排序(冒泡排序&快速排序)void BubbleSort(pNode* pplist)
{
int flag =0;
pNode cur =NULL;
pNode pre =NULL;
pNode tail =NULL;
assert(pplist);
cur =*pplist;
pre = cur->next;
if (*pplist ==NULL|| (*pplist)->next ==NULL)
{
return;
}
while (pre != tail)
{
while (pre != tail)
{
if (cur->data> pre->data)
{
DataType temp = cur->data;
cur->data= pre->data;
pre->data= temp;
}
cur = cur->next;
pre = pre->next;
}
tail = cur;
cur =*pplist;
pre = cur->next;
}
}//冒泡排序
//8.查找单链表的中间节点,要求只能遍历一次链表
pNode FindMiddleNode(pNode plist)
{
pNode fast = NULL;
pNode slow = NULL;
if (plist == NULL)
{
returnNULL;
}
fast = plist;
slow = plist;
while (fast && fast->next)//fast为NULL时有偶数个节点,fast->next为NULL时有奇数个节点
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
//9.查找单链表的倒数第K个节点,要求只能遍历一次链表
pNode FindInverseKNode(pNode plist, constint K)
{
pNode fast = NULL;
pNode slow = NULL;
int k = K;
if (plist == NULL || K <= 0)
{
returnNULL;
}
fast = plist;
slow = plist;
while (k--)
{
if (fast == NULL)
{
returnNULL;
}
fast = fast->next;
}
while (fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
//10.删除链表的倒数第K个节点void DeleteInverseKNode(pNode* pplist, const int K)
{
int k = K;
pNode fast =NULL;
pNode slow =NULL;
pNode pre =NULL;
assert(pplist);
if (K <=0)
{
return ;
}
fast =*pplist;
slow =*pplist;
while (k--)
{
if (fast ==NULL)
{
return ;
}
fast = fast->next;
}
while (fast)
{
pre = slow;
slow = slow->next;
fast = fast->next;
}
if (slow ==*pplist)
{
*pplist = slow->next;
free(slow);
slow =NULL;
}
else
{
pre->next = slow->next;
free(slow);
slow =NULL;
}
}
//11.判断单链表是否带环?若带环,求环的长度?求环的入口?//判断是否带环
pNode IsListCircle(pNode plist)
{
pNode fast = plist;
pNode slow = plist;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
return slow;
}
}
returnNULL;
}
//求环的长度
int ListCiecleLen(pNode meet)
{
int count =1;
pNode cur = meet;
if (cur ==NULL)
{
return0;
}
while (cur->next != meet)
{
count++;
cur = cur->next;
}
return count;
}
//求环的入口
pNode ListCircleEnter(pNode plist, pNode meet)
{
pNode cur = plist;
pNode pre = meet;
if (plist ==NULL|| meet ==NULL)
{
returnNULL;
}
while (cur != pre)
{
cur = cur->next;
pre = pre->next;
}
return cur;
}
//12.判断两个链表是否相交,若相交,求交点(假设链表不带环)//判断是否相交
int IsListCross(pNode plist1, pNode plist2)
{
pNode cur = plist1;
pNode pre = plist2;
if (plist1 ==NULL|| plist2 ==NULL)
{
return0;
}
while (cur->next !=NULL)
{
cur = cur->next;
}
while (pre->next !=NULL)
{
pre = pre->next;
}
if (cur == pre)
{
return1;
}
else
{
return0;
}
}
//求交点
pNode ListCrossPoint(pNode plist1, pNode plist2)
{
int count1 =0;
int count2 =0;
int count;
pNode cur = plist1;
pNode pre = plist2;
while (cur)
{
count1++;
cur = cur->next;
}
while (pre)
{
count2++;
pre = pre->next;
}
count = count1 - count2;
cur = plist1;
pre = plist2;
if (count >0)
{
while (count--)
{
cur = cur->next;
}
}
elseif (count <0)
{
while (count++)
{
pre = pre->next;
}
}
while (cur != pre)
{
cur = cur->next;
pre = pre->next;
}
return cur;
}
//13.判断两个链表是否相交,若相交,求交点(假设链表可能带环)【升级版】//判断是否相交
int IsListCross_C(pNode plist1, pNode plist2)
{
pNode temp;
pNode cur = plist1;
pNode pre = plist2;
pNode list1_c = IsListCircle(plist1);
pNode list2_c = IsListCircle(plist2);
if (plist1 ==NULL|| plist2 ==NULL)
{
return0;
}
if (((list1_c ==NULL) && list2_c) || (list1_c && (list2_c ==NULL)))
{
return0;
}
if (list1_c && list2_c)
{
temp = list1_c;
while (temp->next != list1_c)
{
if (temp == list2_c)
{
return2;
}
temp = temp->next;
}
if (temp == list2_c)
{
return2;
}
return0;
}
return IsListCross(plist1, plist2);
}
//求相交的交点
pNode ListCrossPoint_C(pNode plist1, pNode plist2)
{
int count1 =0;
int count2 =0;
int count;
pNode cur = plist1;
pNode pre = plist2;
pNode meet1 = IsListCircle(plist1);
pNode meet2 = IsListCircle(plist2);
pNode enter1 = ListCircleEnter(plist1, meet1);
pNode enter2 = ListCircleEnter(plist2, meet2);
if (enter1 == enter2)
{
while (cur != meet1)
{
count1++;
cur = cur->next;
}
while (pre != meet1)
{
count2++;
pre = pre->next;
}
count = count1 - count2;
cur = plist1;
pre = plist2;
if (count >0)
{
while (count--)
{
cur = cur->next;
}
}
elseif (count <0)
{
while (count++)
{
pre = pre->next;
}
}
while (cur != pre)
{
cur = cur->next;
pre = pre->next;
}
return cur;
}
returnNULL;
}