为什么我的代码块上没有显示输出?
Why there is no output showing on my codeblocks?
我正在尝试在 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
Output file is bin\Debug\Delete duplicate-value in Linked List.exe with size 1.51 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
-------------- Run: Debug in Delete duplicate-value in Linked List (compiler: GNU GCC Compiler)---------------
Checking for existence: C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List\bin\Debug\Delete duplicate-value in Linked List.exe
Executing: "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 List.exe" (in C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List.)
Process terminated with status -1073741510 (0 minute(s), 10 second(s))
#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;
问题好像是这个:
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
在 pushinorder
函数中,这意味着 *head
将 不是 是空指针,而且 *head
的成员 (因此 temp
) 将具有 indeterminate (并且看似随机或垃圾)值。使用 *head
的未初始化成员将导致 undefined behavior.
简单的解决方案是将 head
初始化为空指针:
Node *head = nullptr;
您的代码可能已挂起,-1073741510 或 0xc000013a 的 return 代码表示您已按 ctrl-c 终止应用程序。
head->next
未初始化。您应该使用 new
而不是 malloc
并向 Node
添加一个构造函数,它将 next
初始化为 null:
struct Node
{
int data;
Node *next;
Node(): next(nullptr) {}
};
我正在尝试在 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
Output file is bin\Debug\Delete duplicate-value in Linked List.exe with size 1.51 MB Process terminated with status 0 (0 minute(s), 1 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 1 second(s))-------------- Run: Debug in Delete duplicate-value in Linked List (compiler: GNU GCC Compiler)---------------
Checking for existence: C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List\bin\Debug\Delete duplicate-value in Linked List.exe Executing: "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 List.exe" (in C:\Users\hp\Desktop\CPP Programming\Delete duplicate-value in Linked List.) Process terminated with status -1073741510 (0 minute(s), 10 second(s))
#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;
问题好像是这个:
struct Node *head = (struct Node*)malloc(sizeof(struct Node));
在 pushinorder
函数中,这意味着 *head
将 不是 是空指针,而且 *head
的成员 (因此 temp
) 将具有 indeterminate (并且看似随机或垃圾)值。使用 *head
的未初始化成员将导致 undefined behavior.
简单的解决方案是将 head
初始化为空指针:
Node *head = nullptr;
您的代码可能已挂起,-1073741510 或 0xc000013a 的 return 代码表示您已按 ctrl-c 终止应用程序。
head->next
未初始化。您应该使用 new
而不是 malloc
并向 Node
添加一个构造函数,它将 next
初始化为 null:
struct Node
{
int data;
Node *next;
Node(): next(nullptr) {}
};