【问题标题】:Why there is no output showing on my codeblocks?为什么我的代码块上没有显示输出?
【发布时间】:2020-01-10 07:44:45
【问题描述】:

我正在尝试在 code::blocks 17.12 上实现一个链表(在节点末尾),但没有显示任何输出。此代码在我的日志中显示黑色输出屏幕,并显示以下消息:

-------------- Build: Debug in Delete duplicate-value in Linked List (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -c "C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List\main.cpp" -o obj\Debug\main.o mingw32-g++.exe -o "bin\Debug\Delete duplicate-value in Linked List.exe" obj\Debug\main.o
输出文件是 Linked List.exe 中的 bin\Debug\Delete duplicate-value,大小为 1.51 MB 进程以状态 0 终止(0 分钟,1 秒) 0 个错误,0 个警告(0 分钟,1 秒)

------------- 运行:Debug in Delete duplicate-value in Linked List(编译器:GNU GCC Compiler)---------------

检查是否存在:C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List\bin\Debug\Delete duplicate-value in Linked List.exe 执行:“C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe”“C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List\bin\Debug\Delete duplicate-value in Linked-value .exe”(在 C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List 中。) 进程终止,状态为 -1073741510(0 分钟,10 秒)

#include <iostream>
#include<stdlib.h>
#include<bits/stdc++.h>
#include<conio.h>

using namespace std;

struct Node
{
    int data;
    Node *next;
};
void pushinorder(struct Node** head, int new_data)
{
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    struct Node *temp = *head;
    new_node->data = new_data;
    new_node->next = NULL;
    if(*head==NULL)
    {
        *head = new_node;
        return;
    }
    while(temp->next!=NULL)
    {
        temp = temp->next;
    }
    temp->next = new_node;
    return;
}

void PrintList(struct Node* head)
{
    struct Node *temp = head;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->next;
    }

}

int main()
{
struct Node *head = (struct Node*)malloc(sizeof(struct Node));

pushinorder(&head,1);
pushinorder(&head,1);
pushinorder(&head,1);
pushinorder(&head,2);
pushinorder(&head,2);
pushinorder(&head,3);
pushinorder(&head,3);
pushinorder(&head,3);
pushinorder(&head,3);
pushinorder(&head,4);
PrintList(head);
getch();
return 0;

【问题讨论】:

  • 您的代码中有一种奇怪的 C 和 C++ 组合。我建议你参加一些 C++ 课程,或者至少让 a few good books 阅读。
  • #include&lt;bits/stdc++.h&gt; - 不要永远那样做。不要在新代码中使用NULL,使用nullptr。不要用malloc分配C++对象,使用new(或者更好的是,智能指针)。

标签: c++ c data-structures codeblocks singly-linked-list


【解决方案1】:

问题似乎是这个东西:

struct Node *head = (struct Node*)malloc(sizeof(struct Node));

pushinorder 函数中,这意味着*head为空指针,并且*head 的成员(因此temp)将具有不确定的(看似随机或垃圾)的值。使用*head 的未初始化成员将导致undefined behavior

简单的解决方案是将head初始化为空指针:

Node *head = nullptr;

【讨论】:

    【解决方案2】:

    您的代码可能挂起,返回码 -1073741510 或 0xc000013a 表示您已按 ctrl-c 终止应用程序。

    head-&gt;next 未初始化。您应该使用new 而不是malloc 并向Node 添加一个构造函数,它将next 初始化为null:

    struct Node
    {
        int data;
        Node *next;
        Node(): next(nullptr) {}
    };
    

    【讨论】:

      猜你喜欢
      • 2015-07-03
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多