链表实现

来源:互联网 发布:公安网络备案 编辑:程序博客网 时间:2024/05/16 07:19
////////////////////////////////////////////////////初始化#include <stdio.h>#include <windows.h>#include <malloc.h>
typedef int DataType;typedef struct Node{ DataType data; struct Node *next;}Node, *PNode;void InitList(PNode *pHead){    *pHead = NULL;}//创建新节点PNode BuyNode(DataType data){    PNode pCur = (PNode)malloc(sizeof(Node));    pCur->data = data;    pCur->next = NULL;    return pCur;}//获取最后的结点PNode Back(PNode pHead){    PNode pCur = NULL;    if (NULL != pHead)    {        pCur = pHead;        while (NULL != pCur->next)        {            pCur = pCur->next;        }    }    return pCur;}//释放结点void Free(PNode pNode){    free(pNode);}//判断链表是否为空int NoEmpty(PNode pHead){    if (NULL != pHead)        return 1;    return 0;}//结点个数size_t Size(PNode pHead){    size_t count = 0;    if (NoEmpty(pHead))    {        while (NULL != pHead)        {            pHead = pHead->next;            count++;        }    }    return count;}//打印void Print(PNode *pHead){ PNode pCur = *pHead; while (pCur) {  printf("%d-> ", pCur->data);  pCur = pCur->next; } printf("\n");}///////////////////////////////////////////////////尾插法插入新的结点void PushBack(PNode *pHead, DataType data){    PNode pCur = Back(*pHead);    PNode pTmp = BuyNode(data);    if (NoEmpty(*pHead))    {        pCur->next = pTmp;    }    else    {        *pHead = pTmp;    }}//删除最后一个节点void PopBack(PNode *pHead){    if (NoEmpty(*pHead))    {        PNode pCur = *pHead;        while (NULL != pCur->next->next)        {            pCur = pCur->next;        }        Free(pCur->next);        pCur->next = NULL;    }}//头插法插入新的结点void PushFront(PNode *pHead, DataType data){    PNode pTmp = BuyNode(data);    PNode pCur = *pHead;    if (NoEmpty(*pHead))    {        *pHead = pTmp;        (*pHead)->next = pCur;    }    else    {        *pHead = pTmp;    }}//删除第一个节点void PopFront(PNode *pHead){    if (NoEmpty(*pHead))    {        PNode pCur = *pHead;        *pHead = (*pHead)->next;        Free(pCur);    }}///////////////////////////////////////////////////////查找值为data的结点(第一个),返回该节点的地址PNode Find(PNode pHead, DataType data){    PNode pCur = NULL;    if (NoEmpty(pHead))    {        pCur = pHead;        while (NULL != pCur->next && pCur->data != data)        {            pCur = pCur->next;        }    }    return pCur;}//在pos位置之后插入新节点void Insert(PNode pos, DataType data){    if (NULL != pos)    {        PNode pCur = pos->next;        pos->next = BuyNode(data);        pos->next->next = pCur;    }}//删除POS位置上的结点void Erase(PNode *pHead, PNode pos){    if (NULL != *pHead)    {        PNode pCur = *pHead;        while (pCur->next != pos)        {            pCur = pCur->next;        }        pCur->next = pos->next;        Free(pos);    }}//删除第一个值为data的结点void Remove(PNode *pHead, DataType data){    if (NULL != *pHead)    {        PNode pCur = *pHead;        while (pCur->data != data)        {            pCur = pCur->next;        }        Erase(pHead, pCur);    }}//删除全部值为data的结点void RemoveAll(PNode *pHead, DataType data){    if (NULL != *pHead)    {        PNode pCur = *pHead;        PNode pTmp = *pHead;        while (NULL != pTmp)        {            while (NULL != pCur && pCur->data != data)            {                pCur = pCur->next;                pTmp = pTmp->next;            }            if (NULL != pCur)            {                pTmp = pTmp->next;                Erase(pHead, pCur);                pCur = pTmp;            }        }    }}//删除链表void DeleteList(PNode *pHead){    if ((NULL != pHead) && (NULL != *pHead))    {        PNode pCur = *pHead;        while (pCur)        {            PNode pNext = pCur->next;            Free(pCur);            pCur = pNext;        }        *pHead = NULL;    }}int main(){ PNode pHead1 = NULL; PNode pHead2 = NULL; PushBack(&pHead1, 1); PushBack(&pHead1, 2); PushBack(&pHead1, 3); Print(&pHead1); PushFront(&pHead2, 4); PushFront(&pHead2, 5); PushFront(&pHead2, 6); Print(&pHead2); Insert(Back(pHead1), 3); Print(&pHead1); PopFront(&pHead1); Print(&pHead1); RemoveAll(&pHead1, 3); Print(&pHead1); system("pause"); return 0;}


0 0
原创粉丝点击