單鏈表的幾種基本操作

本文涉及的單鏈表的基本操作包括:初始化、插入、展示、排序、逆序和銷燬

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct list
{
    int data;
    struct list *next;
}LIST_S;


void List_Init(LIST_S** pHead);
void List_Insert(LIST_S* pHead,int data);
void List_DisPlay(LIST_S* pHead);
void List_Sort(LIST_S* pHead);
void List_Reversal(LIST_S* pHead);
void List_Destory(LIST_S* pHead);

int main(int argc,char** argv)
{
    int a[] = {9,0,2,6,3,8,1,5,4,7,1,6,4,9};
    int i = 0;
    LIST_S* pHead = NULL;

    List_Init(&pHead);

    for(i = 0;i < sizeof(a)/sizeof(int);i++)
    {
        List_Insert(pHead,a[i]);
    }
    printf("      First Show List:");
    List_DisPlay(pHead);

    List_Sort(pHead);
    printf("\r\n Sort After Show List:");
    List_DisPlay(pHead);

    List_Reversal(pHead);
    printf("\r\nRever After show List:");
    List_DisPlay(pHead);
    printf("\r\n");

    List_Destory(pHead);

    return 0;
}

void List_Init(LIST_S** pHead)
{
    LIST_S* pList = NULL;

    pList = (LIST_S*)malloc(sizeof(LIST_S));
    if (NULL == pList)
    {
        printf("Init Failed!\r\n");
        return;
    }
    memset(pList,0x00,sizeof(LIST_S));

    pList->next = NULL;
    *pHead = pList;

    return;
}

void List_Insert(LIST_S* pHead,int data)
{
    LIST_S *pList = NULL;

    if (NULL == pHead)
    {
        printf("Insert,pHead is NULL!\r\n");
        return;
    }

    pList = (LIST_S*)malloc(sizeof(LIST_S));
    if (NULL == pList)
    {
        printf("Insert Malloc Failed!\r\n");
        return;
    }
    memset(pList,0x00,sizeof(LIST_S));

    pList->data =  data;
    pList->next = pHead->next;
    pHead->next = pList;

    return;
}

void List_DisPlay(LIST_S* pHead)
{
    LIST_S* pList = NULL;

    if (NULL == pHead)
    {
        printf("DisPlay pHead is NULL!\r\n");
        return;
    }

    pList = pHead->next;
    while(pList != NULL)
    {
        printf("%3d",pList->data);
        pList = pList->next;
    }

    return;
}

void List_Sort(LIST_S* pHead)
{
    LIST_S* pMin = NULL;
    LIST_S* pList = NULL;
    LIST_S* pTmp = NULL;
    LIST_S* pTmp1 = NULL;

    if (NULL == pHead)
    {
        printf("Sort pHead is NULL!\r\n");
        return;
    }

    pList = pHead->next;
    while(NULL != pList)
    {
        pTmp = pList;
        pList = pList->next;

        if (NULL == pMin)
        {
            pMin = pTmp;
            pMin->next = NULL;
        }
        else
        {
            if (pTmp->data < pMin->data)
            {
                pTmp->next = pMin;
                pMin = pTmp;
            }
            else
            {
                pTmp1 = pMin;
                while(NULL != pTmp1->next)
                {
                    if ((pTmp1->data <= pTmp->data) && (pTmp1->next->data >= pTmp->data))
                    {
                        pTmp->next = pTmp1->next;
                        pTmp1->next = pTmp;
                        break;
                    }
                    pTmp1 = pTmp1->next;
                }
                if (NULL == pTmp1->next)
                {
                    pTmp->next = pTmp1->next;
                    pTmp1->next = pTmp;
                }
            }
        }
    }
    pHead->next = pMin;

    return;
}

void List_Reversal(LIST_S* pHead)
{
    LIST_S* pList = NULL;
    LIST_S* pTmp = NULL;
    LIST_S* pReversal = NULL;

    if (NULL == pHead)
    {
        printf("Reversal pHead is NULL!\r\n");
        return;
    }

    pList = pHead->next;
    while(NULL != pList)
    {
        pTmp = pList;
        pList = pList->next;

        if (NULL == pReversal)
        {
            pReversal = pTmp;
            pReversal->next = NULL;
        }
        else
        {
            pTmp->next = pReversal;
            pReversal = pTmp;
        }
    }
    pHead->next = pReversal;

    return;
}

void List_Destory(LIST_S* pHead)
{
    LIST_S* pList = NULL;
    LIST_S* pTmp = NULL;

    if (NULL == pHead)
    {
        return;
    }

    if (NULL == pHead->next)
    {
        free(pHead);
        pHead = NULL;
        return;
    }

    pList = pHead->next;
    while(NULL != pList)
    {
        pTmp = pList;
        pList = pList->next;
        free(pTmp);
    }
    free(pHead);
    pHead = NULL;

    return;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章