ProAnswers.org

algorithm Sort the elements of a linked list

algorithm Sort the elements of a linked list

typedef struct node{

	    int data;

	    struct node *next;

	}node;

	 

	void sort_list(node *head){

	    node *currn, *nextn;

	    int temp;

	    for(currn = head; currn->next != NULL; currn = currn ->next){

	        for(nextn = currn->next; nextn != NULL; nextn = nextn->next){

	            if(currn->data > nextn->data){

	                temp = currn->data;

	                currn->data = nextn->data;

	                nextn->data = temp;

	            }

	        }

	    }

	}