(无头结点)单链表的基本操作(C++实现)

#pragma once
#include<iostream>
using namespace std;

typedef int SLDataType;


class SList
{
public:
    class Node//定义结点
    {
    public:
        SLDataType data;//值域
        Node* next;//指向下一个结点域
    };
    Node* head;//单链表头节点指针
public:
    Node* BuySListNode(SLDataType x);//开辟新结点
    void SListInit();//初始化
    void SListPrint();//打印
    void SListPushBack(SLDataType x);//尾插
    void SListPushFront(SLDataType x);//头插
    void SListPopFront();//头删
    void SListPopBack();//尾删
    void SListInsert(Node* pos,SLDataType x);//在pos的前面插入x
    void SListErase(Node* pos);//删除pos位置的值
    Node* SListFind(SLDataType x);//查找
    void SListDestroy();//删除链表
};

#include"SList.h"

SList::Node* SList::BuySListNode(SLDataType x)//开辟新结点
{
    Node* newnode = new Node;
    newnode->data = x;
    newnode->next = nullptr;
    return newnode;
}

void SList::SListInit()//初始化
{
    head = nullptr;
}

void SList::SListPrint()//打印
{
    Node* cur = head;
    while (cur != nullptr)
    {
        cout << cur->data << "->";
        cur = cur->next;
    }
    cout << "nullptr" << endl;
}

void SList::SListPushBack(SLDataType x)//尾插
{
    Node* newnode = BuySListNode(x);

    if (head == nullptr)
    {
        head = newnode;
    }
    else
    {

        //找到尾结点的指针
        Node* tail = head;
        while (tail->next != nullptr)
        {
            tail = tail->next;
        }
        //尾结点,链接新结点
        tail->next = newnode;
    }
}


void SList::SListPushFront(SLDataType x)//头插
{
    Node* newnode = BuySListNode(x);
    newnode->next = head;
    head = newnode;
}

void SList::SListPopFront()//头删
{
    if (head == nullptr)
    {
        return;
    }
    Node* next = head->next;
    delete head;
    head = next;
}

void SList::SListPopBack()//尾删
{
    if (head == nullptr)
    {
        return;
    }
    else if (head->next == nullptr)
    {
        delete head;
        head = nullptr;
    }
    else
    {
        Node* tail = head;
        Node* prev = nullptr;
        while (tail->next != nullptr)
        {
            prev = tail;
            tail = tail->next;
        }
        delete tail;
        prev->next = nullptr;
    }
}

SList::Node* SList::SListFind(SLDataType x)
{
    Node* cur = head;
    while (cur)
    {
        if (cur->data == x)
        {
            return cur;
        }
        cur = cur->next;
    }
    return nullptr;
}

void SList::SListInsert(Node* pos,SLDataType x)//在pos的前面插入x
{
    if (pos == head)
    {
        SListPushFront(x);
    }
    else
    {
        Node* newnode = BuySListNode(x);

        Node* prev = head;

        while (prev->next != pos)
        {
            prev = prev->next;
        }

        prev->next = newnode;
        newnode->next = pos;
    }
}

void SList::SListErase(Node* pos)//删除pos位置的值
{
    if (pos == head)
    {
        SListPopFront();
    }
    else
    {
        Node* prev = head;
        while (prev->next != pos)
        {
            prev = prev->next;
        }
        prev->next = pos->next;
        delete pos;
    }
}

void SList::SListDestroy()
{
    Node* cur = head;
    while (cur != nullptr)
    {
        Node* next = cur->next;
        delete cur;
        cur = next;
    }
    head = nullptr;
}

#include"SList.h"

void test01()
{
    SList st;
    st.SListInit();
    st.SListPushBack(1);
    st.SListPushBack(2);
    st.SListPushBack(3);
    st.SListPushBack(4);
    st.SListPrint();
    st.SListDestroy();
    st.SListPrint();
}

void test02()
{
    SList st;
    st.SListInit();
    st.SListPushFront(1);
    st.SListPushFront(2);
    st.SListPushFront(3);
    st.SListPushFront(4);
    st.SListPrint();
    SList::Node* pos = st.SListFind(1);

    if (pos)
    {
        st.SListErase(pos);
    }
    st.SListPrint();

    pos = st.SListFind(2);

    if (pos)
    {
        st.SListErase(pos);
    }
    st.SListPrint();

    pos = st.SListFind(3);

    if (pos)
    {
        st.SListErase(pos);
    }
    st.SListPrint();

    pos = st.SListFind(4);

    if (pos)
    {
        st.SListErase(pos);
    }
    st.SListPrint();
}

int main()
{
    test01();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值