【数据结构】4. 带头节点的单链表(完整代码实现:初始化、尾插、尾删、头插、头删、任意位置的插入、任意位置的删除、获取有效元素的大小、查找、销毁)

SHList.h

#pragma once 

typedef int DataType;

typedef struct SHListNode
{
    DataType data;
    struct SHListNode* next;
}Node;

//初始化
void SHListInit(Node** head);


//尾插
void SHListPushBack(Node* head, DataType data);


//尾删
void SHListPopBack(Node* head);


//头插
void SHListPushFront(Node* head, DataType data);


//头删
void SHListPopFront(Node* head);


//任意位置的插入
void SHListInsert(Node* pos, DataType data);


//任意位置的删除
void SHListErase(Node* pos);


//获得有效元素的大小,遍历
int SHListSize(Node* head);


//查找,注意返回的是节点
Node* SHListFind(Node* head, DataType data);


//销毁,必须传二级指针
void SHListDestroy(Node** head);


//测试
void SHListPrint(Node* head);

SHList.c

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

#include "SHList.h"

//创建新节点
Node* NewSHListNode(DataType data)
{
    Node* node = (Node*)malloc(sizeof(Node));
    if(NULL == node)
    {
        assert(0);
        return NULL;
    }

    node->next = NULL;
    node->data = data;

    return node;
}

//初始化
void SHListInit(Node** head)
{
    assert(head);
    *head = NewSHListNode(0); //给出头节点(里面的值随便给),头节点的值域不存储任何有效数据
}

//尾插
void SHListPushBack(Node* head, DataType data)
{
    //head就是链表的头指针,*head就是整个链表
    assert(head); //检测带头节点链表的合法性

    Node* cur = head; //head即链表的头节点

    while(cur->next) //找到最后一个节点
    {
        cur = cur->next; //cur->next == 0,cur就是最后一个节点
    }

    cur->next = NewSHListNode(data); //将cur的下一个节点指向新的节点。NewSHListNode返回是Node*,是一个节点
}

//尾删
void SHListPopBack(Node* head)
{
    assert(head); //检测头指针是否存在

    Node* cur = NULL;
    Node* pre = NULL; //初始化两个指针变量

    if(NULL == head->next) //第一种情况:空链表,只有头节点
        return;

    cur = head->next;
    pre = head;
    //第二种情况:非空链表:包括1个节点和多个节点
    while(cur->next) //找到最后一个节点,保留最后一个节点的前一个结点
    {
        pre = cur;
        cur = cur->next;
    }

    free(cur);
    pre->next = NULL;
}

//头插
void SHListPushFront(Node* head, DataType data)
{
    assert(head);

    Node* newNode = NewSHListNode(data);
    newNode->next = head->next;
    head->next = newNode;
}

//头删
void SHListPopFront(Node* head)
{
    assert(head);

    if(NULL == head->next) //只有头节点,则返回
        return;
    //else if(NULL == head->next->next) //只有一个有效元素节点
    //{
    //    Node* cur = head->next;
    //    free(head->next);
    //    cur = NULL;
    //}
    Node* cur = head->next;
    head->next = cur->next;
    free(cur);
}

//任意位置的插入,只能插入到pos位置的下一个位置
void SHListInsert(Node* pos, DataType data)
{
    if(NULL == pos)
        return;

    Node* newNode = NewSHListNode(data);
    newNode->next = pos->next;
    pos->next = newNode;
}

//任意位置的删除,只能删除pos位置的下一个元素
void SHListErase(Node* pos)
{
    if(NULL == pos || NULL == pos->next)
        return;

    Node* cur = pos->next;
    pos->next = cur->next;
    free(cur); //先链接,再free。如果先free,就不能链接后面的节点
}

//获得有效元素的大小,遍历
int SHListSize(Node* head)
{
    int count = 0;
    Node* cur = head->next;
    while(cur)
    {
        ++count;
        cur = cur->next;
    }
    return count;
}

//查找,注意返回的是节点
Node* SHListFind(Node* head, DataType data)
{
    assert(head);

    Node* cur = head->next;
    while(cur)
    {
        if(data == cur->data)
            return cur; //返回是节点,不是data

        cur = cur->next;
    }

    return NULL;
}

//销毁,不管有没有头节点,销毁必须传二级指针
void SHListDestroy(Node** head)
{
    Node* cur1 = (*head)->next;
    Node* cur2 = NULL;
    while(NULL != cur1) //先把头节点以后的所有节点销毁
    {
        cur2 = cur1;
        free(cur1);
        cur1 = cur2->next;
    }
    free(*head); //再销毁头节点
    *head = NULL;
    //Node* cur = NULL;
    //while(NULL != *head) 
    //{
    //    cur = *head;
    //    free(cur);
    //    *head = cur->next;
    //}
}

//测试
void SHListPrint(Node* head)
{
    Node* cur = head->next;
    while(cur)
    {
        printf("%d--->",cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}

test.c

#include <stdio.h>

#include "SHList.h"

int main()
{
    Node* head;
    SHListInit(&head);
    SHListPushBack(head,1);
    SHListPushFront(head,0);
    SHListSize(head);
    SHListPrint(head);

    SHListPopFront(head);
    SHListSize(head);
    SHListPrint(head);

    SHListPushBack(head,2);
    SHListPushBack(head,3);
    SHListPushBack(head,4);
    SHListPushBack(head,4);
    SHListPushBack(head,5);
    SHListPushBack(head,6);
    SHListPopBack(head);
    SHListSize(head);
    SHListPrint(head);

    SHListInsert(SHListFind(head,2),8);
    SHListErase(SHListFind(head,4));
    SHListSize(head);
    SHListPrint(head);

    SHListDestroy(&head);
    SHListPrint(head);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值