C++模版类实现单链表(继承自抽象基类线性表)

简单测试过一下,应该没有问题,如有疏忽,欢迎指正。

1、抽象基类——LinearList.h

#ifndef LinearList_h
#define LinearList_h
template <class T>
class LinearList{
public:
    LinearList(){}
    virtual ~LinearList(){}
    //virtual int Size()const = 0;
    virtual int Length()const = 0;
    virtual int Search(T& x)const = 0;
    //virtual int Locate(int i)const = 0;
    virtual T getData(int i)const = 0;
    virtual void setData(int i, T& x) = 0;
    virtual bool Insert(int i, T& x) = 0;
    virtual bool Remove(int i, T& x) = 0;
    virtual bool IsEmpty()const = 0;
    //virtual bool IsFull()const = 0;
    //virtual void Sort() = 0;
    virtual void input() = 0;
    virtual void output() = 0;
};

#endif /* LinearList_h */

2、具体类——LinkedList.h

#ifndef List_h
#define List_h
#include "LinearList.h"
template <class T>      //C++中结构体中也可以有构造函数,类似于类,区别仅在于结构体默认是public,类默认private
struct LinkNode{
    T data;
    LinkNode<T>* link;
    LinkNode(LinkNode<T>* ptr = NULL) : link(ptr){}
    LinkNode(const T& item, LinkNode<T> *ptr = NULL) : data(item), link(ptr){}
};

template <class T>
class List : public LinearList<T>{
protected:
    LinkNode<T>* first;
public:
    List(){first = new LinkNode<T>;}
    List(const T& x){first = new LinkNode<T>(x);}
    List(List<T>& L);
    ~List(){makeEmpty();}
    void makeEmpty();
    LinkNode<T>* getHead()const{return first;}
    void setHead(LinkNode<T>* p){first = p;}
    int Size()const;
    int Length()const;
    int Search(T& x)const;
    LinkNode<T>* Locate(int i)const;
    T getData(int i)const;
    void setData(int i, T& x);
    bool Insert(int i, T& x);
    bool Remove(int i, T& x);
    bool IsEmpty()const;
    bool IsFull()const;
    //void Sort();
    void input();
    void output();
    List<T>& operator=(List<T>& L);
};

#endif /* List_h */

3、具体类中非内联函数的实现——LinkedList.cpp

#include <iostream>
#include <stdio.h>
#include "LinkedList.h"
using std::cerr;
using std::endl;
using std::cout;
using std::cin;

template <class T>
List<T>::List(List<T>& L){
    T value;
    LinkNode<T>* srcptr = L.getHead();
    LinkNode<T>* destptr = first = new LinkNode<T>;
    while(srcptr->link != NULL){
        value = srcptr->link->data;
        destptr->link = new LinkNode<T>(value);
        destptr = destptr->link;
        srcptr = srcptr->link;
    }
}

template <class T>
void List<T>::makeEmpty(){
    LinkNode<T>* q;
    while(first->link != NULL){
        q = first->link;
        first->link = q->link;
        delete q;
    }
}

template <class T>
int List<T>::Length()const{
    LinkNode<T>* p = first->link;
    int count = 0;
    while(p != NULL){
        p = p->link;
        count ++;
    }
    return count;
}

template <class T>
int List<T>::Search(T& x)const{
    LinkNode<T>* current = first->link;
    int count = 1;
    while(current != NULL)
        if(current->data == x)
            break;
        else{
            current = current->link;
            count ++;
        }
    if(current != NULL)
        return count;
    return 0;
}

template <class T>
LinkNode<T>* List<T>::Locate(int i)const{
    if(i <= 0)
        return NULL;
    LinkNode<T>* current = first->link;
    int k = 1;
    while(current != NULL && k < i){
        current = current->link;
        k++;
    }
    return current;
}

template <class T>
T List<T>::getData(int i)const{
    if(i < 1) return NULL;
    LinkNode<T>* current = Locate(i);
    if(current == NULL)
        return NULL;
    return current->data;
}

template <class T>
void List<T>::setData(int i, T &x){
    if(i < 1)
        return;
    LinkNode<T>* current = Locate(i);
    if(current == NULL)
        return;
    current->data = x;
}

template <class T>
bool List<T>::Insert(int i, T &x){
    LinkNode<T>* current = Locate(i);
    if(current == NULL)
        return false;
    LinkNode<T>* newNode = new LinkNode<T>(x);
    if(newNode == NULL){
        cerr << "存储分配错误!" << endl;
        exit(1);
    }
    newNode->link = current->link;
    current->link = newNode;
    return true;
}

template <class T>
bool List<T>::Remove(int i, T &x){
    LinkNode<T>* current = Locate(i-1);
    if(current == NULL || current->link == NULL)
        return false;
    LinkNode<T>* del = current->link;
    current->link = del->link;
    x = del->data;
    delete del;
    return true;
}

template <class T>
bool List<T>::IsEmpty()const{
    if(first->link->link == NULL)
        return true;
    return false;
}

template <class T>
void List<T>::output(){
    int count = 1;
    LinkNode<T>* current = first->link;
    while(current != NULL){
        cout << count <<": " << current->data << endl;
        current = current->link;
        count ++;
    }
}

template <class T>
List<T>& List<T>::operator=(List<T> &L){
    T value;
    LinkNode<T>* srcptr = L.getHead();
    LinkNode<T>* destptr = first;
    while(srcptr->link != NULL){
        value = srcptr->link->data;
        destptr->link = new LinkNode<T>(value);
        destptr = destptr->link;
        srcptr = srcptr->link;
    }
    //destptr->link = NULL
    return *this;
}

//前插法建立单链表
/*
template <class T>
void List<T>::input(){
    cout << "开始建立单链表,请输入表中元素个数: ";
    int a;
    while(1){
        cin >> a;
        if(a >= 1)
            break;
        cout << "表元素输入有误,必须为大于等于1的数" << endl;
    }
    cout << "请逐个输入表元素:" << endl;
    LinkNode<T>* newNode;
    T val;
    for(int i = 1; i <= a; i++){
        cout << i <<": ";
        cin >> val;
        newNode = new LinkNode<T>(val);
        if(newNode == NULL){
            cerr << "存储分配错误!" << endl;
            exit(1);
        }
        newNode->link = first->link;
        first->link = newNode;
    }
}
*/

//后插法建立单链表
template <class T>
void List<T>::input(){
    cout << "开始建立单链表,请输入表中元素个数: ";
    int a;
    while(1){
        cin >> a;
        if(a >= 1)
            break;
        cout << "表元素输入有误,必须为大于等于1的数" << endl;
    }
    cout << "请逐个输入表元素:" << endl;
    LinkNode<T>* newNode,* last;
    T val;
    last = first;
    for(int i = 1; i <= a; i++){
        cout << i <<": ";
        cin >> val;
        newNode = new LinkNode<T>(val);
        if(newNode == NULL){
            cerr << "存储分配错误!" << endl;
            exit(1);
        }
        last->link = newNode;
        last = newNode;
    }
}

4、主函数——简单测试一下

#include <iostream>
#include "LinkedList.cpp"
void Merge(SeqList<int>& LA, SeqList<int>& LB);
void Intersection(SeqList<int>& LA, SeqList<int>& LB);

int main(){
    List<int> list1,list3;
    list1.input();
    list1.output();
    List<int> list2(list1);
    list2.output();
    list3 = list1;
    list3.output();
    int l = list1.Length();
    cout << "the length of list1 is: " << l << endl;
    cout << "the result of search is: " << list1.Search(l) << endl;
    cout << list1.IsEmpty() << endl;
    list1.setData(2, l);
    int a = 100;
    list1.Insert(2,a);
    list1.output();
    int b;
    list1.Remove(3, b);
    list1.output();
    return 0;
}
发布了13 篇原创文章 · 获赞 0 · 访问量 448

猜你喜欢

转载自blog.csdn.net/Nemoosi/article/details/104329179
今日推荐