C++ 单链表无限循环
从我开始学习链表到现在已经一个星期了,我只学会了单链表。所以今天我实现了我在C++中学习的链表,当我试图运行它时,代码进入了一些随机数的无限循环。我试着调试代码,但我找不到代码有什么问题。代码如下。谢谢你的帮助,谢谢C++ 单链表无限循环,c++,linked-list,singly-linked-list,C++,Linked List,Singly Linked List,从我开始学习链表到现在已经一个星期了,我只学会了单链表。所以今天我实现了我在C++中学习的链表,当我试图运行它时,代码进入了一些随机数的无限循环。我试着调试代码,但我找不到代码有什么问题。代码如下。谢谢你的帮助,谢谢 #include <iostream> using namespace std; struct node{ int data; node * next; }; class singly{ private: node * head,*tail; public: sin
#include <iostream>
using namespace std;
struct node{
int data;
node * next;
};
class singly{
private:
node * head,*tail;
public:
singly(){
head=NULL;
tail=NULL;
}
void createNode(int value){
node * temp = new node;
temp->data=value;
temp->next=NULL;
if(head==NULL){
head=temp;
tail=temp;
temp=NULL;
}
else{
tail->next=temp;
tail=temp;
}
}
void display(){
node * temp = new node;
head=temp;
while(temp!=NULL){
cout << temp->data << "\t" << endl;
temp->next=temp;
}
}
void insert_end(int value){
node*newnode = new node;
node*temp = new node;
newnode->data=value;
newnode->next=NULL;
temp=head;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next=newnode;
}
void delete_node(){
node*current = new node;
node*previous = new node;
current = head;
while(current->next!=NULL){
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
};
int main(){
singly lists;
lists.createNode(32);
lists.createNode(654);
lists.createNode(34);
lists.createNode(234);
cout<<"\n--------------------------------------------------\n";
cout<<"---------------Displaying All nodes---------------";
cout<<"\n--------------------------------------------------\n";
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Inserting At End-----------------";
cout<<"\n--------------------------------------------------\n";
lists.createNode(55);
lists.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Deleing At End-------------------";
cout<<"\n--------------------------------------------------\n";
lists.delete_node();
lists.display();
}
首先,显示是错误的。您希望更新为temp=temp->next;它也可以初始化为node*temp=head,因此不需要第二行
您的删除节点可以重新写入:
if (head->next == NULL) // handles the case that it consists of 1 element
{
delete head;
head = NULL;
}
else
{
node *nextToEnd = head;
node *end = head->next;
while (end->next != NULL)
{
nextToEnd = end;
end = end->next;
}
delete end;
nextToEnd->next = NULL;
}
如评论中所述,审查
成员功能显示没有意义。
它用未初始化的新创建的临时值覆盖数据成员头
因此,函数调用未定义的行为
该函数可以如下所示
void display()
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
cout << temp->data << "\t";
}
}
void delete_node()
{
if ( head != nullptr )
{
tail = nullptr;
node *current = head;
while ( current->next )
{
tail = current;
current = current->next;
}
if ( tail == nullptr )
{
head = tail;
}
else
{
tail->next = nullptr;
}
delete current;
}
}
成员函数delete_node首先对单链表没有意义,再次是错误的,调用了未定义的行为。函数应从列表中删除第一个节点
然而,如果您想从列表中删除最后一个节点,那么函数可以如下所示
void display()
{
for ( node * temp = head; temp != nullptr; temp = temp->next )
{
cout << temp->data << "\t";
}
}
void delete_node()
{
if ( head != nullptr )
{
tail = nullptr;
node *current = head;
while ( current->next )
{
tail = current;
current = current->next;
}
if ( tail == nullptr )
{
head = tail;
}
else
{
tail->next = nullptr;
}
delete current;
}
}
它在哪一点进入无限循环?您不能指望我们为您调试整个代码。我建议您对新关键字的作用进行一些研究,因为从这些代码判断,您对它没有真正的了解显示和删除节点都已损坏。您应该只在将节点添加到列表的函数中写入新的。不使用new创建指针–node*temp=head;如果您想要一个与head值相同的指针,则只需这样做。顺便说一句,对于一个带有尾部指针的列表,insert_end是不必要的复杂,而且,在末尾插入是createNode所做的。@molbdnino感谢alotThank bro的帮助,我仍然是一个noob,所以仍处于学习阶段。但这对我帮助很大,解决了我的问题。
void delete_node()
{
if ( head != nullptr )
{
tail = nullptr;
node *current = head;
while ( current->next )
{
tail = current;
current = current->next;
}
if ( tail == nullptr )
{
head = tail;
}
else
{
tail->next = nullptr;
}
delete current;
}
}