I am trying do a simple insert Node to last position of linked list. But I am running into trouble.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
Node* Insert(Node* head, int data);
Node* print(Node* head);
void ReverseIterative();
Node* Insert(Node* head, int data)
{
Node* newNode;
newNode->data = data;
newNode->next = NULL;
if(head == NULL)
{
return newNode;
}
Node* curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next = newNode;
return head;
}
Node* printList(Node* head)
{
while(head)
{
cout<<head->data;
head=head->next;
}
cout<<endl;
}
int main()
{
struct Node* head = NULL;
head = Insert(head, 2);
head = Insert(head, 4);
printList(head);
return 0;
}
I am not sure what im doing wrong. Please help~! I looked over my logic and everything should be correct. It might be the way I am creating a new node in the insert(). I think there is something wrong with my syntax, but i am not sure what it is. I really hate when this happens...
Thanks for your help