#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
void inElFr(struct node** head, int val) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = val;
temp->next = NULL;
if(*head == NULL)
*head = temp;
else {
struct node* run = *head;
while(run->next != NULL) {
run = run->next;
}
run->next = temp;
}
}
void midInsert(struct node** head, int elt) {
struct node* find = *head;
struct node* prvs = *head;
struct node* temp = malloc(sizeof(struct node));
if(elt < find->data) {
temp->data = elt;
temp->next = *head;
*head = temp;
} else {
while(find->next != NULL) {
if(find->data > elt) {
break;
} else {
prvs = find;
find = find->next;
}
}if(find->next = NULL) {
temp->data = elt;
prvs->next = temp;
temp->next = find;
} else {
temp->data = elt;
temp->next = NULL;
prvs->next->next = temp;
}
}
}
void shElFr(struct node* head) {
struct node* sho = head;
while(sho!=NULL) {
printf("%d->", sho->data);
sho = sho->next;
}
printf("Reached");
}
int main() {
int num;
struct node* head = NULL;
inElFr(&head, 10);
inElFr(&head, 20);
inElFr(&head, 40);
inElFr(&head, 50);
shElFr(head);
printf("\n Enter the number \t");
scanf("%d", &num);
midInsert(&head, num);
shElFr(head);
return 0;
}
No comments:
Post a Comment