单链表的面试题

void DeleteNotTail(PNode pos);//删除一个无头单链表的非尾节点
void InsertNotHead(PNode pHead, PNode pos, DataType data);//在无头单链表的一个非头节点前插入一个节点
void JosephCircle(PNode* pHead, int M);//约瑟夫环
PNode FindMidNode(PNode pHead);//查找单链表的中间节点,要求只能遍历一次链表
PNode FindLastKNode(PNode pHead, int k);//查找单链表的倒数第k个节点,要求只能遍历一次链表
PNode ReverseList(PNode pHead);//逆置单链表
void BubbleSort(PNode pHead);//冒泡排序
PNode MergeList(PNode pList1, PNode pList2);//合并有序单链表,合并完仍有序
int IsCross(PNode pHead1, PNode pHead2);//判断是否相交
PNode GetCrossNode(PNode pHead1, PNode pHead2);//寻找两条链表相交的结点
void MakeCircle(PNode pHead);//使链表成环
int GetCirclrLen(PNode pMeetNode);//求链表环的长度
int GetNode(PNode pHead);//一条链表找相交点
PNode HasCircle(PNode pHead);//判断链表是否带环
void UnionSet(PNode p1, PNode p2);//求两个已排序单链表中相同的数据--打印出来

void DeleteNotTail(PNode pos)//删除无头非尾节点 
{
    if (pos == NULL || pos->next == NULL)
    {
        return;
    }
    else
    {
        PNode Nextpos = pos->next;
        PNode LastNextpos = Nextpos->next;
        pos->data = Nextpos->data;
        pos->next = LastNextpos;
        free(Nextpos);
        Nextpos = NULL;
    }
}
void InsertNotHead(PNode pHead, PNode pos, DataType data)//在链表的非头位置插入节点
{
    if (pHead == NULL || pos == NULL||pHead->next==NULL)
    {
        return;
    }
    else
    {
        PNode PreNode = NULL;
        PNode CurNode = pHead;
        PNode NewNode = CreatNode(data);
        while (CurNode != pos)
        {
            PreNode = CurNode;
            CurNode = CurNode->next;
        }
        NewNode->next = CurNode;
        PreNode->next = NewNode;
    }
}
void JosephCircle(PNode* pHead, int M)//约瑟夫环
{
    PNode CurNode = *pHead;
    PNode LastNode = NULL;
    PNode TailNode = NULL;//成环
    int count = M;
    if (*pHead == NULL)
    {
        printf("链表为空!\n");
    }
    else
    {
        TailNode = Back(*pHead);
        TailNode->next = *pHead;
        while (CurNode->next!=CurNode)
        {
            count = M;
            while (--count)
            {
                CurNode = CurNode->next;
            }
            LastNode = CurNode->next;
            CurNode->data = LastNode->data;
            CurNode->next = LastNode->next;
            free(CurNode->next);
            CurNode->next = NULL;
        }
    }
}
PNode FindMidNode(PNode pHead)//查找一个单链表的中间节点
{
    if (pHead == NULL)
    {
        printf("链表为空!\n");
    }
    else if (pHead->next == NULL)
    {
        return pHead;
    }
    else
    {
        PNode FastNode = pHead;
        PNode SlowNode = pHead;
        while ((FastNode->next)&&(FastNode->next->next))
        {
            FastNode = FastNode->next->next;
            SlowNode = SlowNode->next;
        }
        return SlowNode;
    }
}
PNode FindLastKNode(PNode pHead, int k)//查找链表的倒数第k个节点
{   
    PNode FastNode = pHead;
    PNode SlowNode = pHead;
    if (NULL == pHead)
    {
        printf("链表为空!\n");
    }
    else
    {
        while (k--)
        {
            FastNode = FastNode->next;
        }
        while (FastNode)
        {
            FastNode = FastNode->next;
            SlowNode = SlowNode->next;
        }
        return SlowNode;
    }   
}

 void MakeCircle(PNode pHead)
{
    if (pHead == NULL)
    {
        printf("链表为空!\n");
    }
    else
    {
        PNode CurNode = pHead;
        PNode PreNode = NULL;
        while (CurNode)//找最后一个结点
        {
            PreNode = CurNode;
            CurNode = CurNode->next;
        }
        PreNode->next = pHead;
    }
}

