无头单链表的一些题

来源:互联网 发布:mac自带壁纸 编辑:程序博客网 时间:2024/06/04 00:52
#include<stdio.h>#include<assert.h>#include<stdlib.h>typedef int TypeData ;typedef struct Node{    TypeData _data;    struct Node * _next;}Node,LinkList,*pNode,*pList;void display(pList pList);//显示链表void DestroyList(pList *ppList);//销毁链表void InitList(pList *ppList);//初始化链表pNode BuyNode(TypeData data);//创建新节点void PushBack(pList* ppList, TypeData data);//后插一个数void PrintListReverse(pList pList);//逆序打印链表void DeleteNodeNotTail(pList*ppList,pNode Pos);//删除无头单链表的非尾节点(不能遍历链表)pNode FindNode(pList *ppList, TypeData data);//查找节点pNode JosephCircle(pList *ppList,size_t k);//约瑟夫环void ReverseList(pList *ppList);//逆序链表void BubbleSort(pList* ppList);//冒泡排序void MergeList(pList* ppList1, pList* ppList2,pList *ppList3);//合并两个有序链表pNode FindMidNode(pList *ppList);//查找中间节点pNode FindKeyNode(pList *ppList,size_t k);//查找倒数第K个节点void DelKeyNode(pList*ppList, size_t k);//删除倒数第K个节点pNode IsCircle(pList* ppList);//该链表是否带环,是的话返回相遇点指针pNode AccessNode(pList*ppList);//求带环链表的入口节点int LenthCircle(pList*ppList, pNode pStart);//如果带环 ,求环的长度void pop_back(pList* ppList)//后拿一个数{    pNode cur = NULL;    assert(ppList);    cur = *ppList;    if (cur == NULL)    {        printf("没有可删除的元素,删除失败!\n");        return;    }    else if (cur->_next == NULL)    {        free(cur);        *ppList = NULL;    }    else    {        while (cur->_next->_next)        {            cur = cur->_next;        }        cur->_next = NULL;    }}void DestroyList(pList *ppList){    assert(ppList);    if (*ppList == NULL)    {        return;    }    else    {        pNode cur = *ppList;        while (cur)        {            pNode tmp = cur;            cur = cur->_next;            free(tmp);            tmp = NULL;        }    }}void InitList(pList *ppList){    assert(ppList);    *ppList = NULL;}void display(pList pList){    if (pList== NULL)    {        printf("Emtyp List\n");    }    else    {        pNode pCur = pList;        while (pCur)        {            printf("%d-->", pCur->_data);            pCur = pCur->_next;        }        if (pCur == NULL)            {                printf("NULL\n");            }    }}pNode BuyNode(TypeData data){    pNode newNode = (pNode)malloc(sizeof(Node));    if (newNode == NULL)    {        perror("out of memory!");    }    newNode->_data = data;    newNode->_next = NULL;    return newNode;}//后插一个数void PushBack(pList* ppList, TypeData data){    pNode pCur = *ppList;    assert(ppList);    pNode newNode = BuyNode(data);    if (*ppList== NULL)    {        *ppList = newNode;        return;    }    else    {        while (pCur->_next!=NULL)        {            pCur= pCur->_next;        }        pCur->_next= newNode;    }}void PrintListReverse(pList pList){    if (pList != NULL)    {        if (pList->_next != NULL)        {            PrintListReverse(pList->_next);        }        printf("-->%d", pList->_data);    }}pNode FindNode(pList *ppList, TypeData data)//查找节点{    assert(ppList);    if (*ppList == NULL)    {        return NULL;    }    else    {        pNode pCur=*ppList;        while (pCur)        {            if (pCur->_data == data)                return pCur;            pCur = pCur->_next;        }    }    printf("not found\n");    return NULL;}void DeleteNodeNotTail(pList*ppList, pNode Pos){    assert(ppList);    if (*ppList == NULL)        return;    else    {        pNode del = Pos->_next;        Pos->_next = del->_next;        Pos->_data = del->_data;        free(del);        del = NULL;    }}void PopFront(pList *ppList, TypeData data){    pNode pCur = NULL;    assert(ppList);    if (*ppList == NULL)        *ppList = BuyNode(data);    else    {        pCur = BuyNode(data);        pCur->_next = *ppList;        *ppList = pCur;    }}pNode JosephCircle(pList *ppList, size_t k){      assert(ppList);    pNode pCur = *ppList;    size_t count;    if (*ppList == NULL)        return NULL;    while (1)    {        if (pCur->_next == pCur)            return pCur;            count = k;        while ((--count))//前置--            pCur = pCur->_next;//        pNode del = pCur->_next;        pCur->_data = del->_data;        pCur->_next = del->_next;        free(del);    }}void ReverseList(pList *ppList){    pNode cur=*ppList;//遍历链表指针    pNode newhead = NULL;//新的头指针    pNode temp = NULL;//临时指针    assert(ppList);    if ((*ppList == NULL) || ((*ppList)->_next) == NULL)//空链表或者链表有一个元素时 不用逆序        return;    while (cur!= NULL)    {        temp = cur;        cur=cur->_next;        temp->_next = newhead;        newhead = temp;    }    *ppList = newhead;}void BubbleSort(pList* ppList)//冒泡排序{    pNode pCur = NULL;    pNode temp = NULL;    //bool flag = true;    assert(ppList);    if ((*ppList == NULL) || ((*ppList)->_next) == NULL)//空链表或者链表有一个元素时 不用排序        return;    pCur = *ppList;    while (pCur)    {        //flag = true;        for (temp = *ppList; temp->_next != NULL; temp = temp->_next)        {            if ((temp->_data) > (temp->_next->_data))            {                TypeData d = temp->_data;                temp->_data = temp->_next->_data;                temp->_next->_data = d;                //flag = false;            }        }        pCur = pCur->_next;    //  if (flag == true)        //  break;    }}void MergeList(pList*  ppList1, pList  *ppList2,pList *ppList3){    pNode NewHead = NULL;    pNode Tail = NULL;    pNode cur1;    pNode cur2;    if (*ppList1 == NULL)    {        if (*ppList2 == NULL)        {            *ppList3 = *ppList1;            return;        }        else        {            *ppList3=*ppList2;            return;        }    }    else    {        if (*ppList2 == NULL)        {            *ppList3 = *ppList1;            return;        }    }    cur1 = *ppList1;    cur2 = *ppList2;    if (cur1->_data <cur2->_data)    {        NewHead = cur1;        cur1 = cur1->_next;    }    else    {        NewHead = cur2;        cur2= cur2->_next;    }    Tail = NewHead;    while (cur1&&cur2)    {        if (cur1->_data < cur2->_data)        {            Tail->_next= cur1;            cur1 = cur1->_next;            Tail = Tail->_next;        }        else        {            Tail->_next = cur2;            cur2 = cur2->_next;            Tail = Tail->_next;        }    }    while (cur1)    {        Tail->_next = cur1;        break;    }    while (cur2)    {        Tail->_next = cur2;        break;    }    *ppList3 = NewHead;}pNode FindMidNode(pList *ppList){    pNode pFast;    pNode pslow;    assert(ppList);    if ((*ppList) == NULL)        return NULL;    if ((*ppList)->_next == NULL)        return *ppList;    pFast = pslow = *ppList;    while (pFast&& (pFast->_next))    {        pslow = pslow->_next;        pFast = pFast->_next->_next;    }    return pslow;}pNode FindKeyNode(pList *ppList,size_t k){    pNode pFast = *ppList;    pNode pSlow = *ppList;    assert(ppList);    if (k >= 0)    {        while (k)        {            pFast = pFast->_next;            if (pFast == NULL)                return NULL;            --k;        }        while (pFast)        {            pFast=pFast->_next;            pSlow = pSlow->_next;        }        return pSlow;    }    return NULL;}void DelKeyNode(pList*ppList, size_t k)//删除倒数第K个节点{    pNode cur = *ppList;    pNode pFast = *ppList;    pNode pSlow = *ppList;    pNode del = NULL;    assert(ppList);    assert(k > 0);    if ((*ppList) == NULL)        return;    if (k > 1)    {        while (k)        {            pFast = pFast->_next;            if (pFast == NULL)                return ;            --k;        }        while (pFast)        {            pFast = pFast->_next;            pSlow = pSlow->_next;        }        del = pSlow->_next;        pSlow->_next = del->_next;        pSlow->_data = del->_data;        free(del);        del = NULL;    }    else        pop_back(ppList);}pNode IsCircle(pList* ppList)//该链表是否带环,是的话返回相遇点指针{    pNode pFast=*ppList;    pNode pSlow=*ppList;    assert(ppList);    if (*ppList == NULL)        return NULL;    while (pFast&&pFast->_next)    {        pFast = pFast->_next->_next;        pSlow = pSlow->_next;        if (pSlow == pFast)            return pSlow;    }    return NULL;}pNode AccessNode(pList*ppList)//求带环链表的入口节点{    pNode pStart;    pNode pSlow;    pNode temp;    assert(ppList);    temp = IsCircle(ppList);    if (temp == NULL)    {        printf("NO Circle");        return NULL;    }    pSlow = temp;    pStart = *ppList;    while (pStart)    {        pStart = pStart->_next;        pSlow = pSlow->_next;        if (pSlow == pStart)            return pStart;    }}int LenthCircle(pList*ppList, pNode pStart)//如果带环求环长度{    pNode pCur;    pNode temp;    int count = 0;    assert(ppList);    if (pStart == NULL||*ppList==NULL)        return 0;    temp= pCur = pStart;    while (pCur)    {        pCur = pCur->_next;        count++;        if (pCur == temp)            return count;       }}//void test2()//{//  pList List1;//  pList List2;//  //pList List3;//  InitList(&List1);//  InitList(&List2);//  //InitList(&List3);//  PushBack(&List1, 1);//  PushBack(&List2, 2);//  PushBack(&List1, 3);//  PushBack(&List2, 4);//  PushBack(&List1, 5);//  PushBack(&List2, 6);//  PushBack(&List2, 7);    //PushBack(&List1, 8);    /*ReverseList(&List);    BubbleSort(&List);*/ //MergeList(&List1, &List2,&List3);    //display(List1);    //display(List2);    //display(List3);    //DestroyList(&List1);    //DestroyList(&List2);    //DestroyList(&List3);//}//void Test()//{//  pList List;//  pNode temp;//  InitList(&List);//  PushBack(&List, 1);//  PushBack(&List, 2);//  PushBack(&List, 3);//  PushBack(&List, 4);//  PushBack(&List, 5);//  PushBack(&List, 6);//  PushBack(&List, 7);//  PushBack(&List, 8);//  //DelKeyNode(&List, 1);    //display(List);    //temp = FindMidNode(&List);    //temp = FindKeyNode(&List, 1);    //printf("%d", temp->_data);    //DestroyList(&List);//}void Test1(){    pList List;    pNode pTail = NULL;    pNode temp = NULL;    InitList(&List);    PushBack(&List, 1);    PushBack(&List, 2);    PushBack(&List, 3);    PushBack(&List, 4);    PushBack(&List, 5);    PushBack(&List, 6);    PushBack(&List, 7);    PushBack(&List, 8); pTail = FindNode(&List, 8);pTail->_next = FindNode(&List,3);    //PrintListReverse(List);//DeleteNodeNotTail(&List, FindNode(&List, 3));//temp=JosephCircle(&List, 3);temp = AccessNode(&List);    printf("%d\n", temp->_data);    printf("%d\n", LenthCircle(&List, temp));   // display(List);    //DestroyList(&List);}int main(){    //Test();    Test1();    return 0;}
0 0
原创粉丝点击