您的位置:首页 > 其它

Collection with Inheritance

2016-04-21 21:31 387 查看
本周一个作业是根据下图实现一个collection,当然,只需要做list那条分支就好了



由于看过C Primer Plus关于虚析构函数的部分,所以很自然地把基类的析构函数声明成了virtural,也就没遇到评论区那个问题。
不过,问题还是有的...


1.error:allocating an object of abstract class type

原因是没把抽象函数写全

2.在remove那debug了好久,还是不熟练啊。

3.然后。。。噢,不会用归并排序。。。。但过了。。。

然后向大家推荐一下+c大神的eden抓文件助手:https://github.com/DaddyTrap/eden_clone_codes

很强大好不,10多个cpp,hpp什么的一下就放到自己命名的文件夹了,助你轻松解决陪陪师兄的变态题哈哈…

好吧,大家应该是想看代码的,按道理,只要我不把xcode自带注释加上来,应该你们直接复制是不会关联到我的帐号并出现抄袭吧,要不。。你们试试哈哈,我不管。。我就是要贴代码。。。

Collection.hpp

#ifndef Collection_h
#define Collection_h

class Collection {
public:
typedef int E;
public:
Collection() {}
virtual ~Collection() {}
virtual void add(E) = 0;
virtual void clear() = 0;
virtual bool contain(E) = 0;
virtual bool isEmpty() = 0;
virtual void remove(E) = 0;
virtual int size() = 0;
void Qsort(E *arr, E left, E right) {
if (left >= right) {
return;
}
E i = left;
E j = right;
E key = arr[left];
while(i < j) {
while (key <= arr[j] && i < j) {
j--;
}
arr[i] = arr[j];
while (key >= arr[i] && i < j) {
i++;
}
arr[j] = arr[i];
}
arr[i] = key;
Qsort(arr, left, i - 1);
Qsort(arr, i + 1, right);
}
};
#endif /* Collection_h */


List.hpp

#ifndef List_h
#define List_h
#include "Collection.hpp"

class List : public Collection {
public:
virtual E& operator[](int index) = 0;
virtual E& get(int index) = 0;
virtual int indexOf(E) = 0;
virtual void sort() = 0;
};

#endif /* List_h */


LinkedList.hpp

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#include "List.hpp"
#include <iostream>

class LinkedList : virtual public List {
public:
typedef struct node {
E data;
struct node* next;
struct node* prev;
node(E data, struct node* next = NULL, struct node* prev = NULL)
: data(data), next(next), prev(prev) {}
} node;
LinkedList();
~LinkedList();
virtual void add(E e);
virtual void clear(void);
virtual bool contain(E e);
virtual bool isEmpty(void);
virtual void remove(E e);
virtual E& operator[](int index);
virtual E& get(int index);
virtual int indexOf(E element);
virtual void sort(void);
virtual int size(void);

private:
node* head;
node* tail;
int _size;
};

#endif


ArrayList.hpp

#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_

#include "List.hpp"

class ArrayList : public List {
public:
ArrayList();
~ArrayList();
virtual void add(E e);
virtual void clear(void);
virtual bool contain(E e);
virtual bool isEmpty(void);
virtual void remove(E e);
virtual E& operator[](int index);
virtual E& get(int index);
virtual int indexOf(E element);
virtual void sort(void);
virtual int size(void);

private:
E* storage;
int _size;
int _maxsize;
static const int extend_factor = 2;
void extend(void);
};

#endif


ArrayList.cpp

#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_

#include "List.hpp"

class ArrayList : public List {
public:
ArrayList();
~ArrayList();
virtual void add(E e);
virtual void clear(void);
virtual bool contain(E e);
virtual bool isEmpty(void);
virtual void remove(E e);
virtual E& operator[](int index);
virtual E& get(int index);
virtual int indexOf(E element);
virtual void sort(void);
virtual int size(void);

private:
E* storage;
int _size;
int _maxsize;
static const int extend_factor = 2;
void extend(void);
};

#endif


LinkedList.cpp

//
//  LinkedList.cpp
//  D&A 5 Collection with Inheritance
//
//  Created by 邱兆丰 on 16/4/18.
//  Copyright © 2016年 菇生. All rights reserved.
//

#include "LinkedList.hpp"

LinkedList::LinkedList() {
head = NULL;
tail = NULL;
_size = 0;
}

LinkedList::~LinkedList() {
clear();
}

void LinkedList::add(E e) {
//  不知道题目要在头加还是在尾部加,先在头加;
if (head == NULL) {
head = new node(e, NULL, NULL);
tail = head;
_size++;
} else {
node* temp = new node(e, head, NULL);
head->prev = temp;
head = temp;
_size++;
}
}

