首页 > 代码库 > 带有头结点的链表的基本操作

带有头结点的链表的基本操作

#ifndef _LIST_h_#define _LIST_h_//链表中的数据结构typedef struct Link_data{    int a;       int b;}Node_data;//链表节点结构typedef struct Link_node{    Node_data    data;       struct Link_node  *pNext;}Node;Node* CreateList(void);Node* FindNodeByGlobalIndex(Node *pHead, int iGlobalIndex);Node* Insert2ListTail(Node *pHead, Node_data  *pAutoInfo);int RemoveList(Node *pHead);void  PrintList(Node *pHead);int DeleteList (Node *pHead,int x);void ReverseList(Node *pHead);void SortList(Node *pHead);#endif
#include <string.h>#include <malloc.h>#include<stdio.h>#include"list.h"/*************************************************Function      : CreateListDescription   : 创建链表头节点Return        : 链表的头指针*************************************************/Node* CreateList(void){    Node *pHead = NULL;    //申请的头指针    pHead = (Node *)malloc(sizeof(Node));    //判断是否申请成功    if (NULL == pHead)    {        return NULL;    }        //针对具体结构进行初始化    pHead->data.a = 0;    pHead->data.b = 0;    pHead->pNext = NULL;    return pHead;}/*************************************************Function      : FindNodeByGlobalIndexDescription   : 根据指定参数,查找某个节点Input         : pHead 链表的头节点指针                要查找的学生IDReturn        : 正确:返回指定节点的指针                失败:返回空指针*************************************************/Node* FindNodeByGlobalIndex(Node *pHead, int iGlobalIndex){    Node *pNode = NULL;    if ((NULL == pHead) || (iGlobalIndex < 0))    {        return NULL;    }    pNode = pHead->pNext;    while ((NULL != pNode))    {        if (pNode->data.a == iGlobalIndex)        {            break;        }        pNode = pNode->pNext;    }    return pNode;}/*************************************************Function      : Insert2ListTailDescription   : 向链表中尾部插入某个节点Input         : pHead        链表的头节点指针                pStudentInfo 学生信息Return        : 正确:返回头节点指针                失败:返回空指针*************************************************/Node* Insert2ListTail(Node *pHead, Node_data  *pAutoInfo){    Node* pNode = NULL;    Node* pNewNode = NULL;    if ((NULL == pHead) || (NULL == pAutoInfo))    {        return NULL;    }    pNode = pHead;    while (pNode->pNext != NULL)    {        pNode = pNode->pNext;    }    pNewNode = (Node *)malloc(sizeof(Node));    if (NULL == pNewNode)    {        return NULL;    }    pNode->pNext = pNewNode;    pNewNode->pNext = NULL;    memcpy(&(pNewNode->data), pAutoInfo, sizeof(Node_data ));    return pHead;}/*************************************************Function      : RemoveListDescription   : 删除整个链表Input         : pHead 链表的头节点指针Return        : 正确: 1                失败: 0*************************************************/int RemoveList(Node *pHead){    Node *pNode = NULL;    Node *pb = NULL;    if (NULL == pHead)    {        return 0;    }    pNode = pHead;    pb = pNode->pNext;    if (NULL == pb)    {        free(pNode);    }    else    {        while (NULL != pb)        {            free(pNode);            pNode = pb;            pb = pb->pNext;        }        free(pNode);    }    pNode = NULL;    return 1;}/*************************************************Function      : PrintListDescription   : 打印整个链表Input         : pHead 链表的头节点指针Return        : *************************************************/void  PrintList(Node *pHead){    Node *pNode = NULL;    if (NULL == pHead)    {        return ;    }    pNode = pHead->pNext;    while ((NULL != pNode))    {        printf("\r\n a is %d   b is  %d",pNode->data.a,pNode->data.b);        pNode = pNode->pNext;    }    return ;}/*************************************************Function      : DeleteListDescription   : 删除链表的一个结点,删除条件该节点的a值与x相同Input         : Return        : *************************************************/int DeleteList (Node *pHead,int x){    Node *pNode = NULL;    Node *pre = NULL;    if (NULL == pHead  )    {        return 0;    }    pNode = pHead->pNext;    pre = pHead;    while(pNode)    {        if(pNode->data.a == x)//删除条件        {            pre->pNext = pNode->pNext;            free(pNode);            return 1;        }        else        {            pre = pNode;        }        pNode = pNode->pNext;    }        return 0;}/*************************************************Function      : ReverseListDescription   : 链表反转Input         : Return        : *************************************************/void ReverseList(Node *pHead){    Node* p = pHead->pNext;    Node* q = p->pNext;    Node* t = NULL;    if(NULL == pHead || NULL == pHead->pNext)    {        return;    }    while(NULL != q)    {        t = q->pNext;        q->pNext = p;        p = q;        q = t;    }    pHead->pNext->pNext = NULL;    pHead->pNext = p;}/*************************************************Function      : SortListDescription   : 按a值排序Input         : Return        : *************************************************/void SortList(Node *pHead){    Node* pi = pHead->pNext;    Node* pj = pi->pNext;    Link_data temp;    memset(&temp,0,sizeof(Link_data));    if(NULL == pHead  || NULL == pHead->pNext)    {        return;    }    for(;pi != NULL;pi=pi->pNext)    {        for(pj = pi->pNext;pj != NULL;pj=pj->pNext)        {            if(pj->data.a < pi->data.a)            {                temp = pj->data;                pj->data = http://www.mamicode.com/pi->data;                pi->data =http://www.mamicode.com/ temp;            }        }    }}
#include<stdio.h>#include<string.h>#include"list.h"Node * g_LinkHead = NULL;int main(){    Node_data data1;    Node_data data2;    Node_data data4;    Node *data3 = NULL;    memset(&data1, 0, sizeof(Node_data));    memset(&data2, 0, sizeof(Node_data));    memset(&data4, 0, sizeof(Node_data));    data1.a=3;    data1.b=3;    data2.a=2;    data2.b=4;    data4.a=5;    data4.b=6;    g_LinkHead=CreateList();        Insert2ListTail(g_LinkHead,&data1);    Insert2ListTail(g_LinkHead,&data2);    Insert2ListTail(g_LinkHead,&data4);    PrintList(g_LinkHead);    //data3 = FindNodeByGlobalIndex(g_LinkHead, 2);    //printf("\r\n data3.a %d data3.b %d",data3->data.a,data3->data.b);    printf("\n\n");    //(void) ReverseList(g_LinkHead);    (void) SortList(g_LinkHead);    PrintList(g_LinkHead);    /*if(DeleteList (g_LinkHead,4))    {        PrintList(g_LinkHead);    }    PrintList(g_LinkHead);*/    /*if(RemoveList(g_LinkHead))    {        g_LinkHead = NULL;    }    PrintList(g_LinkHead);*/    return 0;}

 

带有头结点的链表的基本操作