+ -
当前位置:首页 → 问答吧 → 在数据结构中继承的问题

在数据结构中继承的问题

时间:2011-12-25

来源:互联网

C/C++ code


/*
 * List.h
 *
 *  Created on: 2011-12-3
 *      Author: 
 */

#ifndef LIST_H_
#define LIST_H_

template <typename T>
class List {
public:
    List();
    virtual ~List();
    virtual int getSize() = 0;
    virtual int getLength() = 0;
    virtual bool isEmpty() = 0;
    virtual bool isFull() = 0;
    virtual int locaton(const T& x) = 0;
    virtual int insert(int k, const T& x) = 0;
    virtual int remove(int k, const T& x) = 0;
};



#endif /* LIST_H_ */


/*
 * SeqList.h
 *
 *  Created on: 2011-12-3
 *      Author: 
 */

#ifndef SEQLIST_H_
#define SEQLIST_H_

#include "List.h"
#include <iostream>
using namespace std;

const int DEFAULTSIZE = 100;

template <typename T>
class SeqList : public List<T> {
private:
    T* data;
    int size;
    int last;

public:
    SeqList(int sz = DEFAULTSIZE);
    ~SeqList();
    int getSize();
    int getLength();
    bool isEmpty();
    bool isFull();
    int location(const T& x);
    int remove(int k, T& x);
    int insert(int k, const T& x);
    void input();
    void output();
};

template <class T>
SeqList<T>::SeqList(int sz) {

    size = sz;
    data = new T[size];
    last = -1;
}

template <typename T>
SeqList<T>::~SeqList() {
    delete[] data;
}

template <typename T>
int SeqList<T>::getLength() {
    return last;
}

template <typename T>
int SeqList<T>::getSize() {
    return size;
}

template <typename T>
bool SeqList<T>::isEmpty() {
    return last == -1;
}

template <typename T>
bool SeqList<T>::isFull() {
    return last == size - 1;
}

template <typename T>
int SeqList<T>::location(const T& x) {
    int i = 0;
    while (i <= last && data[i] != x) i++;
    if (i > last) {
        return -1;
    } else {
        return i;
    }
}

template <typename T>
int SeqList<T>::remove(int k, T& x) {
    if (k > last || k < 0) {
        return 0;
    }
    last--;
    x = data[k];
    for (int i = k; i <= last; i++) {
        data[i] = data[i + 1];
    }
    return 1;
}

template <typename T>
int SeqList<T>::insert(int k, const T& x) {
    if (k > last + 1 || k < 0 || last == size - 1) {
        return 0;
    }
    last++;
    int i;
    for (i = last; i > k; i--) {
        data[i] = data[i - 1];
    }
    data[i] = x;
    return 1;
}

template <typename T>
void SeqList<T>::input() {
    cout << "How many elements do you want to input?";
    cin >> last;
    if (last > size - 1) {
        cout << "overflow!";
        return;
    }
    for (int i = 0; i < last; i++) {
        cin >> data[i];
    }
}

template <typename T>
void SeqList<T>::output() {
    for (int i = 0; i < last; i++) {
        cout << data[i] << " ";
    }
}


#endif /* SEQLIST_H_ */

/*
 * main.cpp
 *
 *  Created on: 2011-12-3
 *      Author: 
 */

#include "SeqList.h"
#include <iostream>
using namespace std;

int main() {
    SeqList<int> seqList(10);
    seqList.input();
    cout << seqList.getLength() << endl;
    int k = 4;
    seqList.insert(3, k);
    seqList.output();
    cout << endl;
    seqList.remove(3, k);
    seqList.output();
    cout << endl;
    k = 9;
    cout << seqList.location(k) << endl;
}




报错如下:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\main.o ..\src\main.cpp
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:13:15: error: cannot declare variable 'seqList' to be of abstract type 'SeqList<int>'
..\src\/SeqList.h:18:7: note: because the following virtual functions are pure within 'SeqList<int>':
..\src\/List.h:20:14: note: int List<T>::locaton(const T&) [with T = int]
..\src\/List.h:22:14: note: int List<T>::remove(int, const T&) [with T = int]
Build error occurred, build is stopped
Time consumed: 230 ms.  

c++自学了两周,上课学的是c,但是数据结构用c++描述,现在对于c++不是狠明白,求教。

作者: im_AK47   发布时间: 2011-12-25

注意书写错误问题:
virtual int locaton(const T& x) = 0;
与 int location(const T& x);

作者: bottlebox   发布时间: 2011-12-25

热门下载

更多