数据结构之顺序表——C++模板类实现

线性表定义

#ifndef LINEARLIST_H_INCLUDED
#define LINEARLIST_H_INCLUDED
#include <bits/stdc++.h>
using namespace std;
template<class T>
class LinearList {
public:
        LinearList() {cout << "Constructing a LinearList in default way" << endl;}
        ~LinearList() {cout << "Desctructing a LinearList in default way" << endl;}
        virtual int Size() const = 0;                   // Get the max length
        virtual int Length() const = 0;                 // Get length
        virtual int Search(T& x) const = 0;             // Search the element x in LinearList
        virtual int Locate(int i) const = 0;            // Get the index of i-th element
        virtual bool GetData(int i, T& x) const = 0;    // Get the value of i-th element
        virtual void SetData(int i, T& x) = 0;          // Modify the value of i-th element
        virtual bool Insert(int i, T& x) = 0;           // Insert x in index i-1
        virtual bool Remove(int i, T& x) = 0;           // Delete i-th element
        virtual bool IsEmpty() const = 0;               // Whether the SequentialList is empty or not
        virtual bool IsFull() const = 0;                // Whether the SequentialList is full or not
        virtual void Sort() = 0;                        // Sort all elements in SequentialList by non-decreasing
        virtual void Input()  = 0;                      // Input elements
        virtual void Output() = 0;                      // Output all elements in SequentialList
        //virtual LinearList<T> operator=(LinearList<T>& L) = 0;  // Overloading =
};


#endif // LINEARLIST_H_INCLUDED


顺序表定义

#ifndef SinglyLinkedListEDLIST_H_INCLUDED
#define SinglyLinkedListEDLIST_H_INCLUDED
#include <bits/stdc++.h>
using namespace std;

