C++的链表实现

初学数据结构与算法,半参考半自写的写出了C++的链表实现。

节点结构体代码:

template<class ElemType>
struct Node {
    ElemType data;
    Node< ElemType>* next;
    Node() {
        next = NULL;
    }
    Node(const ElemType& e, Node< ElemType>* link = NULL) {
        data = e;
        next = link;
    }
};

链表:

#include"node.h"
template<class ElemType>
class SimpleLinkList {
protected:
    Node< ElemType>* head;
    Node< ElemType>* GetElemPtr(int position)const;
public:
    SimpleLinkList();
    virtual ~SimpleLinkList();
    int Length()const;
    bool Empty()const;
    void Clear();
    void Traverse(void(*visit)(const ElemType&))const;
    bool GetElem(int position, ElemType& e)const;
    bool SetElem(int position, ElemType& e);
    bool Delete(int position);
    bool Delete(int position, const ElemType& e);
    bool Insert(int position, const ElemType& e);
    SimpleLinkList(const SimpleLinkList<ElemType>& source);
    SimpleLinkList<ElemType>& operator=(const SimpleLinkList<ElemType>& source);
};
template<class ElemType>
void SimpleLinkList<ElemType>::Clear() {
    while (!Empty()) {
        Delete(1);
    }
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Empty()const {
    return head->next == NULL;
}
template<class ElemType>
int SimpleLinkList<ElemType>::Length()const {
    int count = 0;
    for (Node<ElemType>* temptr = head->next; temptr != NULL; temptr = temptr->next) {
        count++;
    }
    return count;
}
template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList() {
    head = new Node<ElemType>;
}
template<class ElemType>
SimpleLinkList<ElemType>::~SimpleLinkList()
{
    Clear();
    delete head;
}
template<class ElemType>
void SimpleLinkList<ElemType>::Traverse(void(*visit)(const ElemType&))const {
    for (Node<ElemType>* temptr = head->next; temptr != NULL; temptr = temptr->next) {
        (*visit)(temptr->data);
    }
}
template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position)const {

    Node<ElemType>*temptr = head;
    int tempos = 0;
    while (temptr != NULL && tempos < position) {
        temptr = temptr->next;
        tempos++;
    }
    if (temptr != NULL && tempos == position) {
        return temptr;
    }
    else {
        return NULL;
    }
}
template<class ElemType>
bool SimpleLinkList<ElemType>::GetElem(int position, ElemType& e)const {
    if (position<1 || position>Length()) {
        return false;
    }
    else {
        Node<ElemType>* temptr = GetElemPtr(position);
        e = temptr->data;
        return true;
    }
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Insert(int position, const ElemType& e) {
    if (position<1 || position>Length() + 1) {
        return false;
    }
    else {
        Node<ElemType>* temptr = GetElemPtr(position - 1);
        Node<ElemType>* newptr = new Node<ElemType>(e, temptr->next);
        temptr->next = newptr;
        return true;
    }
}
template<class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList(const SimpleLinkList<ElemType>& source)
{
    head = source.head;
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position, const ElemType& e) {
    if (position<1 || position>Length()) {
        return false;
    }
    else {
        Node<ElemType>* temptr = GetElemPtr(position - 1);
        Node<ElemType>* nextptr = temptr->next;
        e = nextptr->data;
        temptr->next = nextptr->next;
        delete nextptr;
        return true;
    }
}
template<class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position) {
    if (position<1 || position>Length()) {
        return false;
    }
    else {
        Node<ElemType>* temptr = GetElemPtr(position - 1);
        Node<ElemType>* nextptr = temptr->next;
        temptr->next = nextptr->next;
        delete nextptr;
        return true;
    }
}
template<class ElemType>
SimpleLinkList<ElemType>& SimpleLinkList<ElemType>::operator=(const SimpleLinkList<ElemType>& source) {
    if (source->head == NULL) {
        head = NULL;
    }
    else {
        SimpleLinkList<ElemType>* nextptr(source->head);
        while (nextptr != NULL) {
            head->next = nextptr;
            nextptr = nextptr->next;
        }
    }
}
template<class ElemType>
bool SimpleLinkList<ElemType>::SetElem(int position, ElemType& e) {
    if (position<1 || position>Length()) {
        return false;
    }
    else {
        Node<ElemType>* temptr = GetElemPtr(position);
        temptr->data = e;
        return true;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值