我的代码有什么问题?输出不显示
What is wrong with my code? The output doesn't show
我是编程新手,所以我被这段代码卡住了。我试图实现一个链表,并应用在评论中提到的功能。当我调试程序时,它没有显示任何错误,但是当我运行代码时,它说"控制台应用程序停止工作……"我不知道这是怎么回事。为什么它不起作用?
#include<iostream>
#include<conio.h>
using namespace std;
/*• InsertNodeAtFront()
• InsertNodeAtBack()
• PrintList()
*/
class list {
struct node {
int data;
node* next;
};
node *head;
node *current;
public:
list() {
head = NULL;
current = NULL;
}
void InsrtAtFront(int n) {
node *temp = new node;
temp->data = n;
temp->next = NULL;
if (head == NULL) {
temp = head;
}
else {
temp->next = head;
head = temp;
}
}
void InsrtAtBack(int a) {
node *temp1 = new node;
temp1->data = a;
temp1->next = NULL;
if (head == NULL) {
temp1 = head;
}
else {
current = head;
while (current->next!=NULL){
current = current->next;
}
current = temp1;
}
}
void print() {
current = head;
while (current->next != NULL) {
cout << current->data;
current = current->next;
}
}
};
void main() {
list obj1, obj2, obj3;
obj3.print();
obj1.InsrtAtFront(5);
obj1.InsrtAtFront(7);
obj1.InsrtAtFront(9);
obj3.print();
obj2.InsrtAtBack(2);
obj2.InsrtAtBack(4);
obj2.InsrtAtBack(6);
obj3.print();
}
list() {
head = NULL;
current = NULL;
}
void print() {
current = head;
while (current->next != NULL) {
cout << current->data;
current = current->next;
}
}
//in main
list obj1, obj2, obj3;
obj3.print();
。你用NULL初始化head然后试图使用while on current那是head那是NULL ..
相关文章:
- 了解 Linux 虚拟内存:valgrind 的 massif 输出显示了有和没有 --pages-as-heap 的主要差异
- 输出显示多个带整数的字符串值
- 如何使代码的输出显示为"工资减税"
- 如何让我的输出显示输入金额的星号
- 控制台输出显示ASCII值,而不是数字的预期数字
- 如何修复输出显示,以便显示正确排序的数据
- Android Studio 3.2:cmake message()输出显示在哪里
- 如何将OpenCV的输出显示或流式传输到HTML页面或其他一些客户端应用程序
- 我写了一个函数来删除循环链接列表中的第 1 个节点,但输出显示无限次"55 44 33 22 11 99",那么我该如何解决呢?
- 从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
- 为什么我的输出显示显示计算器中的每个选项
- C++将输出显示到php中
- 从 PHP 调用 C++ 应用程序 - 输出显示"Array"
- 有没有办法C++让循环的输出显示在同一行上
- 为什么日志输出显示在控制台主机中,而不显示在ISE中?(v4.0)
- 如何让GDB为每一行输出显示时间戳
- 为什么输出显示为无符号数
- 如何获得gdb tui汇编输出显示指令
- 为什么输出显示错误
- FFTW输入输出显示DSP