-2

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;
Noobie
  • 3
  • 4
  • 3
    You have a weird mix of C and C++ in your code. I suggest you take some C++ classes, or at the very least get [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Jan 10 '20 at 07:50
  • 2
    Also [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Jan 10 '20 at 07:50
  • 3
    `#include` - don't *ever* do that. Don't use `NULL` in new code, use `nullptr`. Don't allocate `C++` objects with `malloc`,, use `new` (or better yet, smart pointers). – Jesper Juhl Jan 10 '20 at 07:51

2 Answers2

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;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

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) {}
};
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60