Hello,

Following is my code for link list which positions the node depending uppon the data value at run time. Though, I know that conceptually binary tree is better solution for such scenario, can anybody suggest me optimization on following code?

Code:# include <stdio.h> # include <malloc.h> void sort_append(struct node **,int); void display(struct node *); struct node { int data; struct node *next; }node; main(void) { int i; struct node *pHead; pHead = NULL; printf("Enter the numbers:\n"); scanf("%d",&i); while (i) { sort_append(&pHead,i); scanf("%d",&i); } display(pHead); return 0; } // Inserts, appends or prepends depending upon its value. void sort_append(struct node **ppHead,int i) { struct node *new_node; new_node = (struct node*)malloc(sizeof(node)); new_node->data = i; // First time when not even single node is generated if ( *ppHead == NULL ) { new_node->next = NULL; *ppHead = new_node; } // When data is smaller than head else if ((*ppHead)->data > i) { new_node->next = *ppHead; *ppHead = new_node; } // new data is larger than head value or tal value else { struct node *current, *forward; current = *ppHead; forward = (*ppHead)->next; while ( forward ) { if ( (current->data < i) && (forward->data > i) ) { current->next = new_node; new_node->next = forward; return; } current = current->next; forward = forward->next; } // Make it as tail. current->next = new_node; new_node->next = NULL; } } // Displays entire link list void display(struct node *walk) { printf("\nLink list is:\n"); while (walk) { printf("%d\n",walk->data); walk = walk->next; } }