How to sort a linked list

//sorting in descending order
struct node {
  int value;
  node * NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort(Node * Head) {
  node * first, second, temp;
  first = Head;
  while (first != null) {
    second = first -> NEXT;
    while (second != null) {
      if (first -> value < second -> value) {
        temp = new node();
        temp -> value = first -> value;
        first -> value = second -> value;
        second -> value = temp -> value;
        delete temp;
      }
      second = second -> NEXT;
    }

    first = first -> NEXT;
  }
}
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in