简体   繁体   中英

Why there is no output showing on my codeblocks?

I'm trying to implement a linked list (At the end of node) on code::blocks 17.12 but there isn't showing any output. This Code is showing me a black output screen with a following message in my logs:

-------------- 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;

The problem seems to be this thing:

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

Inside the pushinorder function this means that *head will not be a null pointer, and also that the members of *head (and therefore temp ) will have indeterminate (and seemingly random or garbage) values. Using the uninitialized members of *head will lead to undefined behavior .

The simple solution is to initialize head to be a null-pointer:

Node *head = nullptr;

Your code is presumably hanging, the return code of -1073741510 or 0xc000013a indicates you've pressed ctrl-c to terminate the application.

head->next is uninitialised. You should use new rather than malloc and add a constructor to Node which initialises next to null:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM