所以我整天都在研究这个程序,所以我可能会精疲力尽,但是对于我一生来说,我无法弄清楚为什么我的显示功能一旦完成第一次打印就会终止该程序。我确实需要能够在每个输入循环上打印整个列表。我尝试通过将其包装在循环中并添加打印语句进行测试来进行调试,以使整个程序运行正常,除了在打印列表后该程序终止的事实。
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node()
{
next = 0;
}
Node(int data)
{
this->data = data;
}
};
class list
{
private:
int Count = 0;
Node *start;
public:
list()
{
start = 0;
}
void display()
{
Node *temp = new Node;
temp = start;
while (temp != 0)
{
cout << temp->data << " ";
temp = temp->next;
}
}
bool insert(int value)
{
Node *newNode = new Node(value);
Node *temp = new Node;
Node *cur = new Node;
Node *pre = new Node;
int track = 0;
cur = start;
if (start == 0)
{
start = newNode;
Count++;
}
else if (value < cur->data)
{
temp->data = value;
temp->next = start;
start = temp;
Count++;
}
else
for (int i = 0; i < Count; i++)
{
if (value > cur->data)
{
pre = cur;
cur = cur->next;
track++;
}
}
if (track > 0)
{
temp->data = value;
pre->next = temp;
temp->next = cur;
Count++;
}
return true;
}
};
int main(void)
{
int input = 0;
list obj;
while (input != -1)
{
cout << "Enter a value: ";
cin >> input;
if (input != -1)
obj.insert(input);
obj.display();
}
}
;