It's not working when I try to insert at beginning first. But working when I try to insert at end first.
Liked lists should work both ways I think. It's singly linked lists.
I am learning so please explain thoroughly and in easy language.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node * insrt_frnt(struct node *head){
struct node *temp = (struct node *) malloc(sizeof(struct node *));
printf("\n Enter data:");
scanf("%d", &temp->data);
if(head == NULL){
head = temp;
} else {
temp->next = head;
}
return temp;
}
struct node * insrt_end(struct node *head){
struct node *ptrr = head;
struct node *neww = (struct node *) malloc(sizeof(struct node *));
printf("\n Enter data:");
scanf("%d", &neww->data);
neww->next = NULL;
if(head == NULL){
head = neww;
} else {
while(ptrr != NULL){
ptrr = ptrr->next;
}
ptrr->next = neww;
}
return head;
}
void display(struct node *head){
while(head != NULL){
printf("%d\n", head->data);
head = head->next;
}
}
int main(){
struct node *head = NULL;
head = insrt_frnt(head);
head = insrt_end(head);
display(head);
return 0;
}