//void ReverseList(PNode pHead)//逆置单链表
//{
//  if (pHead)
//  {
//      ReverseList(pHead->next);
//      printf("%d->",pHead->data);
//  }
//}
PNode ReverseList(PNode pHead)
{
    PNode New = CreatNode(pHead->data);
    while (pHead->next)
    {
        pHead = pHead->next;
        PNode New1 = CreatNode(pHead->data);
        New1->next = New;
        New = New1;
    }
    return New;
}
void BubbleSort(PNode pHead)//冒泡排序
{
    PNode CurNode = pHead;
    PNode TailNode = NULL;
    int Data = 0;
    while (pHead != TailNode)
    {
        CurNode = pHead;
        while (TailNode != CurNode->next)
        {
            if ((CurNode->data) > (CurNode->next->data))
            {
                Data = CurNode->data;
                CurNode->data = CurNode->next->data;
                CurNode->next->data = Data;
            }
            CurNode = CurNode->next;
        }
        TailNode = CurNode;
    }
}
PNode MergeList(PNode pList1, PNode pList2)//合并有序单链表,合并完仍有序
{
    PNode New = NULL;
    PNode p1 = pList1;
    PNode p2 = pList2;  
    if ((p1->data) <= (p2->data))   
    {   
        New = p1;   
        p1 = p1->next;  
    }   
    else    
    {   
        New = p2;   
        p2 = p2->next;
    }   
    PNode New1 = New;
    while (p1 && p2)
    {
        if (p1->data <= p2->data)
        {
            New->next = p1;
            New = p1;
            p1 = p1->next;
        }
        else
        {
            New->next = p2;
            New = p2;
            p2 = p2->next;
        }
    }
    if ((p1 == NULL) && (p2!=NULL))
    {
        New->next = p2;
    }
    else if ((p1 != NULL) && (p2 == NULL))
    {
        New->next = p1;
    }
    return New1;
}
int IsCross(PNode pHead1, PNode pHead2)//判断是否相交
{//不带环的相交
    int count1 = 0;
    int count2 = 0;
    PNode CurNode1 = pHead1;
    PNode CurNode2 = pHead2;
    while (CurNode1)
    {
        count1++;
        CurNode1 = CurNode1->next;
    }
    while (CurNode2)
    {
        count2++;
        CurNode2 = CurNode2->next;
    }
    while (pHead1)
    {
        if (count1 >= count2)
        {
            int count = count1 - count2;
            while (count--)
            {
                pHead1 = pHead1->next;
            }
            if (pHead1 != pHead2)
            {
                pHead1 = pHead1->next;
                pHead2 = pHead2->next;
            }
            else
            {
                return 1;
            }
        }
        else
        {
            int count = count2 - count1;
            while (count--)
            {
                pHead2 = pHead2->next;
            }
            if (pHead1 != pHead2)
            {
                pHead1 = pHead1->next;
                pHead2 = pHead2->next;
            }
            else
            {
                return 1;
            }
        }
    }
    return -1;
}
PNode GetCrossNode(PNode pHead1, PNode pHead2)//寻找相交的结点
{
    int count1 = 0;
    int count2 = 0;
    PNode CurNode1 = pHead1;
    PNode CurNode2 = pHead2;
    while (CurNode1)
    {
        count1++;
        CurNode1 = CurNode1->next;
    }
    while (CurNode2)
    {
        count2++;
        CurNode2 = CurNode2->next;
    }
    while (pHead1)
    {
        if (count1 >= count2)
        {
            int count = count1 - count2;
            while (count--)
            {
                pHead1 = pHead1->next;
            }
            if (pHead1 != pHead2)
            {
                pHead1 = pHead1->next;
                pHead2 = pHead2->next;
            }
            else
            {
                return pHead1;
            }
        }
        else
        {
            int count = count2 - count1;
            while (count--)
            {
                pHead2 = pHead2->next;
            }
            if (pHead1 != pHead2)
            {
                pHead1 = pHead1->next;
                pHead2 = pHead2->next;
            }
            else
            {
                return pHead2;
            }
        }
    }
}
PNode HasCircle(PNode pHead)//判断链表是否带环
{
    PNode FastNode = pHead;
    PNode SlowNode = pHead;
    while (FastNode)
    {
        FastNode = FastNode->next->next;
        SlowNode = SlowNode->next;
        if (FastNode == SlowNode)
            return SlowNode;
    }
}
int GetCirclrLen(PNode pMeetNode)//求链表环的长度
{
    PNode CurNode = pMeetNode;
    int count = 1;
    while (CurNode->next != pMeetNode)
    {
        count++;
        CurNode = CurNode->next;
    }
    return count;
}
int GetNode(PNode pHead)//一条链表找相交点
{
    int count = GetCirclrLen(HasCircle(pHead));
    PNode FastNode = pHead;
    PNode SlowNode = pHead;
    while (count--)
    {
        FastNode = FastNode->next;
    }
    if (FastNode != SlowNode)
    {
        FastNode = FastNode->next;
        SlowNode = SlowNode->next;
    }
    return SlowNode->data;
}
void UnionSet(PNode p1, PNode p2)
{
    assert(p1);
    assert(p2);
    PNode pp1 = p1;
    PNode pp2 = p2;
    while (pp1)
    {
        pp2 = p2;
        while (pp2)
        {
            if (pp1->data == pp2->data)
            {
                printf("%d\n", pp1->data);
            }
            pp2 = pp2->next;
        }
        pp1 = pp1->next;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值