栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

单链表的增删改查

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单链表的增删改查

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include 
#include 
#include 
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("NULLn");
}
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;
    }
    
}
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);
    
    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);
    
    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;
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号