单链表相关操作

本文详细介绍了单链表的基本操作实现,包括初始化、销毁、在链表尾部和头部添加节点、删除尾部和头部的节点、在指定位置前插入节点、按位置删除节点等。此外还提供了查找特定数据节点的功能。
#include "test.h"

SListNode* BuyNode(DataType x)
{
    SListNode* node = (SListNode*)malloc(sizeof(SListNode));
    node->data = x;
    node->next = NULL;
    return node;
}

void PrintSList(SListNode*& pHead)
{
    assert(pHead);
    SListNode* cur = pHead;
    while (cur)
    {
        printf("%d ",cur->data);
        cur = cur->next;
    }
}

void InitSList(SListNode*& pHead)
{
    pHead = NULL;
}

void DistorySList(SListNode*& pHead)
{
    assert(pHead);
    while (pHead)
    {
        SListNode* cur = pHead;
        pHead = pHead->next;
        free(cur);
    }
    pHead = NULL;
}

void PushBack(SListNode*& pHead, DataType x)
{
    if (pHead == NULL)
    {
        pHead = BuyNode(x);
    }
    else 
    {
        SListNode* cur = pHead;
        while (cur->next)
        {
            cur = cur->next;
        }
        cur->next = BuyNode(x);
    }
}

void PopBack(SListNode*& pHead)
{
    //1.空
    //2.一个节点
    //3.多个节点

    if (pHead == NULL)
    {
        printf("List is empty!\n");
        return;
    }
    else if (pHead->next == NULL)
    {
        free(pHead);
        pHead = NULL;
    }
    else
    {
        SListNode* cur = pHead;
        SListNode* prev = NULL;
        while (cur->next!=NULL)
        {
            prev = cur;
            cur = cur->next;
        }
        prev = NULL;
        free(cur);
    }
}

void PushFront(SListNode*& pHead, DataType x)
{
    if (pHead == NULL)
    {
        pHead = BuyNode(x);
    }
    else
    {
        SListNode* newHead = NULL;
        newHead = BuyNode(x);
        newHead ->next = pHead;
        pHead = newHead;
    }
}

void PopFront(SListNode*& pHead)
{
    if (pHead == NULL)
    {
        printf("List is empty!\n");
        return;
    }
    else if (pHead->next == NULL)
    {
        free(pHead);
        pHead = NULL;
    }
    else
    {
        SListNode* cur = pHead;
        pHead = pHead->next;
        free(cur);
    }
}

//在pos之前插入x
void Insert(SListNode*& pHead,SListNode*& pos, DataType x)
{
    SListNode* cur = pHead;
    SListNode* prev=NULL;

    if (pHead->next == NULL)
    {
        PushFront(pHead, x);
    }
    else
    {
        while (cur != pos)
        {
            prev = cur;
            cur = cur->next;
        }
        SListNode* newNode = BuyNode(x);
        newNode->next = cur;
        prev->next = newNode;
    }
}

//按位置删除节点
void Erase(SListNode*& pHead, SListNode* pos)
{
    assert(pos && pHead);
    if (pos == pHead)
    {
        PopFront(pHead);
    }
    else 
    {
        SListNode* cur = pHead;
        SListNode* prev = NULL;
        while (cur != pos)
        {
            prev = cur;
            cur = cur->next;
        }
        prev->next = cur->next;
        free(cur);
    }
}

void Remove(SListNode*& pHead, DataType x)
{
    SListNode* del=Find(pHead,x);
    Erase(pHead, del);
}

SListNode* Find(SListNode*& pHead, DataType x)
{
    SListNode* cur = pHead;
    while (cur)
    {
        if (cur->data == x)
        {
            return cur;
            break;
        }
        cur = cur->next;
    }
    if (cur == NULL)
    {
        return NULL;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值