My program runs but displays a message that it has stopped working correctly

Write your question here.
Can anyone help me with this message :
Head_insert.exe has stopped working.

A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  Put the code you need help with here.
//Adding a node to a Linked List
#include<iostream>
#include<cstdlib>

using namespace std;

struct Node
{
	int data;
	Node *link ;
};

typedef Node *NodePtr;     //the structure called "NODE" declares a pointer variable called NodePtr

//Function to Add a Node at the head of a Linked List
void head_insert(NodePtr&head, int the_number);
//Precondition: The pointer variable head points to
//the head of a linked list.
//Postcondition: a new node containg the_number
//has been added at the head of the linked list.

//FUNCTION DEFINITION
void head_insert(NodePtr& head, int the_number)
{
	NodePtr temp_Ptr;     //the node pointer called "NodePtr" created a new pointer called temp_Ptr
	temp_Ptr = new Node;    //the temp_ptr creates a new node
	
	temp_Ptr->data = the_number;  // temp_ptr adds data into the new node
	
	temp_Ptr->link = head;    //temp_ptr link member of temp_ptr is made to point to the head
	head = temp_Ptr ;    //the head is now pointing to the temp_ptr
}
//Steps for inserting a node as the FIRST node
//1) create a new dynamic variable called temp_ptr (this the new Node)
//2) Place the data in this new Node
//3) MAke the link member of this new node point to the head node(first node) of the original linked list
//4) Make the pointer variable named head point to the new node
   
   int main() {
   	
   	Node *head, *after_me, *newNode, *first, *second;
	   Node data, *NodePtr;
   	     	   
   	   first = new Node;
		  first->data = 15;
		  first->link = head;
		  head = first;
   	    second = new Node;
   	    second->data = 50;
   	    second->link = head;
   	  	head = second;
		    	     	   
   	   cout<< "new node inserted at the head = " << first->data;
   		cout << endl << "second node inserted = " << second->data;
   		NodePtr = newNode;
   			newNode->data = 89 ;
   		 	//second->link = newNode->data;	   	
   	        newNode->link = NULL;
	       cout<< endl << "third node = : " <<newNode->data;
   return 0;
   }

Here is the snap shot of the program running:

new node inserted at the head = 15
second node inserted = 50
third node = : 89

The linked lists are interesting I must admit. They intrigue me.
You have forgot to initialize head before using it on line 47.

newNode has also not been initialized before using it on line 56. The crash probably happens on line 57 when you try to read the data of the node pointed to by the uninitialized pointer.

Not really an error, but a slightly confusing I must say, is that you have a variable and a type both named NodePtr.
Last edited on
Lets first strip the nonessential bits from your pogram:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

struct Node
{
	int data;
	Node * link;
};

int main() {
  Node * newNode; // uninitialized pointer

  newNode->data = 89 ; // undefined behaviour
  newNode->link = NULL; // undefined behaviour
  std::cout << newNode->data; // undefined behaviour
  return 0;
}

Do you now see the source of the hiccup?
The linked lists are interesting I must admit. They intrigue me.

Trees are usually studied next. The many algorithms and uses for trees was always on of my favorite things to study.



You have this wonderful head_insert() function but then you try to insert nodes by hand. Why not use the function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//Adding a node to a Linked List
#include<iostream>
#include<cstdlib>

using namespace std;

struct Node
{
    int data;
    Node *link;
};

typedef Node *NodePtr;		//the structure called "NODE" declares a pointer variable called NodePtr

//Function to Add a Node at the head of a Linked List
void head_insert(NodePtr & head, int the_number);
//Precondition: The pointer variable head points to
//the head of a linked list.
//Postcondition: a new node containg the_number
//has been added at the head of the linked list.

//FUNCTION DEFINITION
void
head_insert(NodePtr & head, int the_number)
{
    NodePtr temp_Ptr;		//the node pointer called "NodePtr" created a new pointer called temp_Ptr
    temp_Ptr = new Node;			 //the temp_ptr creates a new node

    temp_Ptr->data = the_number;		 // temp_ptr adds data into the new node

    temp_Ptr->link = head;			 //temp_ptr link member of temp_ptr is made to point to the head
    head = temp_Ptr;				 //the head is now pointing to the temp_ptr
}

//Steps for inserting a node as the FIRST node
//1) create a new dynamic variable called temp_ptr (this the new Node)
//2) Place the data in this new Node
//3) MAke the link member of this new node point to the head node(first node) of the original linked list
//4) Make the pointer variable named head point to the new node

int
main()
{

    Node *head = nullptr, *first, *second, *third;
    head_insert(head, 15);
    first = head;
    head_insert(head,50);
    second = head;

    cout << "new node inserted at the head = " << first->data;
    cout << endl << "second node inserted = " << second->data;

    head_insert(head, 89);
    third = head;
    cout << endl << "third node = : " << third->data;
    return 0;
}

new node inserted at the head = 15
second node inserted = 50
third node = : 89

Thank you all.
Everything became OKAY!!! after initializing the head to NULL.
Topic archived. No new replies allowed.