• 单链表


    单链表为链表的一种,链表和顺序表又同属于线性表,在正式写单链表之前,可以先定义一个线性表抽象类,用来规范所有线性表共同的操作

    #include <iostream>
    using namespace std;
    
    // 线性表:顺序表和链表共同的抽象类
    template<typename elemType>
    class list{
    public:
        virtual void clear() = 0;                            // 删除线性表中的所有数据元素
        virtual int length() const = 0;                      // 求线性表的长度
        virtual void insert(int i, const elemType & x) = 0;  // 在第i个位置插入一个元素
        virtual void remove(int i) = 0;                      // 删除第i个位置的元素
        virtual int search(const elemType & x) const = 0;    // 搜索某个元素x在线性表中是否出现
        virtual elemType visit(int i) const = 0;             // 返回线性表中第i个数据元素的值
        virtual void traverse() const = 0;                   // 按序访问线性表的每一数据元素
        virtual ~list(){};
    };

     定义单链表,链表中用到的节点被定义为类内的结构体。

    // 单链表
    template<typename elemType>
    class sLinkList:public list<elemType>{
    private: struct node{ // 单链表中的节点类 elemType data; node* next; node(const elemType & x, node* n = NULL) {data = x; next = n;} node():next(NULL){} ~node(){} }; node* head; // 头指针 int currentLength; // 表长 node* move(int i) const; // 返回指向第i个结点的指针 class NoSuchNodeError{}; // 按索引的结点不存在导致的错误 public: sLinkList(); ~sLinkList(){clear(); delete head;} void clear(); int length() const{return currentLength;} void insert(int i, const elemType & x); void remove(int i); int search(const elemType & x) const; elemType visit(int i) const; void traverse() const; };

    函数的类外定义,注意在构造函数中应该新建一个哨兵节点,这样就保证所有储存信息的节点都有前一个节点,可以简化代码和加快运行速度

    template<typename elemType>
    typename sLinkList<elemType>::node* sLinkList<elemType>::move(int i) const{
        node* p = head;
    
        while (i-- >= 0) p = p->next;
        return p;
    }
    
    
    template<typename elemType>
    sLinkList<elemType>::sLinkList(){
        head = new node;                                     // 哨兵结点
        currentLength = 0;
    }
    
    
    template<typename elemType>
    void sLinkList<elemType>::clear(){
        node* p = head->next;
        node* q;
    
        head->next = NULL;
        while(p != NULL){
            q = p->next;
            delete p;
            p = q;
        }
        currentLength = 0;
    }
    
    
    template<typename elemType>
    void sLinkList<elemType>::insert(int i, const elemType & x){
        if(i > currentLength || i < 0) throw NoSuchNodeError();
    
        node* pos = move(i - 1);
    
        pos->next = new node(x, pos->next);
        ++ currentLength;
    }
    
    
    template<typename elemType>
    void sLinkList<elemType>::remove(int i){
        if(i >= currentLength || i < 0) throw NoSuchNodeError();
    
        node* pos = move(i - 1);
        node* delp = pos->next;
    
        pos->next = delp->next;
        delete delp;
        --currentLength;
    }
    
    
    template<typename elemType>
    int sLinkList<elemType>::search(const elemType & x) const{
        node* p = head->next;
        int i = 0;
    
        while(p!= NULL && p->data != x){p = p->next; ++i;}
        if(p == NULL) return -1; else return i;
    }
    
    
    template<typename elemType>
    elemType sLinkList<elemType>::visit(int i) const{
        if(i >= currentLength || i < 0) throw NoSuchNodeError();
    
        return move(i)->data;
    }
    
    
    template<typename elemType>
    void sLinkList<elemType>::traverse() const{
        node* p = head->next;
    
        while(p != NULL){
            cout << p->data << " ";
            p = p->next;
        }
        cout << endl;
    }
  • 相关阅读:
    HDU----(4291)A Short problem(快速矩阵幂)
    HDU----(2157)How many ways??(快速矩阵幂)
    hdu---(2604)Queuing(矩阵快速幂)
    hdu---(5038)Grade(胡搞)
    齐次方程到矩阵(番外篇)
    uva--1339
    hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)
    hdu----(2848)Repository(trie树变形)
    hdu---(1800)Flying to the Mars(trie树)
    hdu----(1075)What Are You Talking About(trie之查找)
  • 原文地址:https://www.cnblogs.com/LC32/p/13435364.html
Copyright © 2020-2023  润新知