Friday 18 October 2013

Best and sort code for Linked List In C++

/*
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ::::  THIS PROGRAMM INDICATES THE CONCEPT OF LINKED LIST   ::::
    ::::  CREATED BY : DX PANCHAL(M.C.A.)  DATED:5/03/2013     ::::
    ::::  PROGRAMMING LANGUAGE : c++                           ::::
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

*/
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<dos.h>

struct list
{
  int info;
  struct list *next;

};

typedef struct list *newnode;
newnode first,newn,temp,save,avail,pred;
void main()
{
 int ch,i,choice;
 textcolor(WHITE);
 clrscr();
 first=NULL;
 avail=(newnode)malloc(sizeof(struct list));
 avail->next=NULL;
 cout<<"\n\t\tENTER VALUES YOU WANT TO CREATE NODE\n";
 cin>>ch;
 temp=avail;
 for(i=1;i<ch;i++)
 {
  temp->next=(newnode)malloc(sizeof(struct list));
  temp=temp->next;
  temp->next=NULL;

 }
 cout<<"\n\t\tYOU HAVE "<<ch<<"  NODE SUCCESSFULLY CREATED....!!!";
 getch();

 do
 {

  clrscr();
  cout<<"\n\t\t ";
  cout<<"\n\t\t|       LINKED LIST MENU         |";
  cout<<"\n\t\t|________________________________|\n";
  cout<<"\n\t\t[1] FOR INSERT NODE AT FIRST      ";
  cout<<"\n\t\t[2] FOR INSERT NODE AT LAST       ";
  cout<<"\n\t\t[3] FOR INSERT NODE AS ORDER      ";
  cout<<"\n\t\t[4] FOR DELETE NODE AT FIRST      ";
  cout<<"\n\t\t[5] FOR DELETE NODE AT LAST       ";
  cout<<"\n\t\t[6] FOR DELETE NODE AT ORDER      ";
  cout<<"\n\t\t[7] FOR DISPLAY NODE LIST         ";
  cout<<"\n\t\t[0] FOR EXIT                      ";
  cout<<"\n\t\t ";
  cin>>choice;
  switch(choice)
  {
   case 1:
       if(avail==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR INSERT OPERATION..!!!");
         textcolor(WHITE);

       }
       else
       {
         newn=avail;
         avail=avail->next;
         cout<<"\n\t\tENTER VALUE FOR NODE ";
         cin>>newn->info;
         if(first==NULL)
         {
          newn->next=NULL;
          first=newn;
         }
         else
         {
         newn->next=first;
         first=newn;

         }
          cout<<"\n\t\tOPERATION SUCCESSFULL..!!!";

       }
       getch();
       break;
   case 2:
      if(avail==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR INSERT OPERATION..!!!");
         textcolor(WHITE);

       }
       else
       {
         newn=avail;
         avail=avail->next;
         cout<<"\n\t\tENTER VALUE FOR NODE ";
         cin>>newn->info;
         if(first==NULL)
         {
          newn->next=NULL;
          first=newn;
         }
         else
         {
         save=first;
         while(save->next!=NULL)
         {
          save=save->next;
         }
         save->next=newn;
         newn->next=NULL;


         }
          cout<<"\n\t\tOPERATION SUCCESSFULL..!!!";

       }
       getch();
       break;
   case 3:
       if(avail==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR INSERT OPERATION..!!!");
         textcolor(WHITE);

       }
       else
       {
         newn=avail;
         avail=avail->next;
         cout<<"\n\t\tENTER VALUE FOR NODE ";
         cin>>newn->info;
         if(first==NULL)
         {
          newn->next=NULL;
          first=newn;
         }
         else
         {
        if(newn->info<=first->info)
        {
          newn->next=first;
          first=newn;

        }
        else
        {
          save=first;
          while(newn->info>=(save->next)->info&&save->next!=NULL)
          {
          save=save->next;
          }

          newn->next=save->next;
          save->next=newn;
        }
         }
         cout<<"\n\t\tOPERATION SUCCESSFULL..!!!";

       }


       getch();
       break;


   case 4:
       if(first==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR DELETE OPERATION..!!!");
         textcolor(WHITE);
       }
       else
       {
        temp=first;
        first=temp->next;
        cout<<"\n\t\tFIRST NODE IS DELETED SUCCESSFULLY...!!!";
        temp->next=avail;
        temp=avail;

       }
       getch();
       break;
   case 5:

       if(first==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR DELETE OPERATION..!!!");
         textcolor(WHITE);
       }
       else
       {
        temp=first;
        while((temp->next)->next!=NULL)
        {
         temp=temp->next;
        }


        (temp->next)->next=avail;
        avail=temp->next;
        temp->next=NULL;
        cout<<"\n\t\tLAST NODE IS DELETED SUCCESSFULLY...!!!";

       }
       getch();
       break;

   case 6:
       if(first==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR DELETE OPERATION..!!!");
         textcolor(WHITE);
       }
       else
       {
        temp=first;
        int selected;
        cout<<"\n\t\tENTER NODE DATA WHICH YOU WANT TO DELETE..!!!";
        cin>>selected;
        while(temp->info!=selected)
        {
         pred=temp;
         temp=temp->next;
        }
        pred->next=temp->next;
        if(temp->info!=selected)
        {
          cout<<"\n\t\tSORRY,NODE NOT FOUND...!!!";


        }
        else
        {
        avail=temp->next;
        avail=temp;

         cout<<"\n\t\tSELECTED NODE IS DELETED SUCCESSFULLY...!!!";
        }
       }
       getch();
       break;
   
   case 7:
       cout<<"\n\t\t ";
       cout<<"\n\t\t TOTAL NODE IN  LIST";
       if(first==NULL)
       {
         cout<<"\n\t\t";
         textcolor(RED+BLINK);
         cprintf("NO NODE AVAILABLE FOR DISPLAY OPERATION..!!!");
         textcolor(WHITE);

       }
       else
       {
        temp=first;
        cout<<"\n\t\t------------------------------";
        cout<<"\n\t\t| DATA | NODE ADD.| NXT. ADD.|";
        cout<<"\n\t\t------------------------------";
        while(temp!=NULL)
        {
         cout<<"\n\n\t\t";
         cout<<"|"<<temp->info<<"    | "<<unsigned(temp)<<"     | "<<unsigned(temp->next)<<"|";

         temp=temp->next;

        }

       }
        cout<<"\n\t\t ";

       break;
   case 0:
      cout<<"\n\t\tDO YOU WANT EXIT...!!! ";
      cout<<"\n\t\tPRESS ANY KEY TO EXIT";
      getch();
      break;


   default:
       cout<<"\n\t\t";
       textcolor(RED+BLINK);
       cprintf("INVALID CHOICE.....!!!");
       textcolor(WHITE);

  }



 getch();


 }while(choice!=0);



}

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...