Thursday 9 January 2014

HOW TO PERFORM SORTING IN LINKED LIST IN C

#include<stdio.h>
struct node
{
 int info;
 struct node *next;

};
typedef struct node *newnode;
newnode first,temp1,newn,pred,temp,avail,save;
void main()
{
 int i,n,t,q,w;
 clrscr();

 //textcolor(3);
 first=NULL;
 avail=(newnode)malloc(sizeof(struct node));
 avail->next=NULL;
 printf("\n\n\t\tenter Value");
 scanf("%d",&n);
 temp=avail;
 for(i=1;i<n;i++)
 {

 temp->next=(newnode)malloc(sizeof(struct node));
 temp=temp->next;
 temp->next=NULL;

 }
m:
clrscr();
textcolor(10);
printf("\n\n\t\tCREATED BY DIXIT PANCHAL");
printf("\n\t\t ");
printf("\n\t\t                                     ");
printf("\n\t\t PRESS 1 For insert Node             ");
printf("\n\t\t PRESS 2 For Display                 ");
printf("\n\t\t PRESS 3 For Sorted Linked list     ");
printf("\n\t\t PRESS 0 For Exit                    ");
printf("\n\t\t                                     ");
printf("\n\t\t ");
scanf("%d",&w);


switch(w)
{
   case 1:
    printf("\n\n\t\t<<YOU HAVE PRESSES 1 FOR INSERT NODE.>>");
    //getch();
    if(avail==NULL)
    printf("\n\t\t<<<Node is not available>>>");
    else

    {
     newn=avail;
     avail=avail->next;
     printf("\n\t\t<<<<Enter value>>>");
     scanf("%d",&q);
     newn->info=q;
     if(first==NULL)
     {
      newn->next=NULL;
      first=newn;

     }
     else
    {
     newn->next=first;
     first=newn;
     }

     }


     getch();

     goto m;

case 2:

      printf("\n\n\t\t<<YOU HAVE PRESSES 2 FOR DISPLAY.>>");
    //getch();
      if(first==NULL)
      {
      printf("\n\n\t\t<<NO node in LINKED LIST >>");
      }
      else
      {
      temp=first;
      while(temp !=NULL)
      {
       printf("\n\t\t %d   %u %u ",temp->info,temp->next,temp);
       temp=temp->next;

      }
      /*if(first1!=NULL)
      {
       printf("\n\n\t\t ");
       printf("\n\t\t Reverse Linked list is Following  ");
       printf("\n\t\t \n\n");
       temp=first1;
      while(temp !=NULL)
      {
       printf("\n\t\t %d   %u %u ",temp->info,temp->next,temp);
       temp=temp->next;

      }

      }
    */
      }
      getch();
      goto m;

case 3:
      printf("\n\n\t\t<<YOU HAVE PRESSES 3 FOR SORTING OF LINKED LIST .>>");
      for(temp=first;temp->next!=NULL;temp=temp->next)
      {
       for(temp1=temp->next;temp!=NULL;temp1=temp1->next)
       {
      if(temp->info>=temp1->info)
      {
       t=temp->info;
       temp->info=temp1->info;
       temp1->info=t;
      }

    }

      }
      printf("\n\n\t\tSorting Operation Is Completly Successfull::");
      printf("\n\n\t\tPress any key to go Main Menu");
      getch();
      goto m;
case 0:
      printf("\n\n\t\t ");
      printf("\n\t\t   Do you want to exit?   ");
      printf("\n\t\t   Press any key to Exit  ");
      printf("\n\t\t ");
      getch();
      exit(0);
default:
      printf("\n\t\t    You Choise is Invalid\n");
      printf("\t\t  ");
      printf("\n\t\tPress any key to go Main Menu");
      getch();
      goto m;
}







getch();

}

No comments:

Post a Comment

How to change Background color by selecting color from Listview

activitymain.xml file::> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xm...