(C++) 使用 Nodes/Linked 列表时得到不一致的结果。
(C++) Getting inconsistent results while using Nodes/Linked Lists.
#include <iostream>
#include <memory>
using namespace std;
struct Node
{
int data;
Node* next;
};
void append(Node*&, int);
void printList(Node*);
void insertNode(Node*&, int, int);
void searchList(Node*, int, int);
int main()
{
Node* head = nullptr;
int initialCount = -1, userInput, newNodeLoc = -1, newNodeVal, searchVal;
/// INITIALIZE LIST
while(initialCount <= 0)
{
cout<<"Enter the number of initial nodes (must be at least 1): ";
cin>>initialCount;
}
cout<<endl;
for(int i = 0;i<initialCount;i++)
{
cout<<"Enter a number: ";
cin>>userInput;
append(head,userInput);
}
cout<<endl;
cout<<"Here are the initial values in the linked list: "<<endl;
printList(head);
cout<<"\nEnter a number for a new node to insert to the linked list: ";
cin>>newNodeVal;
cout<<endl;
cout<<"Enter which position you want to insert it (ex. "<<head->data<<" is at pos 1): ";
cin>>newNodeLoc;
while((newNodeLoc<=0 || newNodeLoc>initialCount))
{
cout<<"New position must be greater than 1 and less than " << initialCount+1 <<": ";
cin>>newNodeLoc;
}
newNodeLoc--;
insertNode(head, newNodeVal, newNodeLoc);
cout<<"\nHere is the updated linked list: "<<endl;
printList(head);
/// SEARCH
cout<<"\nEnter the number that you want to search for in the list: ";
cin>>searchVal;
cout<<endl;
initialCount++;
cout<<initialCount;
searchList(head,searchVal,initialCount);
return 0;
}
void printList(Node* head)
{
Node *n = head;
cout<<n->data<<endl;
while(n->next != nullptr) // print out all nodes values'
{
cout << n->next->data<<endl;
n = n->next;
}
}
void append(Node*& head, int val)
{
Node* temp = new Node;
temp->data = val;
temp->next = nullptr;
Node* ptr = head;
if(head == nullptr) // check if list is empty
{
head = temp;
}
else
{
while(ptr->next != nullptr) // if list isn't empty, get to last element set it equal to temp
{
ptr = ptr->next;
}
if(ptr->next == nullptr)
{
ptr->next = temp;
}
}
delete temp;
temp = nullptr;
}
void insertNode(Node*& head, int val, int loc)
{
Node* temp = new Node;
Node* prevLoc = new Node;
Node* curr = head;
temp->data = val;
int tempPos = 0;
while(curr->next != nullptr && tempPos != loc)
{
prevLoc = curr;
curr = curr->next;
tempPos++;
}
prevLoc->next = temp;
temp->next = curr;
delete temp;
delete prevLoc;
curr = nullptr;
prevLoc = nullptr;
}
void searchList(Node* head, int sVal, int iCount)
{
Node* curr = head;
int index=0;
while(curr->next != nullptr && curr->next->data != sVal)
{
curr = curr->next;
index++;
}
cout<<index;
cout<<iCount;
if(index != iCount)
{
cout<<"Number found at index "<<index<<" in the linked list!";
}
if(index-1 == iCount)
cout<<"Number could not be found in this linked list.";
delete curr;
curr = nullptr;
}
您好!我正在尝试实现 append/prntlist/insertnode/search 函数,但我得到的编译结果极其不一致。有时代码 运行 没问题。有时代码会随机中断。其他时候它会无限循环地打印出数字。我认为这是某处的内存泄漏(在 append/print 函数中),但我不是很有信心。它也可能是一个破坏代码的循环。任何帮助表示赞赏! 我知道搜索功能不起作用,所以你可以忽略它。谢谢!
在你的 append()
:
delete temp;
temp
是您刚刚 append
() 添加到列表中的新元素。现在它是列表的一部分,它会立即得到 delete
d。再见!
linked 列表中最后一个元素的旁边现在指向已删除的内存。随后尝试遍历列表会导致未定义的行为。随后尝试向列表中添加更多元素只会让事情变得更糟,在 delete
ed 内存上涂鸦(如果它没有被涂鸦,在下一个 new
之前)并且通常会造成严重混乱一切。
insert
() 中的内存分配逻辑同样存在缺陷。更糟的是。两个 new
和两个 delete
。此外,整体逻辑也是错误的。它表面上的目的是向列表中再插入一个元素,因此它不应该分配两个新节点,而只能分配一个。 append() 和 insert() 都向列表中添加一个节点;所以在这两种情况下只需要 new
ed 一个节点。
但总体问题是 new
ed 元素的错误删除,当它们不应该被 new
ed 时,它们会继续被使用。获得 delete
d 后,您将无法使用任何东西。它消失了。它不复存在了。它加入了合唱团——隐形的。这是一个前对象。但是显示的代码在添加到 link 列表后错误地 delete
是一个元素,表面上看,它在逻辑上仍然是 link 列表的一部分。
#include <iostream>
#include <memory>
using namespace std;
struct Node
{
int data;
Node* next;
};
void append(Node*&, int);
void printList(Node*);
void insertNode(Node*&, int, int);
void searchList(Node*, int, int);
int main()
{
Node* head = nullptr;
int initialCount = -1, userInput, newNodeLoc = -1, newNodeVal, searchVal;
/// INITIALIZE LIST
while(initialCount <= 0)
{
cout<<"Enter the number of initial nodes (must be at least 1): ";
cin>>initialCount;
}
cout<<endl;
for(int i = 0;i<initialCount;i++)
{
cout<<"Enter a number: ";
cin>>userInput;
append(head,userInput);
}
cout<<endl;
cout<<"Here are the initial values in the linked list: "<<endl;
printList(head);
cout<<"\nEnter a number for a new node to insert to the linked list: ";
cin>>newNodeVal;
cout<<endl;
cout<<"Enter which position you want to insert it (ex. "<<head->data<<" is at pos 1): ";
cin>>newNodeLoc;
while((newNodeLoc<=0 || newNodeLoc>initialCount))
{
cout<<"New position must be greater than 1 and less than " << initialCount+1 <<": ";
cin>>newNodeLoc;
}
newNodeLoc--;
insertNode(head, newNodeVal, newNodeLoc);
cout<<"\nHere is the updated linked list: "<<endl;
printList(head);
/// SEARCH
cout<<"\nEnter the number that you want to search for in the list: ";
cin>>searchVal;
cout<<endl;
initialCount++;
cout<<initialCount;
searchList(head,searchVal,initialCount);
return 0;
}
void printList(Node* head)
{
Node *n = head;
cout<<n->data<<endl;
while(n->next != nullptr) // print out all nodes values'
{
cout << n->next->data<<endl;
n = n->next;
}
}
void append(Node*& head, int val)
{
Node* temp = new Node;
temp->data = val;
temp->next = nullptr;
Node* ptr = head;
if(head == nullptr) // check if list is empty
{
head = temp;
}
else
{
while(ptr->next != nullptr) // if list isn't empty, get to last element set it equal to temp
{
ptr = ptr->next;
}
if(ptr->next == nullptr)
{
ptr->next = temp;
}
}
delete temp;
temp = nullptr;
}
void insertNode(Node*& head, int val, int loc)
{
Node* temp = new Node;
Node* prevLoc = new Node;
Node* curr = head;
temp->data = val;
int tempPos = 0;
while(curr->next != nullptr && tempPos != loc)
{
prevLoc = curr;
curr = curr->next;
tempPos++;
}
prevLoc->next = temp;
temp->next = curr;
delete temp;
delete prevLoc;
curr = nullptr;
prevLoc = nullptr;
}
void searchList(Node* head, int sVal, int iCount)
{
Node* curr = head;
int index=0;
while(curr->next != nullptr && curr->next->data != sVal)
{
curr = curr->next;
index++;
}
cout<<index;
cout<<iCount;
if(index != iCount)
{
cout<<"Number found at index "<<index<<" in the linked list!";
}
if(index-1 == iCount)
cout<<"Number could not be found in this linked list.";
delete curr;
curr = nullptr;
}
您好!我正在尝试实现 append/prntlist/insertnode/search 函数,但我得到的编译结果极其不一致。有时代码 运行 没问题。有时代码会随机中断。其他时候它会无限循环地打印出数字。我认为这是某处的内存泄漏(在 append/print 函数中),但我不是很有信心。它也可能是一个破坏代码的循环。任何帮助表示赞赏! 我知道搜索功能不起作用,所以你可以忽略它。谢谢!
在你的 append()
:
delete temp;
temp
是您刚刚 append
() 添加到列表中的新元素。现在它是列表的一部分,它会立即得到 delete
d。再见!
linked 列表中最后一个元素的旁边现在指向已删除的内存。随后尝试遍历列表会导致未定义的行为。随后尝试向列表中添加更多元素只会让事情变得更糟,在 delete
ed 内存上涂鸦(如果它没有被涂鸦,在下一个 new
之前)并且通常会造成严重混乱一切。
insert
() 中的内存分配逻辑同样存在缺陷。更糟的是。两个 new
和两个 delete
。此外,整体逻辑也是错误的。它表面上的目的是向列表中再插入一个元素,因此它不应该分配两个新节点,而只能分配一个。 append() 和 insert() 都向列表中添加一个节点;所以在这两种情况下只需要 new
ed 一个节点。
但总体问题是 new
ed 元素的错误删除,当它们不应该被 new
ed 时,它们会继续被使用。获得 delete
d 后,您将无法使用任何东西。它消失了。它不复存在了。它加入了合唱团——隐形的。这是一个前对象。但是显示的代码在添加到 link 列表后错误地 delete
是一个元素,表面上看,它在逻辑上仍然是 link 列表的一部分。