单链表

来源:互联网 发布:节目点播安庆网络广播 编辑:程序博客网 时间:2024/06/05 09:28

双向链表enter link description here
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
具体实现如下

#pragma once#include<iostream>#include<assert.h>using namespace std;typedef int DataType;//链表中的数据struct SListNode{    SListNode* _next;    DataType _data;    SListNode(DataType x)        :_data(x)        , _next(NULL)    {}};class SList{    typedef SListNode Node;public:    //构造    SList()        :_head(NULL)        ,_tail(NULL)    {        ;    }    SList(const SList& s)    {        /*Node* s_tmp = s._head;        if (_head == NULL)        {            _head = _tail = new Node(s_tmp->_data);            s_tmp = s_tmp->_next;        }        while (s_tmp)        {            _tail->_next = new Node(s_tmp->_data);            _tail = _tail->_next;        }*/        Copy(s);    }    //传统法    /*SList& operator=(const SList& s)    {        Node* s_tmp = s._head;        if (_head == NULL)        {            _head = _tail = new Node(s_tmp->_data);            s_tmp = s_tmp->_next;        }        while (s_tmp)        {            _tail->_next = new Node(s_tmp->_data);            _tail = _tail->_next;        }        return *this;    }*/    //现代法    SList& operator=(SList s)    {        swap(_head, s._head);        swap(_tail, s._tail);        return *this;    }    //析构    ~SList()    {        Node* cur = _head;        if (cur)        {            Node* next = cur->_next;            delete cur;            cur = next;        }        _head = _tail = NULL;    }    //尾插    void PushBack(DataType x)    {        if (_head == NULL)        {            _head = _tail = new Node(x);        }        else        {            _tail->_next = new Node(x);            _tail = _tail->_next;        }    }    //尾删    void PopBack()    {        if (_head == NULL)        {            cout << "没有数据可以删除" << endl;        }        else if (_head->_next == NULL)        {            delete _head;            _head = _tail = NULL;        }        else        {            Node* tmp = _head;            while (tmp->_next != _tail)            {                tmp = tmp->_next;            }            delete _tail;            _tail = tmp;            _tail->_next = NULL;        }    }    //头插    void PushFront(DataType x)    {        if (_head = NULL)        {            _head = _tail = new Node(x);        }        else        {            Node* prev = new Node(x);            prev->_next = _head;            _head = prev;        }    }    //头删-------删除数据记得要delete,不然导致内存泄漏    void PopFront()    {        if (_head == NULL)        {            cout << "没有数据可以删除" << endl;        }        else if(_head->_next == NULL)        {            delete _head;            _head = _tail = NULL;        }        Node* cur = _head;        _head = _head->_next;        delete cur;        cur = NULL;    }    // 插入一个节点在pos的前面     void Insert(Node* pos, DataType x)    {        assert(pos);        if (_head == pos)        {            PushFront(x);        }        else        {            Node* head = _head;            while (head->_next != pos)            {                head = head->_next;            }            Node* tmp = new Node(x);            head->_next = tmp;            tmp->_next = pos;        }    }    //删除pos位置的数据    void Erase(Node* pos)    {        if (_head == pos)        {            PopFront();        }        else if (_tail == pos)        {            PopBack();        }        else        {            Node* head = _head;            while (head->_next != pos)            {                head = head->_next;            }            head->_next = pos->_next;            delete pos;        }    }    //查找    Node* Find(DataType x)    {        Node* cur = _head;        while (cur)        {            if (cur->_data == x)            {                return cur;            }                cur = cur->_next;        }        return NULL;    }    //拷贝  拷贝构造就可以复用这个函数    void Copy(const SList& s)    {        Node* cur = s._head;        while (cur)        {            PushBack(cur->_data);            cur = cur->_next;        }    }    //    void Print()    {        Node* cur = _head;        while (cur)        {            cout << cur->_data << " ";            cur = cur->_next;        }        cout << endl;    }private:    Node* _head;//头结点    Node* _tail;//尾结点};
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 换水了龙鱼顶缸怎么办 压着眼睛睡觉醒来模糊怎么办 天热眼睛有眼屎怎么办 眼睛里膜起来了怎么办 眼睛一边大一边小怎么办 眼白膜鼓起来了怎么办 主持问答环节没人提问怎么办 转学原学校不给怎么办 村长借东西不还怎么办 村长不上报建房申请怎么办 村长不上报建房手续申请怎么办 村长不给村民盖章怎么办? 找村干部办事难怎么办 洪洞县村长不给我盖章怎么办 睾丸穿刺取精只配到6个胚胎怎么办 孩子一只耳朵听不到声音怎么办 孕早期孕囊生长慢怎么办 试管2次不着床怎么办? pescm球员年龄大了怎么办 实况足球俱乐部经理球员老了怎么办 你不是我的菜怎么办 苹果平板电脑耳机有回音怎么办 obs直播有电流音怎么办 语音里网吧很吵怎么办 电脑k歌有延迟怎么办 想开个跆拳道馆怎么办营业执照? 壶嘴拐弯处漏水怎么办 裂纹茶壶嘴坏了怎么办 小孩刚上学怕她上火怎么办 在幼儿园小朋友不愿叠衣服怎么办 孩子在家听话幼儿园不听话怎么办 变魔术时观众说看过这个怎么办 孕妇8个月摔跤了怎么办 孩子吃了残奶怎么办 小米手机变卡了怎么办 主持时说错话了怎么办 小鲜肉老了不红怎么办 同学聚会大家玩手机你怎么办 率土之滨被掠夺怎么办 戒指戴手上取不下来怎么办 择离开我我该怎么办