-1

我很难在我的数组列表中实现一些函数。

我想使用预定义的函数“插入”将一个值插入到列表中。然后我想使用检索函数打印出整个列表的值。

但是,当我测试我的函数时,程序在插入最后一个值后以分段核心转储退出

任何帮助,将不胜感激。

标题:

/** @file ListA.h */
#include <string>
using namespace std;
const int MAX_LIST = 0;
typedef string ListItemType;
class List
{

public:
   List();
   bool isEmpty() const;
   int getLength() const;
   void insert(int index, const ListItemType& newItem, bool& success);
   void retrieve(int index, ListItemType& dataItem, bool & success) const;
   void remove(int index, bool& success);
   List selectionSort(List selectList);
private:
    ListItemType items[];
    int size;
    int translate(int index) const;
};

执行:

     /** @file ListA.cpp */
#include "ArrayList.h"  // header file
#include <iostream>
#include <fstream>
List::List() : size()
{
}
bool List::isEmpty() const
{
   return size == 0;
}
int List::getLength() const
{
   return size;
}
void List::insert(int index, const ListItemType& newItem, bool& success)
{
   success = (index >= 1) &&
             (index <= size + 1) &&
             (size < MAX_LIST);
   if (success)
   {
      for (int pos = size; pos >= index; --pos)

         items[translate(pos + 1)] = items[translate(pos)];
      items[translate(index)] = newItem;

      ++size;

   }

}
void List::remove(int index, bool& success)
{
   success = (index >= 1) && (index <= size);
   if (success)
   {
      for (int fromPosition = index + 1;
       fromPosition <= size;
       ++fromPosition)
         items[translate(fromPosition - 1)] = items[translate(fromPosition)];
      --size;  // decrease the size of the list by one
   }  // end if

}  // end remove

void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
   success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[translate(index)];
}

int List::translate(int index) const
{
   return index - 1;
}

List List::selectionSort(List selectList)

{


return selectList;
}


 int main()
 {
 ListItemType insertType = "listItem1";
 ListItemType retrieveType = "listitem2";
 int numberofitems;
 cout << "Please enter the number of data items:" << endl;
 cin >> numberofitems;
 cout << endl;
 cout << "Please enter the data items, one per line:" << endl;
 int listofitems[numberofitems];
 List myArrayList;
 cout << myArrayList.getLength() << endl;
        if (myArrayList.isEmpty())  // tests before
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
 bool mainsucc = true;
 for (int i = 0; i<numberofitems; i++)
 {
 cout << "Enter number " << i + 1 << " : " << endl;
 cin >> listofitems[i];
 }
 for (int i =0; i <numberofitems; i++){
 myArrayList.insert(listofitems[i],  insertType, mainsucc);}
 cout << "Size of the list is : " << myArrayList.getLength() << endl;
/*for (int i=0; i<mainarraylistsize; i++)
 {
 cout << myArrayList.retrieve(listofitems[i], retrieveType, mainsucc);
}*/
if (myArrayList.isEmpty())  // tests after
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
 return 1;
 }
4

1 回答 1

0

拥有 MAX_LIST == 0 意味着成功在您的插入函数中始终为假。

于 2013-03-13T17:59:04.037 回答