#include "linkedlist.h"
/**
* * @brief 逆序打印单链表.
* *
* * @param head
* */
void LinkListReversePrint(LinkNode* head)
{
if(head==NULL)
{
return;
}
LinkListReversePrint(head->next);
printf("%c<-");
}
/**
* * @brief 不允许遍历链表, 在 pos之前插入
* *
* * @param head
* * @param pos
* * @param value
* */
void LinkListInsertBefore(LinkNode** head, LinkNode* pos, LinkType value)
{
if(head==NULL)
{
return;
}
if(*head==NULL)
{
return;
}
if(pos==NULL)
{
return;
}
LinkNode*cur=(LinkNode*)malloc(sizeof(LinkNode));
cur->data=pos->data;
cur->next=pos->next;
pos->data=value;
pos->next=cur;
}
LinkNode* JosephCycle(LinkNode* head, size_t food)
{
if(head==NULL)
{
printf("The head pointer is NULL!");
return;
}
LinkNode* to_delete=NULL;
size_t i=0;
while(head!=head->next)
{
for(i=1;i<food;i++)
{
head=head->next;
}
to_delete=head->next;
head->data=to_delete->data;
head->next=to_delete->next;
free(to_delete);
to_delete=NULL;
}
return head;
}
/**
* * @brief 单链表逆置
* *
* * @param head
* */
void LinkListReverse(LinkNode** head)
{
if(head==NULL)
{
return;
}
if(*head==NULL)
{
return;
}
LinkNode* top=NULL;
LinkNode* to_delete=NULL;
LinkNode* pre=NULL;
LinkNode* cur=NULL;
top=*head;
pre=*head;
cur=(*head)->next;
while(cur!=NULL)
{
to_delete=cur;
pre->next= to_delete->next;
to_delete->next=top;
top=to_delete;
pre=cur;
cur=cur->next;
}
*head=top;
}
//void LinkListReverse2(LinkNode** head)
/**
* * @brief 单链表的冒泡排序
* *
* * @param head
* */
void LinkListBubbleSort(LinkNode* head)
{
if(head==NULL)
{
return;
}
LinkNode* cur=NULL;
LinkNode* tail=NULL;
cur=head;
while(cur!=tail)
{
while(cur->next!=tail)
{
if(cur->data>cur->next->data)
{
char tmp=cur->data;
cur->data=cur->next->data;
cur->next->data=tmp;
}
cur=cur->next;
}
tail=cur;
cur=head;
}
}
/**
* * @brief 将两个有序链表, 合并成一个有序链表
* *
* * @param head1
* * @param head2
* *
* * @return
* */
LinkNode* LinkListMerge(LinkNode* head1, LinkNode* head2)
{
LinkNode* first=NULL;
LinkNode* tail =NULL;
first=(head1->data>head2->data? head2:head1);
tail=first;
while(head1!=NULL && head2!=NULL)
{
if(head1->data<=head2->data)
{
tail->next=head1;
head1=head1->next;
}
else if(head1->data>head2->data)
{
tail->next =head2;
head2=head2->next;
}
}
if(head1==NULL)
{
tail->next=head2;
}
else if(head2=NULL)
{
tail->next=head2;
}
return first;
}
/**
* * @brief 找到倒数第 K 个节点.
* *
* * @param head
* *
* * @return
* */
LinkNode* FindLastKNode(LinkNode* head, size_t K)
{
LinkNode*cur=head;
LinkNode*fast=NULL;
LinkNode*slow=head;
size_t i=0;
for(i=1;i<K;i++)
{
cur=cur->next;
}
fast=cur;
while(fast->next!=NULL)
{
slow=slow->next;
fast=fast->next;
}
return slow;
}
/**
* * @brief 删除倒数第K个节点
* *
* * @param head
* * @param K
* */
void EraseLastKNode(LinkNode** head, size_t K)
{
if(head==NULL)
{
return;
}
if(*head==NULL)
{
return;
}
LinkNode*cur=*head;
LinkNode*fast=NULL;
LinkNode*slow=*head;
LinkNode*pre=NULL;
size_t i=0;
for(i=1;i<K;i++)
{
cur=cur->next;
}
fast=cur;
while(fast->next!=NULL)
{
pre=slow;
slow=slow->next;
fast=fast->next;
}
if(pre==NULL)
{
pre=*head;
*head=(*head)->next;
free(pre);
}else
{
pre->next=slow->next;
free(slow);
}
}
/**
* * @brief 判定单链表是否带环. 如果带环返回1
* *
* * @param head
* *
* * @return
* */
LinkNode* HasCycle(LinkNode* head)
{
if(head==NULL)
{
return;
}
LinkNode*fast=head;
LinkNode*slow=head;
while(fast->next!=NULL && fast->next->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
printf("Hascycle");
return slow;
}
}
return NULL;
}
/**
* * @brief 如果链表带环, 求出环的长度
* *
* * @param head
* *
* * @return
* */
size_t GetCycleLen(LinkNode* head)
{
LinkNode*pos=HasCycle(head);
LinkNode*cur=pos;
cur=cur->next;
size_t i=1;
while(cur!=pos)
{
cur=cur->next;
i++;
}
return i;
}
/**
* * @brief 如果链表带环, 求出环的入口
* *
* * @param head
* *
* * @return
* */
LinkNode* GetCycleEntry(LinkNode* head)
{
if(head==NULL)
{
return;
}
LinkNode* pos =HasCycle(head);
LinkNode*first=head;
if(pos==NULL)
{
return NULL;
}
while(first!=pos)
{
first=first->next;
pos=pos->next;
}
return pos;
}
/**
* * @brief 判定两个链表是否相交, 并求出交点
* *
* * @param head1
* * @param head2
* *
* * @return
* */
LinkNode* HasCross(LinkNode* head1, LinkNode* head2)
{
if(head1==NULL)
{
return ;
}
if(head2==NULL)
{
return ;
}
size_t count1=0;
size_t count2=0;
LinkNode*pos1=HasCycle(head1);
LinkNode*pos2=HasCycle(head2);
//Two no cycle linkedlist.
if(pos1==NULL && pos2==NULL)
{
LinkNode*cur1=head1;
LinkNode*cur2=head2;
while(cur1->next!=NULL)
{
cur1=cur1->next;
count1++;
}
while(cur2->next!=NULL)
{
cur2=cur2->next;
count2++;
}
if(cur1==cur2)
{
printf("The two linkedlist is intersect.");
//to find the intersect point.
if(count1>count2)
{
size_t offset=count1-count2;
LinkNode*fore=head1;
LinkNode*after=head2;
for(offset;offset>0;offset--)
{
fore=fore->next;
}
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}else if(count1<count2){
size_t offset=count2-count1;
LinkNode*fore=head2;
LinkNode*after=head1;
for(offset;offset>0;offset--)
{
fore=fore->next;
}
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}else{
LinkNode*fore=head1;
LinkNode*after=head2;
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}
}else{
printf("The two linkdelist is not intersect.");
}
}
//Two linkedlist both have cycle.(two situations)
else if(pos1!=NULL && pos2!=NULL)
{
//fisrt to find the entry point for the two cycles.
LinkNode*cur1=head1;
LinkNode*cur2=head2;
while(cur1!=pos1)
{
cur1=cur1->next;
pos1=pos1->next;
}
while(cur2!=pos2)
{
cur2=cur2->next;
pos2=pos2->next;
}
//same entry point.
if(pos1==pos2)
{
printf("The two linkedlist is intersect.");
// find the intersect point.
LinkNode*tail=pos1;
cur1=head1;
cur2=head2;
while(cur1->next!=NULL)
{
cur1=cur1->next;
count1++;
}
while(cur2->next!=NULL)
{
cur2=cur2->next;
count2++;
}
if(cur1==cur2)
{
if(count1>count2)
{
size_t offset=count1-count2;
LinkNode*fore=head1;
LinkNode*after=head2;
for(offset;offset>0;offset--)
{
fore=fore->next;
}
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}else if(count1<count2)
{
size_t offset=count2-count1;
LinkNode*fore=head2;
LinkNode*after=head1;
for(offset;offset>0;offset--)
{
fore=fore->next;
}
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}else
{
LinkNode*fore=head1;
LinkNode*after=head2;
while(fore!=after)
{
fore=fore->next;
after=after->next;
}
return fore;
}
}
}
// different entry point
else
{
cur1=pos1->next;
while(cur1!=pos1)
{
if(cur1==pos2)
{
//two linkedlist is intersect.
printf("The two linkedlist is intersect.");
return pos1;
}
cur1=cur1->next;
}
}
}
else
{
// One has cycle another don't have cycle.
printf("The two linkedlist has no intersect.");
}
return NULL;
}
int main()
{
return;
}
链表笔试题1
最新推荐文章于 2024-05-15 08:33:51 发布