Ошибка ошибки сегмента при копировании существующего указателя - PullRequest
0 голосов
/ 31 октября 2019

Это реализация связанного списка для проекта класса, и все это работает, кроме этой функции. Я сузил местоположение ошибки сегмента до указанной строки (обернуто в 'std :: cout ...' statments.

// Purpose: removes a section of a lists
// Parameters:    start pointer to the begining of the section to remove.
//                end pointer to the end of the section to remove.
// Preconditions: start and end are pointers to nodes in this list.
//                start precedes end in this list.
// Postconditions: elements between start and end (inclusive) are removed
//                 from the list.
template<typename T>
void LinkedList<T>::clip(LLNode<T>* start, LLNode<T>* stop)
{
  if(m_size > 2)
  {
    LLNode<T>* walker = m_head;
    LLNode<T>* hold;
    LLNode<T>* deleter;
    while(walker != start)
    {
      hold = walker;
      walker = walker -> m_next;
    }
    int i;
    //TODO: seg-fault here
    if(stop == NULL)
    {
      hold -> m_next = NULL;
    }
    else if(stop -> m_next == NULL)
    {
      hold -> m_next = NULL;
    }
    else
    {
      std::cout << "error on next line\n";
      hold -> m_next = stop -> m_next;
      std::cout << "oh... i guess it's fixed...\n";
    }
    if(hold -> m_next == NULL)
    {
      m_back = hold;
    }
    stop -> m_next = NULL;
    walker = start;
    while(walker != NULL && walker -> m_next != NULL)
    {
      hold = walker;
      walker = walker -> m_next;
      delete deleter;
      deleter = hold;
      i++;
    }
    delete walker;
    delete deleter;
    m_size -= i;
  }
  else
  {
    clear();
  }
}

Вывод получается так:

ошибка в следующей строке

Ошибка сегментации (ядро сброшено)

Любая помощь будет принята с благодарностью!

1 Ответ

0 голосов
/ 31 октября 2019

Очевидно, я упустил из виду, что край 'начала' равен голове.

Это полностью фиксированный код:

// Purpose: removes a section of a lists
// Parameters:    start pointer to the begining of the section to remove.
//                end pointer to the end of the section to remove.
// Preconditions: start and end are pointers to nodes in this list.
//                start precedes end in this list.
// Postconditions: elements between start and end (inclusive) are removed
//                 from the list.
template<typename T>
void LinkedList<T>::clip(LLNode<T>* start, LLNode<T>* stop)
{
  LLNode<T>* walker = m_head;
  LLNode<T>* deleter;
  LLNode<T>* lastNodeOnFront;
  if(m_head == start)
  {
    lastNodeOnFront = m_head;
  }
  while(walker != start)
  {
    lastNodeOnFront = walker;
    walker = walker -> m_next;
  }
  std::cout << *this;
  if(stop == m_back || stop == NULL)
  {
    lastNodeOnFront -> m_next = NULL;
  }
  else
  {
    lastNodeOnFront -> m_next = stop -> m_next;
  }
  std::cout << "before delete\n" << *this;
  walker = start;
  while(walker != lastNodeOnFront -> m_next)
  {
    deleter = walker;
    walker = walker -> m_next;
    delete deleter;
    m_size--;
  }
  return;
}

Спасибо Аконкагуа за помощь!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...