Put the code you need help with here.
//Adding a node to a Linked List
#include<iostream>
#include<cstdlib>
usingnamespace 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.
//Adding a node to a Linked List
#include<iostream>
#include<cstdlib>
usingnamespace 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