I am a beginner in coding and i am creating a linked list(without using class concept), but this is not working, please help.
#include<iostream>
#include<malloc.h>
using namespace std;
struct node{
int data;
node *next;
};
void add(int n){
node *head = NULL;
node *temp = (node*)malloc(sizeof(node));
temp->data = n;
temp->next = head;
head=temp;
}
void traverse(){
node *temp1 = head;
while(temp1!=NULL){
cout<<temp1->data;
temp1 = temp1->next;
}
}
int main(){
add(1);
add(2);
add(3);
traverse();
return 0;
}