![](/img/trans.png)
[英]Struggling to convert an integer into a linked list. Not sure what I'm doing wrong here
[英]linked list segmentation fault. I'm guessing I'm doing something wrong with pointers here
編輯:這是更新的插入代碼。 我會嘗試斷言,但是看看我是否仍在犯錯
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文件遇到麻煩。 我猜想我可能使用了錯誤的指針或類似的東西。 主文件是作為目標文件提供的,所以我看不到它,由於某種原因,我只考慮測試insert方法(我的睡眠和掙扎狀況極低)。 無論如何,我猜在insert方法的某個地方我錯誤地引用了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;
}
}
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的行上,您結束執行, 然后刪除指針。 您應該先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
時才取消引用。 壞棗 在標記為4的行中,取消引用current->next
而不先檢查它是否為NULL
。 我認為您無論如何都要將“ addition->next
設置為“ current->next
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.