template<class T>
class LinkNode {
public:
        LinkNode(LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by default way!" << endl; link = ptr;}
        LinkNode(const T& item, LinkNode<T>* ptr = NULL) {cout << "Constructing a LinkNode by defined way!" << endl; data = item, link = ptr;}
        ~LinkNode() {cout << "Desctructing a LineNode!" << endl; delete link;}
        T data;                    // Data Scope
        LinkNode<T> *link;         // Point Scope, Point to next Node
};

// SinglyLinkedListList with head node
template <class T>
class SinglyLinkedList : public LinkNode<T> {
public:
        SinglyLinkedList() {cout << "Constructing a SinglyLinkedListList by default way" << endl; first = new LinkNode<T>;}
        SinglyLinkedList(const T&x);
        SinglyLinkedList(SinglyLinkedList<T>& L);       // Deep copy constructor function
        ~SinglyLinkedList() {cout << "Destructing a SinglyLinkedListNode by default way" << endl; delete first;}
        void makeEmpty();           // Delete all nodes in the SinglyLinkedList
        int Length() const;         // Get the length of the SinlyLinkedList
        LinkNode<T> *GetHead() const {return first;} // Return first
        LinkNode<T> *Search(T x);   // x can only be a variable while T& x
        LinkNode<T> *Locate(int i); // Get the i-th node's address
        bool GetData(int i, T& x);        // Get the data in i-th node and let x = data
        void SetData(int i, T& x);              // Make the data in i-th node equals x
        bool Insert(int i, T& x);               // Insert a node in i-th with its' data be x
        bool Remove(int i, T& x);               // Delete the i-th node and make x = node[i]->data
        bool IsEmpty() const {return first->link == NULL ? true : false;}
        bool IsFull() const {return false;}
        void Sort();                            // Sort all datas in the SinglyLinkedList by non-decreasing way
        void Input();                           // Input all elements
        void Output();                          // Out all elements
        SinglyLinkedList<T>& operator=(SinglyLinkedList<T>& L); // Overloading operator =
protected:
        LinkNode<T> *first;     // Point to Head node
};

#endif // SinglyLinkedListEDLIST_H_INCLUDED

顺序表实现

#include <bits/stdc++.h>
#include "SequentialList.h"

using namespace std;

template<class T>
SequentialList<T>::SequentialList(int sz) {         // Constructor
        cout << "Constructing a SequentialList in defined way" << endl;
        if(sz >= 0) {
                maxSize = sz; last = -1;
                data = new T[maxSize];
                if(data == NULL) {
                        cerr << "Invalid allocation" << endl;
                        exit(1);
                }
        }
        else {
                cerr << "Invalid size" << endl;
                exit(1);
        }
}

template<class T>
SequentialList<T>::SequentialList(SequentialList<T>& L) {       // Deep copy constructor function
        maxSize = L.Size();
        last = L.Length() - 1;
        T value;
        data = new T[maxSize];
        if(data == NULL) {
                cerr << "Invalid allocation" << endl;
                exit(1);
        }
        for(int i = 1; i <= last+1; ++i) {
                L.GetData(i, value);
                data[i-1] = value;
        }
}

template<class T>
int SequentialList<T>::Search(T& x) const {
        for(int i = 0; i <= last; ++i) {
                if(data[i] == x) {return i+1;}          // Return the order of element x in SequentialList
        }
        return 0;       // not found
}

template<class T>
int SequentialList<T>::Locate(int i) const {
        if(i >= 1 && i <= last+1) {return i;}
        return 0;
}

template<class T>
bool SequentialList<T>::GetData(int i, T& x) const {
        if(i <= 0 || i > last+1) {return false;}
        else {
                x = data[i-1];
                return true;
        }
}

template<class T>
void SequentialList<T>::SetData(int i, T& x) {
        if(i <= 0 || i > last+1) {
                cerr << "Invalid order of element x" << endl;
                exit(1);
        }
        data[i-1] = x;
}

template<class T>
bool SequentialList<T>::Insert(int i, T& x) {
        if(i <= 0 || i > last+2) {
                cerr << "Invalid order of element x" << endl;
                return false;
        }
        else {
                last++;
                for(int pos = last; pos >= i; --pos) {
                        data[pos] = data[pos-1];
                }
                data[i-1] = x;
                return true;
        }
}

template<class T>
bool SequentialList<T>::Remove(int i, T& x) {
        if(i <= 0 || i > last+1) {
                cerr << "Invalid order of element x" << endl;
                return false;
        }
        else {
                x = data[i-1];
                for(int pos = i-1; pos < last; ++pos) {
                        data[pos] = data[pos+1];
                }
                last--;
                return true;
        }
}

template<class T>
bool SequentialList<T>::IsEmpty() const {
        return last == -1 ? true : false;
}

template <class T>
bool SequentialList<T>::IsFull() const {
        return maxSize <= last ? true : false;
}

template <class T>
void SequentialList<T>::Sort() {        // Sort SequentialList in non-decreasing way
        for(int i = last; i > 0; --i) {
           for(int j = i-1; j >= 0; --j) {
                if(data[j] > data[i]) {
                        T tmp = data[j];
                        data[j] = data[i];
                        data[i] = tmp;
                }
                }
        }
}

template <class T>
void SequentialList<T>::Input() {
        cout << "Please enter you final index of element to construct a SequentialList " << endl;
        while(true) {
                cin >> last;
                if(last < maxSize && last >= 0) {
                        break;
                }
                else {
                        cout << "Invalid index " << endl;
                        cout << "The number of elements in SequentialList not exceed " << maxSize << endl;
                }
        }
        for(int i = 0; i <= last; ++i) {
                cin >> data[i];
        }
}

template <class T>
void SequentialList<T>::Output(){
        cout << "The last index of the SequentialList is " << last << endl;
        for(int i = 0; i <= last; ++i) {
                cout << "#No." << i+1 << ": " << data[i] << endl;
        }
}


template <class T>
SequentialList<T> SequentialList<T>::operator=(SequentialList<T>& L) {
        int sz = L.Size();
        T value;
        SequentialList<T> newSequentialList = new SequentialList(sz);
        newSequentialList.last = L.last;
        for(int i = 0; i <= last; ++i) {
                L.GetData(i+1, value);
                newSequentialList.data[i] = value;
        }
        return newSequentialList;
}

int main() {
        SequentialList<int> seqList(10);
        seqList.Input();
        seqList.Output();
        SequentialList<int> seqListPlus = seqList;
        seqListPlus.Sort();
        seqListPlus.Output();
        int a = 3;
        int pos = seqListPlus.Search(a);
        cout << pos << endl;
        if(seqListPlus.GetData(3, a)){
                cout << "Jesus Christ, Found it" << endl;
                cout << a << endl;
        }
        cout << "size " << seqListPlus.Size() << endl;
        if(seqListPlus.IsFull()) {
                cout << "God like, is full" << endl;
        }
        else cout << "Oh, not full" << endl;
        SequentialList<double> emptySequentialList(0);
        if(emptySequentialList.IsEmpty()) {
                cout  << "God like, is empty" << endl;
        }
        double num = 0.1;
        if(emptySequentialList.Insert(1, num)) {
                cout << "Inserted successfully" << endl;
        }
        emptySequentialList.Output();
        double val;
        emptySequentialList.Remove(1, val);
        emptySequentialList.Output();
        cout << val << endl;
        return 0;
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c++实现顺序表:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序表末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序表容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值