继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

C++链表模板化设计【Van0512】

慕姐3585284
关注TA
已关注
手记 10
粉丝 22
获赞 199

为了节约篇幅,对其他意义不大,并且容易通过现有List类函数得到实现的函数不做定义。如前驱、后续、判空、判满、在头部插入、在尾部插入等函数。
函数不做类外定义,也不分文件定义,写得也比较紧凑,同样都是为了节约篇幅。o(╯╰)o

#include <iostream>
using namespace std;

typedef int ElementType;
typedef struct node INode;
struct node {
    ElementType data;
    INode *next;
};
/*
1. 设计通用的结构体类型INode,实现List的模板化。
做些细微的改动,List中的ElementType都可以设计成Node类型,
从而把参数列表中的ElementType &elem改成Node *elem或Node &elem。
2. 也可以把类的模板设计成template <typename ElementType> ...
然后把结构体封装在类的内部,其他实现细节都差不多。
*/ 
template <typename Node>
class List {
public:
    List() {    //创建一个头结点。 
        pList = new Node;
        pList->next = NULL;
        len = 0;
    }
    ~List() {
        clearList();
        delete pList;
    }
    void clearList() {
        Node *currNode = pList->next;
        while (currNode != NULL) {
            Node *tmp = currNode->next;
            delete currNode;
            currNode = tmp;
        }
        len = 0;
    }
    int listLen() const { return len; }
    bool getElem(int i, ElementType &elem) {
        if (i < 0  i >= len) return false;
        Node *iNode = iNodePointer(i)->next;
        elem = iNode->data;
        return true;
    }
    /*
    返回第index-1个位置上node的指针,位置编号从0开始。
    不可乱用,否则可能会抛出异常。
    */
    Node* iNodePointer(int index) {
        Node *iNode = pList;
        for (int i = 0; i < index; i++)
            iNode = iNode->next;
        return iNode;
    }
    int locateElem(const ElementType &elem) {
        Node *tmp = pList;
        for (int i = 0; i < len; i++) {
            tmp = tmp->next;
            if (tmp->data == elem) return i;
        }
        return -1;
    }
    //在第i个位置插入elem,i从0开始,i=len表示插在链表尾部。 
    bool insertList(int i, const ElementType &elem) {
        if (i < 0  i > len) return false;
        Node *newNode = new Node;
        newNode->data = elem;
        Node *iNode = iNodePointer(i);
        newNode->next = iNode->next;
        iNode->next = newNode;
        len++;
        return true;
    }
    bool deleteList(int i, ElementType &elem) {
        if (i < 0  i >= len) return false;
        Node *iNode = iNodePointer(i);
        Node *deNode = iNode->next;
        elem = deNode->data;
        iNode->next = deNode->next;
        delete deNode;
        len--;
        return true;
    }
    void traverseList() {
        Node *tmp = pList->next;
        while (tmp != NULL) {
            cout << tmp->data << endl;
            tmp = tmp->next;
        }
    }
private:
    Node *pList;
    int len;
};

int main(void) {
    List<INode> *ls = new List<INode>;
    ls->insertList(ls->listLen(), 1);
    ls->insertList(ls->listLen(), 2);
    ls->insertList(ls->listLen(), 3);
    ls->insertList(1, 4);
    ls->traverseList();
    ElementType tmp;
    ls->getElem(1, tmp);
    cout << "tmp = " << tmp << endl;
    ls->deleteList(2, tmp);
    cout << "tmp = " << tmp << endl;
    ls->traverseList();

    delete ls;
    return 0;
}
打开App,阅读手记
11人推荐
发表评论
随时随地看视频慕课网APP