Thank you so much
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
void check_allocation(void *ptr) {
if (!ptr) {
fputs("Allocation failed!\n", stderr);
exit(EXIT_FAILURE);
}
}
struct node * ADD ( struct node *Head, int value)
{
struct node * new = malloc( sizeof(*new));
check_allocation(new);
new->data = value;
if (Head) {//check if Head is NULL
new->next = Head->next;
Head->next = new;
}else {
new->next = new;
}
return new;
}
void show ( struct node* head)
{
struct node* current;
for ( current = head; current != NULL; current = current -> next )
{
printf("%d ", current -> data);
}
}
int main()
{
struct node *head = NULL;
head = ADD( head, 1);
head = ADD( head, 2);
head = ADD( head, 3);
head = ADD( head, 4);
head = ADD( head, 5);
show(head);
return 0;
}
Output
Code:
5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3