Create and Sort a Single Linked List - C Program

Write a C program to create a single linked list and then sort that list using separate function (Use local pointer i.e. with return statement).

Solution:

Linked List:
Linked list is a linear data structure and each element containing the address of its successor. A linked list represents by a pointer to the first node of the linked list. The first node is called head.

In this example we developed two functions one for insertion of linked list and another is for sorting of elements.

Program

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
     int info;
     struct node *next;
}nodetype;
nodetype* insert(nodetype*);
void sort(nodetype*);
void main()
{
     nodetype* left=NULL;
     nodetype* right=NULL;
     int ch,c;
     printf("Enter 1 for insert \nEnter 2 for sort \nEnter 3 for exit");
     do
     {
          printf("\nEnter choice: ");
          scanf("%d",&ch);
          switch(ch)
          {
               case 1:
               right=insert(right);
               if(left==NULL)
               {
                   left=right;
               }
               break;
               case 2:
               sort(left);
               case 3:
               exit(1);
               default:
               printf("invalid choice");
               break;
          }
          printf("do you want to continuoe press 1: ");
          scanf("%d",&c);
     }while(c==1);
     getch();
}
nodetype* insert(nodetype *r)
{
     i.nt n;
     nodetype *p;
     p=(nodetype*)malloc(sizeof(nodetype));
     printf("\nEnter the number: ");
     scanf("%d",&n);
     if(p!=NULL)
     {
          p->info=n;
          p->next=NULL;
          if(r==NULL)
          {
               r=p;
          }
          else
          {
               r->next=p;
               r=p;
          }
          return(r);
     }
     else
     {
          printf("not enough memory");
     }
}
void sort(nodetype *l)
{
     nodetype *t;
     nodetype *s;
     int x;
     t=l;
     while(t!=NULL)
     {
          s=t->next;
          while(s!=NULL)
          {
               if(t->info>s->info)
               {
                    x=t->info;
                    t->info=s->info;
                    s->info=x;
               }
               s=s->next;
          }
          t=t->next;
      }
      t=l;
      while(t!=NULL)
      {
            printf(" %d ",t->info);
            t=t->next;
      }
}


Output:

sort single linked list