单链表的基本操作

来源:互联网 发布:电脑连wifi软件 编辑:程序博客网 时间:2024/05/30 05:04
1)list.h#include < stdio.h>#include < stdlib.h>#include < assert.h>#include < windows.h>#include < malloc.h>typedef int DataType;typedef struct strNode{   DataType _data;   struct strNode *next;} Node,*pNode;void Initlist(pNode *pHead){   assert(pHead);  //建立头结点   *pHead=NULL;    // 建立空的单链表pHead   //其中pHead是指向单链表的头结点的指针,*pHead相当于待初始化单链表的头指针变量}void CreateList(pNode pHead)         //建表{     pHead = (Node*) malloc(sizeof(Node));   //建立链表,并未链表分配空间     if(NULL == pHead)       {          return ;       }       else       {            pHead->_data = 0;            pHead->next = NULL;       }}pNode BuyNode(DataType data) //建立结点{    pNode pTemp =(pNode)malloc(sizeof(Node));  //申请新结点    if(pTemp)      {      pTemp->_data = data;      pTemp->next = NULL;   }   return pTemp;}int SizeList(pNode pHead)  //求链表的长度{    int count=0;    pNode pCurNode=pHead;   //定义一个指针,使其指向头结点    while(pCurNode)        //当当前指针所指结点为NULL时,链表遍历完毕,退出循环语句    {      count++;      pCurNode=pCurNode->next;  //指向向后移    }    printf("the list length is :%d\n",count);    return count;             //返回链表长度}void PushBack(pNode* pHead,DataType data)//尾插{    pNode pNew=BuyNode(data);  //建立新节点    pNode pCur=*pHead;//定义一个指针使其指向头结点    pNode pPre=NULL;       pNew->next=NULL;               pNew->_data =data;      if(*pHead==NULL)     //链表为空时的情况    {        *pHead=pNew;     //新结点直接插到头结点后面    }    else    {        while(pCur)      //        {            pPre=pCur;  //将当期指针保留到pPre            pCur=pCur->next;        }        pPre->next=pNew;      }}void PopBack(pNode* pHead)//尾删{    pNode pCurNode=*pHead;    pNode pDelNode=*pHead;    pDelNode=pCurNode;    //将当期指针保留到pDelNode中    if(*pHead==NULL)    {        return;    }    else if((*pHead)->next==NULL)    {       free(*pHead);       *pHead=NULL;    }    else    {        while(pCurNode->next)  //如果当前指针的下一个指向空,就到了结尾,不再进入此循环        {            pDelNode= pCurNode;            pCurNode=pCurNode->next;        }        free(pCurNode);  //删除当前结点        pDelNode->next=NULL;    }}void PushFront(pNode* pHead,DataType data)//头插{    pNode pNew=BuyNode(data);    pNew->next=NULL;    pNew->_data =data;    //pNode pCur=*pHead;    if(*pHead==NULL)    {        *pHead=pNew;    }    else    {        pNew->next=*pHead;        *pHead=pNew;    }}void PopFront(pNode* pHead)  //头删{    pNode pCurNode=*pHead;    pNode pDelNode=*pHead;    pDelNode=pCurNode;    if(*pHead==NULL)    {        return;    }    else    {        *pHead=pCurNode->next;        free(pDelNode);    }}pNode Find(pNode pHead,const DataType data)  //在链表中找一个结点{    pNode pCurNode=pHead;    if(NULL==pHead)        return NULL;    while(pCurNode)    {        if(pCurNode->_data==data)            return pCurNode;        pCurNode=pCurNode->next; //如果没有找到将当前指针向后移    }    return NULL;}void Insert(pNode pos,const DataType data)//向链表中插入一个结点{    pNode pNewNode=NULL;    if(pos==NULL)        return;      pNewNode=BuyNode(data);      pNewNode->next =pos->next;      pos->next=pNewNode;}void Erase(pNode *pHead, pNode pos)////删除某个位置上的结点{    pNode pCurNode=*pHead;    pNode pCurNext=*pHead;    if(*pHead==NULL)        return;    else if(*pHead==pos)    {        *pHead=pos->next;        free(pos);    }    pCurNext= pCurNode->next;    while(pos!=pCurNext)    {        pCurNode=pCurNext;        pCurNext=pCurNext->next;    }    pCurNode->next=pos->next;    free(pos);}void Remove(pNode* pHead, DataType data)//  删除链表中某值的结点{    assert(pHead);    Erase(pHead,Find(*pHead,data));}void RemoveAll(pNode* pHead, DataType data)//将链表中所有某值的结点都删除{    pNode pCur = NULL;    pNode pPre = NULL;    assert(pHead);    pPre =pCur = *pHead;    while(pCur->next)    {              if(pCur->_data == data)        {            pPre->next = pCur->next;            free(pCur);            pCur = pPre;        }       else       {          pPre=pCur;          pCur=pCur->next;       }    }    if((*pHead)->_data == data)    {        pCur = *pHead;        *pHead = (*pHead)->next;        free(pCur);    }}int Empty(pNode pHead)    //判空{      if(pHead==NULL)          return 0;      else          return 1;}void Clear(pNode pHead) //清空{    pNode pCurNode=NULL;    if(pHead!=NULL)    {        pCurNode=pHead;         while(pHead)        {            pCurNode=NULL;            pCurNode=pCurNode->next;        }    }}void Destroy(pNode* pHead)  //销毁单链表{       free(*pHead);       //pHead=NULL;}void PrintList(pNode pHead)   //打印链表{     pNode pCurNode=pHead;    if(NULL==pHead)        return;    while(pCurNode)    {        printf("%d-> ",pCurNode->_data);        pCurNode=pCurNode->next;    }    printf("NULL\n");}pNode ResversePrintList(pNode pHead)     //逆置链表{    pNode pCurNode=pHead;    pNode pCurNext=pHead;    pNode pNewHead=NULL; //定义新链表    while(pHead)    {        if(NULL==pHead ||NULL==pHead->next)  //空链表或者只有一个结点       {           return pHead;       }       pCurNext=pCurNode->next;        while(pCurNext)       {            pCurNode->next= pNewHead;            pNewHead=pCurNode;            pCurNode=pCurNext;            pCurNext=pCurNext->next;        }    }      pCurNode->next= pNewHead;      pNewHead=pCurNode;      return pNewHead;}

