This is a algorithm for sorting a singly linked list. Also we mention the topic we cover here , are create e linked list , traverse linked list , sorting a linked list.

ALGORITHM FOR SORTING A SINGLY LINKED LIST

_____________________________________________________________________

Algorithm to create a linked list

INPUT: A set of data values, number of list element, n

OUTPUT: Singly linked list

                                                                                         

Step 1: Input n

Step 2:  head = new node

Step 3: list = head

Step 4: list -> next= NULL

Step 5: For I = 1 to n

            (i). Input list -> data

            (ii). list = new node

            (iii). list 1 -> next = NULL

            (iv). list 1 = list

           (End of for loop)

Step 6: Return head

(End of Algorithm)

                                                                                                                                                                      

Algorithm for traversal of a singly linked list

INPUT: A singly linked list whose first nodes given by pointer head.

OUTPUT: Data of each nodes

                                                                           

Step 1: list = head

Step 2:  While (list NOT EQUAL to NULL)

              (i). list -> data

              (ii). list = list -> next

              (End of while loop)

Step 3: Return head

(End of Algorithm)

                                                                                                                                                                      

Algorithm for sorting a singly linked list

INPUT: A singly linked list whose first nodes given by pointer head.

OUTPUT: Singly linked list

                                                                                        

Step 1: For I=head and I NOT EQUAL TO NULL and I=I->next

(1).for j=I->next and j NOT EQUAL TO NULL and j=j->next

   a). if(I->data>j->data)

      Temp=I->data

I->data=j->data

J->data=temp

           (End of if)

        (End of nested for loop)

Step 2:display

Step 3:Return head

 

(End of Algorithm)

                                                                                                                                                                   

Go to the C program: https://browncodeit.blogspot.com/2020/05/write-program-in-c-to-sort-linked-list.html

0 comments:

Post a Comment