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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有头结点的链表的基本操作相关的知识,希望对你有一定的参考价值。

#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      : CreateList
Description   : 创建链表头节点
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      : FindNodeByGlobalIndex
Description   : 根据指定参数,查找某个节点
Input         : pHead 链表的头节点指针
                要查找的学生ID
Return        : 正确:返回指定节点的指针
                失败:返回空指针
*************************************************/
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      : Insert2ListTail
Description   : 向链表中尾部插入某个节点
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      : RemoveList
Description   : 删除整个链表
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      : PrintList
Description   : 打印整个链表
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      : DeleteList
Description   : 删除链表的一个结点,删除条件该节点的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      : ReverseList
Description   : 链表反转
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      : SortList
Description   : 按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 = pi->data;
                pi->data = 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;
}

 

以上是关于带有头结点的链表的基本操作的主要内容,如果未能解决你的问题,请参考以下文章

5种常见的链表基本操作

剑指offer十六之合并两个排序的链表

剑指offer-合并两个排序的链表

数据结构-链表

链表反转

表-单链表(list/singly-linked-list)