【线性表(一)】:链表之单链表

一、定义节点

//链表节点
template <class T>
class ListNode
{
public:
    ListNode():value_(NULL),front(nullptr),next(nullptr){}
    ListNode(const T &value):value_(value),front(nullptr),next(nullptr){}
    ListNode(const T &value, ListNode<T> *left, ListNode<T> *right): value_(value),
                                                                     front(left),
                                                                     next(right){}
    ListNode(const ListNode<T> &theNode): value_(theNode.value_),
                                          front(nullptr),
                                          next(nullptr){}
public:
    T value_;
    ListNode<T> *front;
    ListNode<T> *next;
};

二、单链表实现

/*******************************************
*                 单向链表                   *
/*******************************************/
template <class T>
class SingleList
{
public:
    SingleList():head_(nullptr),size_(0){}
    ~SingleList();

    //链表大小
    size_t size(){return size_;}
    //链表是否为空
    bool empty(){return size_==0;}
    //获取头结点
    ListNode<T> *get_head(){return head_;}

    //插入节点
    void insert(const int &index,const T &value);
    //删除节点
    T erase(const int &index);
    //修改节点
    void set(const int &index,const T &value);
    //查询节点
    T &get(const int &index);
    //获取链表值对应下标
    int get_index(const T &value);

    //打印链表
    void print();

    //单向链表反转
    void reverse();
    //单向链表排序
    void sort();
    //获取链表的中间节点
    ListNode<T> *get_mid_node();
private:
    void quick_sort(ListNode<T> *head,ListNode<T> *end);//快速排序
    void swap(ListNode<T> *p,ListNode<T> *q);//交换两个节点中的值
private:
    ListNode<T> *head_;
    size_t size_;
};

template <class T>
SingleList<T>::~SingleList()
{
    while(head_!= nullptr){
        ListNode<T> *node=head_;
        head_=head_->next;
        delete node;
        node= nullptr;
    }
}

template <class T>
void SingleList<T>::insert(const int &index, const T &value)
{
    if(index<0 || index>size_){
        cout<<"index out of range,index < 0 or index > size_"<<endl;
        exit(0);
    }

    ListNode<T> *node=new ListNode<T>(value);

    if(size_==0){
        head_=node;
    } else{
        if(index==0){
            node->next=head_;
            head_=node;
        } else{
            ListNode<T> *cur=head_;
            int i=0;
            while(i<index-1){
                cur=cur->next;
                ++i;
            }
            node->next=cur->next;
            cur->next=node;
        }
    }

    ++size_;
}

template <class T>
T SingleList<T>::erase(const int &index)
{
    if(index<0 || index >= size_){
        cout<<"index out of range,index < 0 or index > size_"<<endl;
        exit(0);
    }

    ListNode<T> *cur=head_;
    T value;
    if(index==0){
        head_=head_->next;
        value=cur->value_;
        delete cur;
        cur= nullptr;
    } else{
        int i=0;
        while(i<index-1){
            cur=cur->next;
            ++i;
        }
        ListNode<T> *temp=cur->next;
        cur->next=temp->next;
        value=temp->value_;
        delete temp;
        temp= nullptr;
    }

    --size_;
    return value;
}

template<class T>
void SingleList<T>::set(const int &index, const T &value)
{
    if(index<0 || index >= size_){
        cout<<"index out of range,index < 0 or index > size_"<<endl;
        exit(0);
    }

    if(index==0){
        head_->value_=value;
    } else{
        ListNode<T> *cur=head_;
        int i=0;
        while(i<index){
            cur=cur->next;
            ++i;
        }
        cur->value_=value;
    }
}

template <class T>
T &SingleList<T>::get(const int &index)
{
    if(index<0 || index >= size_){
        cout<<"index out of range,index < 0 or index > size_"<<endl;
        exit(0);
    }

    T value;
    if(index==0){
        return head_->value_;
    } else{
        ListNode<T> *cur=head_;
        int i=0;
        while(i<index){
            cur=cur->next;
            ++i;
        }
        return cur->value_;
    }
}

template <class T>
int SingleList<T>::get_index(const T &value)
{
    for(int i=0;i<size_;++i){
        if(get(i)==value){
            return i;
        }
    }

    return -1;
}

template <class T>
void SingleList<T>::print()
{
    ListNode<T> *cur=head_;
    while(cur!= nullptr){
        cout<<cur->value_<<" ";
        cur=cur->next;
    }
    cout<<endl;
}

template <class T>
void SingleList<T>::reverse()
{
    if(size_==1){
        return ;
    }

    ListNode<T> *front=head_;
    ListNode<T> *cur=head_->next;
    ListNode<T> *next=cur;
    front->next= nullptr;
    while(next!= nullptr){
        next=cur->next;
        cur->next=front;
        front=cur;
        cur=next;
    }

    head_=front;
}

template <class T>
void SingleList<T>::swap(ListNode<T> *p,ListNode<T> *q)
{
    T temp=p->value_;
    p->value_=q->value_;
    q->value_=temp;
}

template <class T>
void SingleList<T>::quick_sort(ListNode<T> *head, ListNode<T> *end)
{
    if(head==end || head->next==end){
        return ;
    }

    T value=head->value_;
    ListNode<T> *p=head;
    ListNode<T> *q=head->next;
    while( q != end){
        if(value <= q->value_){
            q=q->next;
        }else {
            p=p->next;
            swap(p,q);
            q=q->next;
        }
    }
    swap(head,p);
    quick_sort(head,p);
    quick_sort(p->next,q);
}

template <class T>
void SingleList<T>::sort()
{
    if(head_== nullptr){
        return ;
    }

    ListNode<T> *head=head_;
    ListNode<T> *end= nullptr;
    quick_sort(head,end);
}

template <class T>
ListNode<T> *SingleList<T>::get_mid_node()
{
    ListNode<T> *fast=head_;
    ListNode<T> *slow=head_;

    while(fast&&fast->next){
        fast=fast->next->next;
        slow=slow->next;
    }
    return slow;
}

参考链接:

单链表的讲解:单链表的原理,添加、删除元素

单链表——基本操作

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值