如何在不使用 C++ 函数的情况下显示链表中的元素
How can I display elements in the linked list without using function in C++?
我想在链表中显示元素而不使用C++函数。我的代码没有出现在屏幕上的任何内容,即使它不包含任何错误。我不知道我该如何解决它?
#include <iostream>
using namespace std;
struct Element {
int data;
Element *next;
};
struct List {
int nb_ele;
Element *head;
Element *tail;
};
int main(){
Element *tmp;
tmp = new(Element);
tmp ->data = 5;
tmp ->next = NULL;
List *li;
li ->head = tmp;
li ->tail = tmp;
li ->nb_ele = 1;
tmp = new(Element);
tmp ->data = 7;
tmp ->next = li->head;
li ->head = tmp;
li ->nb_ele = li->nb_ele + 1;
Element *ptr;
ptr = li->head;
while (ptr != NULL){
cout<< ptr->data<<" ";
ptr = ptr->next;
}
}
你的代码的问题在于这一行:
List *li;
li 是一个未初始化的指针,当您稍后尝试访问它时,会导致问题。将此行更改为:
List *li = new List;
或者不要使用指针。 只需将其更改为:
List li;
相关文章:
- 如何在不将其转换为 ASCII 字符的情况下显示 QByteArray 的十六进制值
- 如果 return 语句在带括号的循环中,错误可能会到达非 void 函数的末尾,但不会显示在没有括号的循环的情况下
- 在这种情况下,GCC 和 clang 是否显示与 Visual Studio 相同的结果,关于语言链接?
- 如何在不显示媒体播放器的情况下在libVLC中拍摄快照
- 尝试创建一个读取.txt文件,显示它,计数唯一单词的程序,并在使用了多少次的情况下显示独特的单词.C
- 如何在不使用 C++ 函数的情况下显示链表中的元素
- 在不到一分钟的情况下,在一秒钟内显示计时器
- 在没有任何外部库的情况下,是否可以在c++/linux的窗口中显示bmp图像
- 在没有人工交互的情况下,在nginx上为每个cpp突出显示语法
- 如何在不显示文本的情况下分配Win32编辑控件的窗口名称
- 如何在不考虑会话的情况下获取显示设备信息
- QtC++编译是在没有显示程序的情况下完成的
- 如何在不显示十六进制的情况下读取dat文件中的文本?C++
- PDB无法显示发布版本崩溃的确切行号,但在二进制调试版本的情况下显示精确行号
- 如何在不舍入的情况下以C++显示固定位数
- 是否可以使用 opencv 的 imshow 在没有工具栏的情况下显示图像?
- 如何在Windows中不显示cmd窗口的情况下运行带参数的外部程序
- 如何在 c++ 中不将其显示为函数定义的情况下调用函数
- 如何使用c++在没有读线的情况下显示字符串中的多个单词
- 我们可以在不使用 c++ 中的任何库的情况下加载、显示和操作图像的矩阵吗?