链表分段错误。我猜这里的指针做错了

时间:2011-10-28 17:28:07

标签: c++ pointers null segmentation-fault

编辑:继承更新的插入代码。我会尝试断言,但看看我是否仍然犯了错误

bool SortedList::insert(Student *s){    
     bool inList = false;    
     // create an iterator for the list    
     ListNode *current = head;    
     // create a new ListNode for the student to be added    
     ListNode *addition = new ListNode();    
     // initialize addition to the student and the next to NULL    
     addition->student = s;    
     addition->next = NULL;    
            //while current is not at the end of the list    
            while(current != NULL){    
                    // if the iteration's ID is equal to the given ID return     
                    // false and delete the ListNode addition    
                    if(current->student->getID() == addition->student->getID()){    
                            delete addition;    
                            return false;    
                    // else if the next student ID in the list is greater than     
                    // the given ID break the while loop     
                    }else if(current->next != NULL && current->next->student->getID() > addition->student->getID()){   
                             inList = true;    
                             break;     
                    }    
                    // otherwise set current to the next student in the list     
                    current = current->next;                  
            }    
     // if current is at the end of the list and student wasn't found, set    
     // current next to addition           
     if(!inList){    
           current->next = addition;    
     // else set addition next to current next next and current next to addition    
     }else{              
           addition->next = current->next;    
           current->next = addition;    
     }        
     // return true regardless as the student has been added    
     return true;    
}

我在使用这个基本的linkedlist.cpp文件时遇到了一些麻烦。我猜我可能正在使用指针错误或类似的东西。主文件作为目标文件给出,所以我无法查看它,由于某种原因我只想测试插入方法(我睡眠和挣扎极低)。无论如何我在插入方法的某处猜测我引用了NULL错误,但我无法弄清楚在哪里。

错误:分段错误(核心转储) 注意:运行插入

后发生此错误
#include <iostream>
#include "SortedList.h"

using namespace std;

/**
  * zero argument constructor - initializes an empty list
  */
SortedList::SortedList() : head(NULL){}

/**
  * If a student with the same ID is not already in the list, inserts 
  * the given student into the list in the appropriate place and returns
  * true.  If there is already a student in the list with the same ID
  * then the list is not changed and false is returned.
  *
  * @param *s a given pointer to a student
  * @return boolean value based on whether the student was inserted or not
  */
bool SortedList::insert(Student *s){
 // create an iterator for the list
 ListNode *current = head;
 // create a new ListNode for the student to be added
 ListNode *addition = new ListNode();
 // initialize addition to the student and the next to NULL
 addition->student = s;
 addition->next = NULL;
        //while current is not at the end of the list
        while(current != NULL){
                // if the iteration's ID is equal to the given ID return 
                // false and delete the ListNode addition
                if(current->student->getID() == addition->getID()){
                        return false;
                        delete addition;
                // else if the next student ID in the list is greater than 
                // the given ID break the while loop                        
                }else if(current->next->student->getID() > addition->getID()){
                         break;     
                }
                // otherwise set current to the next student in the list 
                current = current->next;              
        }
 // if current is at the end of the list and student wasn't found, set
 // current next to addition       
 if(current == NULL){
       current->next = addition;
 // else set addition next to current next next and current next to addition
 }else{          
 addition->next = current->next->next;
 current->next = addition;
 }    
 // return true regardless as the student has been added
 return true;
}

/**
  * Searches the list for a student with the given student ID.  If the
  * student is found, it is returned; if it is not found, NULL is returned.
  *
  * @param studentID the given studentID to find in the list
  * @return a pointer to a the student found or NULL if the student isn't found
  */
Student * SortedList::find(int studentID){
    // create iterator for the list
    ListNode *current = head;
    // while not at the end of the list iterate
    while(current != NULL){
           // if the current student ID equals the given student ID return
           // the student       
           if(current->student->getID() == studentID){
                    return current->student;
           }
           // otherwise continue iterating
           current = current->next;
    }
    // if not found then return NULL
    return NULL;                   
}

/**
  * Searches the list for a student with the given student ID.  If the 
  * student is found, the student is removed from the list and returned;
  * if no student is found with the given ID, NULL is returned.
  *
  * @param studentID the given student ID to be removed from the list
  * @return a pointer to the student that was removed or NULL if the student
  * wasn't found
  */
Student * SortedList::remove(int studentID){
    // create iterator for the list
    ListNode *current = head;
    // create the to hold the value ahead of the iterator
    ListNode *currentNext;
    // create to hold the removed student
    Student *remove;
    // while current is not at the end of the list iterate
    while(current != NULL){
                  // set currentNext to the value ahead of iterator
                  currentNext = current->next;
                  // if its ID equals the given ID
                  if(currentNext->student->getID() == studentID){
                       // set remove to the student removing
                       remove = currentNext->student;
                       // set current next to currentNext next
                       // (current next next)
                       current->next = currentNext->next;
                       //delete the removed ListNode
                       delete currentNext;
                       //return the removed student
                       return remove;
                  }
    }
    //if the student wasn't found return NULL
    return NULL;
}

/**
  * Prints out the list of students to standard output.  The students are
  * printed in order of student ID (from smallest to largest), one per line
  */
 void SortedList::print() const{
  //create iterator for list
 ListNode *current = head;
          //while current is not at the end of the list iterate
          while(current != NULL){
                  // print each individual student and end line      
                  current->student->print();
                  cout << endl;
                  //iterate the list
                  current = current->next;              
          }           
}

1 个答案:

答案 0 :(得分:1)

   while(current != NULL){
            // if the iteration's ID is equal to the given ID return 
            // false and delete the ListNode addition
            if(current->student->getID() == addition->getID()){
   2                 return false;
                    delete addition;
            // else if the next student ID in the list is greater than 
            // the given ID break the while loop                        
   1         }else if(current->next->student->getID() > addition->getID()){
                     break;     
            }

在我标记为1的行中,您取消引用current->next,而不检查它是否为NULL。此外,在标记为2的行中,您结束exectution,然后然后删除指针。您应delete之前return

if(current == NULL){
3   current->next = addition;
 // else set addition next to current next next and current next to addition
 }else{          
4     addition->next = current->next->next;
     current->next = addition;
 }  

在标记为3的行中,只有在current时才取消引用NULL。坏juju。在标记为4的行中,您取消引用current->next,而不先检查它是NULL。我想你无论如何都要将addition->next设置为current->next