number in link list

I have this program in c which i ask user if how many numbers they want to have then i store it in a link list then print it.

 

#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
void add_end(struct node **, int);
int main(){
int n, m;
printf(“How many numbers: “);
scanf(“%d”, &m);
printf(“Enter the number: “);
scanf(“%d”, &n);
struct node *head = malloc(sizeof(struct node));
head->data = n;
head->link = NULL;
struct node *ptr = head;
for(int i = 0; i < (m-1); i++){
printf(“Enter the number: “);
scanf(“%d”, &n);
add_end(&ptr, n);
}
ptr = head;
while(ptr != NULL){
printf(“%d “, ptr->data);
ptr = ptr->link;
}
return 0;
}
void add_end(struct node **ptr, int data){
struct node *temp = malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
(*ptr)->link = temp;
*ptr = temp;
}

1 thought on “number in link list”

Leave a Comment