简体   繁体   English

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

[英]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. 我一直在使用这个基本的linkedlist.cpp文件遇到麻烦。 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). 主文件是作为目标文件提供的,所以我看不到它,由于某种原因,我只考虑测试insert方法(我的睡眠和挣扎状况极低)。 Anyways I'm guessing somewhere in the insert method I'm referencing NULL wrong, but I can't figure out where. 无论如何,我猜在insert方法的某个地方我错误地引用了NULL,但是我不知道在哪里。

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. 在我标记为1的那一行中,您取消引用current->next而不检查它是否为NULL Also, at the line I marked 2, you end exectution, and then delete the pointer. 同样,在我标记为2的行上,您结束执行, 然后删除指针。 You should delete before you return . 您应该先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;
 }  

At the line marked 3, you dereference current only if it's NULL . 在标记为3的行中,仅当current NULL时才取消引用。 Bad juju. 坏枣 At the line marked 4, you dereference current->next without checking if it's NULL first. 在标记为4的行中,取消引用current->next而不先检查它是否为NULL I think you meant to set addition->next to current->next anyway. 我认为您无论如何都要将“ addition->next设置为“ current->next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 努力将整数转换为链表。 不确定我在这里做错了什么 - Struggling to convert an integer into a linked list. Not sure what I'm doing wrong here 滥用指针会产生分段错误。 我仍然不确定如何纠正它 - Misuse of a pointer is producing a segmentation fault. I'm still not sure how to correct it though 尝试将节点添加到 C++ 中的链表末尾时出现分段错误(核心转储)错误 - I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++ 我无法找到细分错误? - I'm having trouble finding the segmentation fault? “错误:没有用于cin的运算符&gt;&gt;”我无法弄清楚我在做什么错 - “Error: no operator for cin>>” I can't figure out what I'm doing wrong here 使用类的链表中的分段错误。 我的方法正确吗? - Segmentation fault in a linked list using classes. Am I doing the method correctly? 调试链表指针代码:segmentation fault - Debugging linked list pointers code: segmentation fault C++ 代码可以编译,但在运行时出现分段错误。 我想我正在修改链表 - C++ Code compiles but I get a segmentation fault when running it. I think I'm mangling linked lists 我在这个结构上做错了什么? - What I'm doing wrong with this structure? 当然我正在做一些非常错误的提升:replace_all。 请进行理智检查? - Sure I'm doing something very wrong with boost:replace_all. Sanity check please?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM