【发布时间】: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<bits/stdc++.h>- 不要永远那样做。不要在新代码中使用NULL,使用nullptr。不要用malloc分配C++对象,使用new(或者更好的是,智能指针)。
标签: c++ c data-structures codeblocks singly-linked-list