0

我正在编写一个有点像 Boggle 的程序,但是我在制作 Pairs 并将它们放入列表中然后将它们从列表中取出时遇到了问题。

List.cc
    #include <iostream>
    #include <cassert>
    #include <cstdlib>
    #include "list.h"

    using namespace std;

    List::Node::Node()
    {
     prev = next = NULL;
    }

    List:: List()
    {
     front = new Node()
     rear = new Node()
     front->next = rear;
     rear->prev = front;

     currentIndex=0;
     current = front->next;
     size=0;
    }

    List::~List()
    {
     _setCurrentIndex(0);
     while(current)
      {
       Node *temp = current;
       current = current -> next;
       delete temp;
      }
    //not showing deep copy function b/c it isn't important for this program
    void List::add(const ElementType & item, size_t index)
    {
     assert(0<=index && index <= size);
     _setCurrentIndex(index);
     size++;

     Node *born = new Node;
     born->data = item;
     born->prev = current->prev;
     born->prev->next = current;
     born->prev = born;
     current = born;
    }

    void List::removeAt(size_t index)
    {
     assert(0<=index<=getSize());
     _setCurrentIndex(index);

     Node *old = current;
     current->prev->next = current->next;
     current->next->prev = current->prev;
     delete old;
     size--;
    }

    void List::remove(const ElementType & item)
    {
     for(size_t i=0; i<size; i++)
      {
      _setCurrentIndex(i);
       if(find(item)<getSize())
        {
         Node *tempOld = current;
         current->next->prev = current->prev;
         current->prev->next = current->next;
         current = current->next;

         delete tempOld;
         size--;
        }
      }
    }

    size_t List::find(const ElementType & item) const
    {
     for(size_t i=0; i<size; i++)
      {
       _setCurrentIndex(i)
       if(get(i) == item)
        return i;
      }
     return getSize();
    }

    List::ElementType List::get(size_t index) const
    {
     assert(0 <= index < size);
     _setCurrentIndex(index);
     assert(current->next != NULL);
     return current->data;
    }

    size_t List::getSize() const
    {
     return size;
    }

    void List::output(std::ostream & ostr) const
    {
     for(size_t i=0; i<size; i++) 
      {
      _setCurrentIndex(i);
      ostr << current->data << " ";
      }
     ostr << endl;
    }

    void List:: _setCurrentIndex(size_t index) const
    {
     int x;
     if(currentIndex > index)
      x = currentIndex - index;
     else
      x = index-currentIndex;

     if(index < (sizez_t)x)
      {
      current = front->next;
      curentIndex=0;
      while(currentIndex != index)
       {
       current = current->next;
       currentIndex++;
       }
      }
     else if((size-index) < (size_t)x)
      {
       current = rear;
       currentIndex = size;
       while(currentIndex != index)
        {
         current = current->prev;
         currentIndex--;
        }
      }
     else
      {
       if(currentIndex > index)
        {
        while(currentIndex!=index)
         {
          current = current->prev;
          currentIndex--;
         }
        }
       else
        {
         while(currentIndex!=index)
          {
           current = current->next;
           currentIndex++;
          }
        }
      }
    }

似乎当我尝试从我的列表中添加或删除任何内容时,它会导致我的断言出现问题,并且它会中止或只是分段错误。

如果我把一个简单的声明

List history;
Pair p1 = Pair(1,1)
history.add(p1,0)

结果是:

scramble: list.cc:154: Pair List::get(size_t) const: assertion 0 <= index <size failed
aborted
4

1 回答 1

2

断言:

assert(0 <= index < size);

在不正确。改成:

assert(0 <= index && index < size);
于 2012-11-09T11:16:59.940 回答