void LinkedList::clear(void) {
node* p = head;
node* temp = NULL;
if (p != NULL) {
while (p->next != NULL) {
temp = p;
p = p->next;
delete temp;
}
delete p;
}
head = NULL;
tail = NULL;
_size = 0;
}

bool LinkedList::contain(E e) {
node* p = head;
while (p != NULL) {
if (p->data == e) {
return true;
}
p = p-> next;
}
return false;
}

bool LinkedList::isEmpty(void) {
return _size == 0;
}

void LinkedList::remove(E e) {
node* p = head;
node* temp = NULL;
while (p != NULL) {
if (p->data == e) {
if (_size == 1) {
delete head;
tail = NULL;
head = NULL;
p = NULL;
_size--;
return;
}
else if (p == head) {
temp = p;
p = p->next;
p->prev = NULL;
head = p;
delete temp;
}
else if (p == tail) {
tail = p->prev;
p->prev->next = NULL;
delete p;
p = NULL;
} else {
p->next->prev = p->prev;
p->prev->next = p->next;
temp = p->next;
delete p;
p = temp;
}
_size--;
} else {
p = p->next;
}
}
}

Collection::E& LinkedList::operator[](int index) {
return get(index);
}

Collection::E& LinkedList::get(int index) {
node* p = head;
while (index--) {
p = p->next;
}
return p->data;
// 无错误判定
}

int LinkedList::indexOf(E element) {
node* p = head;
unsigned count = 0;
while (p != NULL) {
if (p->data == element) {
return count;
}
count++;
p = p->next;
}
return -1;
}

void LinkedList::sort(void) {
if (_size > 1) {
node* slow = head;
node* fast = head->next;
node* p = NULL;
while (fast != NULL) {
if (fast->data >= slow->data) {
fast = fast->next;
slow = slow->next;
} else {
p = head;
if (head->data > fast->data) {
slow->next = fast->next;
if (fast->next != NULL) {
fast->next->prev = slow;
}
fast->next = head;
fast->prev = NULL;
head->prev = fast;
head = fast;
} else {
while (p->next->data <= fast->data) {
p = p->next;
}
slow->next = fast->next;
if (fast->next != NULL) {
fast->next->prev = slow;
}
fast->next = p->next;
p->next->prev = fast;
p->next = fast;
fast->prev = p;
}
fast = slow->next;
}
}
tail = slow;
}
}

int LinkedList::size(void) {
return _size;
}


测试程序main.cpp

#include <iostream>
#include <cstdlib>
#include "Collection.hpp"
#include "List.hpp"
#include "LinkedList.hpp"
#include "ArrayList.hpp"
#include <exception>

using std::cin;
using std::cout;
using std::endl;
using std::exception;

class AlgorithmnForbidden : public exception {
virtual const char *what() const throw() {
return "Please do not use std::sort or std::list or std::vector .....";
}
};

class TEST {
private:
int *testData;
int data_size;

public:
TEST() {
#if defined(_GLIBCXX_ALGORITHM) || defined(_GLIBCXX_LIST) || \
defined(_GLIBCXX_VECTOR)
//throw AlgorithmnForbidden();
cout << "please do not use algorithm" << endl;
#endif
cin >> data_size;
cout << "test data size:" << data_size << endl;
testData = new int[data_size];
for (int i = 0; i < data_size; i++) {
cin >> testData[i];
}
}

~TEST() { delete[] testData; }

void test_List(Collection *c) {
cout << (c->isEmpty() ? "true" : "false") << endl;

int n = data_size;

for (int i = 0; i < n; i++) {
c->add(testData[i]);
}

reinterpret_cast<List *>(c)->sort();

for (int i = 0; i < n; i++) {
cout << (*reinterpret_cast<List *>(c))[i] << " ";
}

cout << endl;

// not empty
cout << (c->isEmpty() ? "true" : "false") << endl;

for (int i = 0; i < n / 2; i++) {
cout << "(" << (c->contain(i) ? "true" : "false");
cout << ","
<< (reinterpret_cast<List *>(c)->indexOf(i) != -1 ? "true" : "false")
<< ") ";
c->remove(i);
}

cout << endl;

for (int i = 0; i < c->size(); i++) {
cout << (*reinterpret_cast<List *>(c))[i] << " ";
}
cout << endl;
}

void test_ArrayList() {
Collection *c = new ArrayList();
test_List(c);
delete c;
}

void test_LinkedList() {
Collection *c = new LinkedList();
test_List(c);
delete c;
}

void runAllTests() {
cout << "Testing ArrayList:" << endl;
test_ArrayList();
cout << endl;
cout << "Testing LinkedList:" << endl;
test_LinkedList();
}
};

int main() {
TEST t;
t.runAllTests();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: