所以我试图在链表中删除一个迭代。我需要能够为该方法提供一个整数,该整数应该是要删除的项目的位置。
例如,如果我的链接列表为{1、3、5、7、9},而我输入RemoveValue(2),则我的新列表应为{1、5、7、9}。
我运行程序,并不断遇到分段错误。我试图查找答案无济于事。我以为可能超出范围,但我不知道何时不越过尾巴。我指的是RemoveValue方法。我在下面将其突出显示。我列出了我正在使用的其他cpp文件,但没有列出标头,我认为它们是不需要的。
linkedlist.cpp
#include "linked_list.hpp"
#include <iostream>
template<typename T>
LinkedList<T>::LinkedList()
{
this->head = nullptr;
this->tail = nullptr;
this->size = 0;
}
template<class T>
void LinkedList<T>::InsertAtHead(T value)
{
Node<T>* newNode = new Node<T>(value);
newNode->SetNext(this->head);
this->head = newNode;
if(this->tail == nullptr)
{
this->tail = newNode;
}
this->size++;
}
template<class T>
void LinkedList<T>::Print()
{
Node<T>* currentNode = this->head;
while(currentNode != nullptr)
{
std::cout << currentNode->GetValue() << std::endl;
currentNode = currentNode->GetNext();
}
}
template<class T>
void LinkedList<T>::ReturnValue(int num){
Node<T>* currentNode = this->head;
int searchNum = num;
for (int x = 0; x <= searchNum ; x++){
if (searchNum == x)
std::cout<<currentNode->GetValue();
currentNode = currentNode -> GetNext();
}
}
template<class T>
template<class T> void LinkedList<T>::RemovePosition(int num){ Node<T>* previousNode = this-> head; Node<T>* currentNode = head-> GetNext(); int searchPosition = num - 1; if ((this->head = nullptr) || (this->tail = nullptr)) return; else { for (int x = 0; x <= searchNum ; x++){ if (searchPosition == x){ previousNode -> SetNext(currentNode-> GetNext()); size--; delete currentNode; break; } currentNode = currentNode -> GetNext(); } } }
template<class T>
void LinkedList<T>::Append(T value){
Node<T>* newNode = new Node<T>(value);
if (this-> head == nullptr) {
this-> head = newNode;
this -> tail = newNode;
}
else {
this -> tail -> SetNext(newNode);
}
size++;
}
template<class T>
void LinkedList<T>::InsertAt(int position, T value)
{
if(position < 0 || position > size)
{
return;
}
if(position == 0)
{
InsertAtHead(value);
return;
}
if(position == this->size)
{
//Append
}
Node<T>* newNode = new Node<T>(value);
int currentPosition = 0;
Node<T>* currentNode = this->head;
while(currentPosition != position - 1)
{
currentPosition++;
currentNode = currentNode->GetNext();
}
newNode->SetNext(currentNode->GetNext());
currentNode->SetNext(newNode);
this->size++;
}
template class LinkedList<int>;
node.cpp
#include "node.hpp"
#include <string>
template<typename T>
Node<T>::Node()
{
this->value = 0;
this->next = nullptr;
}
template<typename T>
Node<T>::Node(T value)
{
this->value = value;
this->next = nullptr;
}
template<typename T>
T Node<T>::GetValue()
{
return this->value;
}
template<typename T>
void Node<T>::SetNext(Node* next)
{
this->next = next;
}
template<typename T>
Node<T>* Node<T>::GetNext()
{
return this->next;
}
template class Node<int>;
template class Node<double>;
main.cpp
#include <iostream>
#include <string>
#include "node.hpp"
#include "linked_list.hpp"
int main()
{
Node<int>* newNode = new Node<int>(12);
Node<int>* nextNode = new Node<int>(24);
if(12 == newNode->GetValue())
std::cout << "GetValue passed" << std::endl;
newNode->SetNext(nextNode);
if(24 == newNode->GetNext()->GetValue())
std::cout << "GetNext passed" << std::endl;
Node<double>* doubleNode = new Node<double>(3.678);
if((3.678 - doubleNode->GetValue() < .001) && (3.678 - doubleNode->GetValue() > -.001))
std::cout << "Double worked" << std::endl;
LinkedList<int>* linkedList = new LinkedList<int>();
linkedList->InsertAtHead(5);
linkedList->InsertAtHead(3);
linkedList->InsertAtHead(1);
linkedList->Append(8);
linkedList->Print();
linkedList->RemovePosition(3);
linkedList->InsertAt(2, 4);
linkedList->Print();
linkedList->ReturnValue(2);
return 0;
}
答案 0 :(得分:1)
您的代码中有一些错误,主要是您没有更新previousNode
的值。您在方法开始时将其设置为head
,然后再也不会碰它。
这就是我的处理方法(包括给所有明智的名称起名)
template<class T>
void LinkedList<T>::RemoveAt(int pos) {
Node<T>* prev = nullptr;
Node<T>* curr = head;
for (int i = 1; i < pos && curr != nullptr; ++i) {
prev = curr; // set prev to old value of current
curr = curr->GetNext(); // and move current forward
}
if (curr == nullptr) { // nothing to delete?
return; // then do nothing
}
else if (prev == null) { // no previous pointer?
head = curr->GetNext(); // then remove the head
}
else {
prev->SetNext(curr->GetNext());
}
if (tail == curr) // are we deleteing the tail?
tail = prev; // then update
delete curr;
}
完全未经测试的代码,因此我不保证不会出错。但是重要的一点是,我在循环中更新了prev
变量。