2)list.c

#include < stdlib.h>#include < stdio.h>#include"List.h"void FunTest() //建表、头插、打印 、链长{    pNode pHead=NULL;    pNode p=NULL;    CreateList(pHead);    printf("*****PushFront()*****\n");    PushFront(&pHead,1);    PushFront(&pHead,2);    PushFront(&pHead,3);    PushFront(&pHead,4);    PushFront(&pHead,5);    PushFront(&pHead,6);    PushFront(&pHead,7);    PushFront(&pHead,8);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****PopFront()*****\n");    PopFront(&pHead);    PopFront(&pHead);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****Find()*****\n");    PushBack(&pHead,4);    PushBack(&pHead,5);    PrintList(pHead); // 尾插后打印这个链表    SizeList(pHead);  // 求这个链表的长度    p=Find(pHead,2);    PrintList(pHead); // 尾插后打印这个链表}void FunTest1(){    pNode pHead=NULL;    pNode p=NULL;    CreateList(pHead);    printf("*****PushBack()*****\n");    PushBack(&pHead,1);    PushBack(&pHead,2);    PushBack(&pHead,3);    PushBack(&pHead,4);    PushBack(&pHead,5);    PushBack(&pHead,6);    PushBack(&pHead,7);    PushBack(&pHead,8);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****ResversePrintList()*****\n");    ResversePrintList(pHead);    PrintList(pHead); // 尾插后打印这个链表    printf("*****Find()*****\n");    p=Find(pHead,2);    PrintList(pHead); // 打印这个链表    printf("*****Insert()*****\n");    Insert(Find(pHead,2),10);  //找到2,并在其后插上10    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度}void FunTest2(){    pNode pHead=NULL;    pNode p=NULL;    CreateList(pHead);    printf("*****PushFront()*****\n");    PushFront(&pHead,1);    PushFront(&pHead,2);    PushFront(&pHead,3);    PushFront(&pHead,4);    PushFront(&pHead,5);    PushFront(&pHead,6);    PushFront(&pHead,7);    PushFront(&pHead,8);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    PopBack(&pHead);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    Insert(pHead,23);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****Erase()*****\n");     Erase(&pHead,Find(pHead,5));    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度}void FunTest3(){    pNode pHead=NULL;    pNode p=NULL;    CreateList(pHead);    PushBack(&pHead,1);    PushBack(&pHead,2);    PushBack(&pHead,3);    PushBack(&pHead,4);    PushBack(&pHead,5);    PushBack(&pHead,6);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****Remove()*****\n");    Remove(&pHead,4);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    PushFront(&pHead,4);    PushFront(&pHead,8);    PushFront(&pHead,3);    PushFront(&pHead,8);    PushFront(&pHead,4);    PushFront(&pHead,6);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    printf("*****RemoveAll()*****\n");    RemoveAll(&pHead,8);    PrintList(pHead); // 打印这个链表    SizeList(pHead);  // 求这个链表的长度    Destroy(&pHead);}int main(){    FunTest();    FunTest1();    FunTest2();    FunTest3();    system("pause");    return 0;}

这里写图片描述

这里写图片描述

这里写图片描述

1 0
原创粉丝点击