简体   繁体   中英

linked list segmentation fault. I'm guessing I'm doing something wrong with pointers here

EDIT: Heres the updated insert code. I'll try asserting it, but see if I'm still making a mistake

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;    
}

I've been having some trouble with this basic linkedlist.cpp file I've been working on. I'm guessing that I'm probably using the pointers wrong or something along those lines. The main file is given as an object file so I can't look at it and for some reason I only thought to test the insert method(I'm extremely low on sleep and struggling). Anyways I'm guessing somewhere in the insert method I'm referencing NULL wrong, but I can't figure out where.

ERROR: Segmentation fault(core dumped) NOTE: this error occured after running the insert

#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;              
          }           
}
   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;     
            }

At the line I marked 1, you dereference current->next without ever checking if it is NULL or not. Also, at the line I marked 2, you end exectution, and then delete the pointer. You should delete before you 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;
 }  

At the line marked 3, you dereference current only if it's NULL . Bad juju. At the line marked 4, you dereference current->next without checking if it's NULL first. I think you meant to set addition->next to current->next anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM