C语言实现链表(增、删、查、改、排序)

链表是一种常用的数据结构,但是在c语言中没有这样的数据结构,只能自己实现,下面是用C语言实现了基本的链表操作
typedef struct STU
{
    int id;
    int age;
    char name[20];
    struct STU* next;
}STU;

STU* CreatNewNode(int id, int age, char name[])
{
    STU* newNode = (STU*)malloc(sizeof(STU));
    newNode->age = age;
    newNode->id = id;
    memcpy(newNode->name, name, 20);
    newNode->next = nullptr;
    return newNode;
}

//尾插
void InsertEndNode(STU** pHead, int id, int age, char name[])
{
    STU* newNode = CreatNewNode(id, age, name);
    if (*pHead == nullptr)
    {
        *pHead = newNode;
    }
    else
    {
        STU* pTemp = *pHead;
        //找尾
        while (pTemp->next != nullptr)
        {
            pTemp = pTemp->next;
        }
        pTemp->next = newNode;
    }
}

//头插
void InsertHeadNode(STU** pHead, int id, int age, char name[])
{
    STU* pNewNode = CreatNewNode(id, age, name);
    pNewNode->next = *pHead;
    *pHead = pNewNode;
}

//中间位置插
void InsertPosNode(STU** pHead, int pos, int id, int age, char name[])
{
    STU* pNewNode = CreatNewNode(id, age, name);
    pNewNode->next = nullptr;
    STU* pTemp = *pHead;
    int i = 1;
    while (pTemp != nullptr && pos > i)
    {
        pTemp = pTemp->next;
        i++;
    }

    if (pTemp == nullptr)
    {
        pTemp = pNewNode;
    }
    else
    {
        pNewNode->next = pTemp->next;
        pTemp->next = pNewNode;
    }
}

//查找
STU* FindNode(STU* pHead, int id)
{
    STU* pTemp = pHead;
    while (pTemp != nullptr)
    {
        if (pTemp->id == id)
        {
            return pTemp;
        }
        pTemp = pTemp->next;
    }

    return nullptr;
}

//删除
void DeleteNode(STU** pHead, int id)
{
    STU* pTemp = *pHead;
    STU* pPre = nullptr;
    while (pTemp != nullptr)
    {
        if (pTemp->id == id)
        {
            if (pPre == nullptr)
            {
                *pHead = pTemp->next;
            }
            else {
                pPre->next = pTemp->next;
            }
            free(pTemp);
            return;
        }
        pPre = pTemp;
        pTemp = pTemp->next;
    }
}

//修改
void ChangedNode(STU* pHead, int id, int age, char name[])
{
    STU* pTemp = pHead;
    while (pTemp != nullptr)
    {
        if (pTemp->id == id)
        {
            pTemp->age = age;
            memcpy(pTemp->name, name, 20);
            break;
        }
        pTemp = pTemp->next;
    }
}

//排序
void SortList(STU* pHead)
{
    //冒泡排序,
    STU* pTemp = pHead;
    while (pTemp != nullptr)
    {
        STU* next = pTemp->next;
        while (next != nullptr)
        {
            if (pTemp->age > next->age)
            {
                int age = pTemp->age;
                pTemp->age = next->age;
                next->age = age;
            }
            next = next->next;
        }
        pTemp = pTemp->next;
    }
}

//获取链表元素个数
int GetListSize(STU* pHead)
{
    STU* pTemp = pHead;
    int num = 0;
    while (pTemp != nullptr)
    {
        num++;
        pTemp = pTemp->next;
    }

    return num;
}

//打印
void PrintNode(STU* pHead)
{
    STU* pTemp = pHead;
    while (pTemp != nullptr)
    {
       printf("id = %d, age = %d, 名称 = %s\n", pTemp->id, pTemp->id, pTemp->name);
        pTemp = pTemp->next;
    }
}

int main()
{
	 STU* pHead = NULL;
    char name[20];
    memcpy(name, "理化", 20);
    for (int i = 0; i < 20; i++)
    {
        InsertEndNode(&pHead, i, rand() % 100, name);
    }

    PrintNode(pHead);

    DeleteNode(&pHead, 5);

    PrintNode(pHead);

    memcpy(name, "张三", 20);
    ChangedNode(pHead, 12, 100, name);
    PrintNode(pHead);

    SortList(pHead);
    PrintNode(pHead);

    memcpy(name, "李逵", 20);
    InsertHeadNode(&pHead, 100, 43, name);
    PrintNode(pHead);

    memcpy(name, "林冲", 20);
    InsertPosNode(&pHead, 17, 100, 43, name);
    PrintNode(pHead);
	return 0;
}

你可能感兴趣的:(C语言,链表,c语言,数据结构)