Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int age;
struct node *next;
};
struct node *add() {
struct node *second = NULL;
struct node *third = NULL;
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
second->age = 20;
second->next = third;
third->age = 30;
third->next = NULL;
return second;
}
int count(struct node *head){
struct node *current = head;
int count=0;
while(current!=NULL){
count++;
current = current->next;
}
return count;
}
struct node* addz(){
struct node *head;
head = add();
struct node *first;
first = malloc(sizeof(struct node));
first->age = 10;
first->next = head;
head = first;
return head;
}
int show(struct node *head){
struct node *current = head;
while(current!=NULL){
printf("age: %d\n",current->age);
current = current->next;
}
}
int main(void){
struct node *head;
head=addz();
show(head);
printf("%d\n",count(head));
getchar();
return 0;
}
my friend says that im doing something wrong, i dont seem to find anything wrong in my code
i create 2 nodes second and third
then i use a function to create the first node that will point to the second node, the second to the third and the head will point to the first