单链表的增删改查

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
    SLTDataType data;
    struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);                //单链表打印
void SListPushBack(SLTNode** pplist, SLTDataType x);            //单链表尾插
void SListPushFront(SLTNode** pplist, SLTDataType x);             //单链表头插
void SListPopBack(SLTNode** pplist);                         //单链表尾删
void SListPopFront(SLTNode** pplist);                     //单链表头删
void SListInsertAfter(SLTNode* pos, SLTDataType x);   //单链表在pos位置之后插入x
void SListEraseAfter(SLTNode* pos);                  //单链表删除pos位置之后的值


void SListPrint(SLTNode* phead)                         //单链表打印
{
    SLTNode* cur = phead;
    while (cur)
    {
        printf("%d->", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}
SLTNode* BuySLTNode(SLTDataType x)             //创建新结点
{
    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}
void SListPushBack(SLTNode** pplist, SLTDataType x)            //单链表尾插
{
    SLTNode* newnode = BuySLTNode(x);
    if (*pplist == NULL)
    {
        *pplist = newnode;
    }
    else
    {
        SLTNode* tail = *pplist;
        while (tail->next)
        {
            tail = tail->next;
        }
        tail->next = newnode;
    }
}
void SListPushFront(SLTNode** pplist, SLTDataType x)              //单链表头插
{
    SLTNode* newnode = BuySLTNode(x);
    newnode->next = *pplist;
    *pplist = newnode;
}
void SListPopBack(SLTNode** pplist)                           //单链表尾删
{
    assert(*pplist);
    SLTNode* tail = *pplist;
    SLTNode* start = *pplist;
    while (tail->next)
    {
        start = tail;
        tail = tail->next;
    }
    if ((*pplist)->next == NULL)
    {
        *pplist = NULL;
        free(*pplist);
    }
    else
    {
        free(tail);
        start->next = NULL;
    }
    /*if ((*pplist)->next == NULL)
    {
        *pplist = NULL;
        free(*pplist);
    }
    else
    {
        SLTNode* tail = *pplist;
        while (tail->next->next)
        {
            tail = tail->next;
        }
        free(tail->next);
        tail->next = NULL;
    }*/
}
void SListPopFront(SLTNode** pplist)                       //单链表头删
{
    assert(*pplist);
    SLTNode* tail = (*pplist)->next;
    free(*pplist);
    *pplist = tail;
}
SLTNode* SListFind(SLTNode* pplist, SLTDataType x)
{
    SLTNode* tail = pplist;
    while (tail)
    {
        if (tail->data != x)
        {
            tail = tail->next;
        }
        else
        {
            return tail;
        }
    }
    return NULL;
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)    //单链表在pos位置之后插入x
{
    assert(pos);
    SLTNode* newnode = BuySLTNode(x);
    SLTNode* next = pos->next;
    pos->next = newnode;
    newnode->next = next;
}
void SListEraseAfter(SLTNode* pos)                    //单链表删除pos位置之后的值
{
    assert(pos);
    /*if (pos->next == NULL)
        return;*/
    SLTNode* tail = pos->next->next;
    pos->next = tail;
    free(pos->next);
}


void TestSList1()
{
    SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n1);
    SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n2);
    SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n3);
    SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
    assert(n4);
    n1->data = 1;
    n2->data = 2;
    n3->data = 3;
    n4->data = 4;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = NULL;
    SLTNode* Plist = n1;
    SListPrint(Plist);
}
void TestSList2()
{
    SLTNode* plist = NULL;
    SListPushBack(&plist, 5); 
    SListPrint(plist);
    SListPushBack(&plist, 6);
    SListPrint(plist);
    SListPushBack(&plist, 7);
    SListPrint(plist);
    SListPushBack(&plist, 8);
    SListPrint(plist);                            //单链表尾插
}
void TestSList3()
{
    SLTNode* plist = NULL;
    SListPushFront(&plist, 6);                       //单链表头插
    SListPushFront(&plist, 7); 
    SListPushFront(&plist, 8); 
    SListPushFront(&plist, 9); 
    SListPrint(plist);
}
void TestSList4()          
{
    SLTNode* plist = NULL;
    SListPushFront(&plist, 7);
    SListPushFront(&plist, 8);                        //单链表尾删
    SListPushFront(&plist, 9);
    SListPopBack(&plist);
    SListPopBack(&plist);
    SListPopBack(&plist);
    /*SListPopBack(&plist);*/
    SListPrint(plist);
}
void TestSList5()
{
    SLTNode* plist = NULL;
    SListPushFront(&plist, 7);
    SListPushFront(&plist, 8);                       
    SListPushFront(&plist, 9);
    SListPopFront(&plist); 
    SListPopFront(&plist);
    SListPopFront(&plist);  //单链表头删
    SListPrint(plist);
}
void TestSList6()
{
    SLTNode* plist = NULL;
    SListPushFront(&plist, 4);
    SListPushFront(&plist, 5);
    SListPushFront(&plist, 6);
    SListPushFront(&plist, 7);
    SListPushFront(&plist, 8);
    SListPushFront(&plist, 9);
    SLTNode* pos = SListFind(plist,6);
    SListInsertAfter(pos, 10);                         //单链表在pos位置之后插入x
    SListPrint(plist);
    SListEraseAfter(pos);                             //删除pos位置之后的值
    SListPrint(plist);
}
int main()
{
    TestSList4();
    return 0;
}

🙇‍感谢大家的阅读,如有错误请指出,我们下次再见。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华的梦境

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值