配列ベースのリストのプログラムには、次の3回を呼び出す関数があります。
list->remove(1)
私の問題は、これが印刷されると、次の新しい最初のアイテムに移動するのではなく、元の最初のアイテムを3回削除すると表示されることです。
具体的には、このコードが呼び出されたときtest.cpp
:
for (int i = 1; i <= 3 && ! list->isEmpty(); i++) {
cout << "Deleting item " << i << "." << endl;
try {
cout << "Deleted item " << list->remove(1) << endl;
}
catch (int e) {
cout << "*** Error deleting from position " << i << " ***" << endl;
}
}
次を返します。
アイテムの削除1.
アイテムの削除1アイテムの
削除2.
アイテムの削除1アイテムの
削除3.
アイテムの削除1
これが私が作成したABList
クラスです。エラーは私のremove
メソッドにあると思いますが、どこにあるのかわかりません:
#ifndef _ABLIST_H_
#define _ABLIST_H_
#define LIST_MAX 10
#include <iostream>
#include "ABCList.hpp"
using namespace std;
template <class T>
class ABList : public ABCList<T> {
private:
T a[LIST_MAX];
int size;
public:
ABList ();
~ABList ();
virtual bool isEmpty ();
virtual int getLength ();
virtual void insert (int pos, T item);
virtual T remove (int pos);
virtual T retrieve (int pos);
};
template <class T>
ABList<T>::ABList () {
size = 0;
}
template <class T>
bool ABList<T>::isEmpty () {
if (size == 0)
return true;
return false;
}
// returns size, which is updated whenever the list is lengthened or shortened
template <class T>
int ABList<T>::getLength() {
return size;
}
template <class T>
void ABList<T>::insert(int pos, T item) {
try {
// if list is at maximum size
if (size >= LIST_MAX)
throw "Size is greater than or equal to LIST_MAX\n";
// if "pos" is a negative number
if (pos < 0)
throw "Pos must be greater than 0\n";
// if "pos" is outside the bounds of the existing list
if (pos > size + 1)
throw "Pos must be less than or equal to list size\n";
//shift all items at indices > pos one index up
for (int i = size; i >= pos; --i) {
a[i] = a[i-1];
}
//insert new item
a[pos-1] = item;
//increment size
size++;
} catch (char* message) {
cout << "Error: " << message;
throw 1; // any int will do, to catch exception flag in test.cpp
}
}
template <class T>
T ABList<T>::remove(int pos) {
try {
if (pos < 1)
throw "Pos cannot be less than 1";
if (pos > size)
throw "Pos cannot be greater than size";
//find T to be removed, to return later
T temp = retrieve(pos);
//shift all items greater than pos down one index
for (int i = pos + 1; i <= size; i++)
a[i] = a[i+1];
// decrement size
size--;
return temp;
} catch (char* message) {
cout << "Error: " << message;
throw 1;
}
}
template <class T>
T ABList<T>::retrieve(int pos) {
try {
//check that pos is valid
if (pos < 1 || pos > size)
throw "Position is outside bounds of ABList\n";
return a[pos-1];
} catch (char* message) {
cout << "Error: " << message;
}
}
#endif
これがメインプログラムです。(この問題のために、私はそれが不変であると仮定する必要があります):
int main () {
// Testing the List implmenetations; first, get a list:
ABCList<string> * list = getList();
// Test "insert"
cout << "Let's populate the list with a few items." << endl;
for (int i = 1; i <= TEST_SIZE; i++) {
int pos = getPos(i); // Randomise "i" to test
string input;
cout << "Enter a word to place into the list at position " << pos << ": ";
cin >> input;
try {
list->insert(pos, input);
cout << "Successfully inserted at position " << pos << "." << endl;
}
catch (int e) {
cout << "*** Error inserting at position " << pos << " ***" << endl;
}
}
// Test "retrieve" & "getLength"
cout << "List is as follows: \n\t";
for (int i = 1; i <= list->getLength(); i++) {
cout << list->retrieve(i) << " ";
}
cout << endl;
// Test "delete" and "isEmpty"
for (int i = 1; i <= 3 && ! list->isEmpty(); i++) {
cout << "Deleting item " << i << "." << endl;
try {
cout << "Deleted item " << list->remove(1) << endl;
}
catch (int e) {
cout << "*** Error deleting from position " << i << " ***" << endl;
}
}
// Done. Let the destructor handle the rest.
delete list;
return 0;
}