Sunday, November 2, 2014

C Program for Array Implementation of LIST with Explanation


If you want to know about ADT, please have a look at What is Abstract Date Type?

If You want to know about LIST ADT, please have a look at What is LIST ADT? 


Please Go through the Array Implementation of List before go into C Program for complete explanation..... 

C Program:::



#include <stdio.h>

int MAXSIZE=10;   //MAXSIZE of List
int CURRENTSIZE=0;//CURRENTSIZE 0f List Initially Zero

int isEmpty()
{
    if(CURRENTSIZE==0)
    {
      return 1;       //If (Current Size==0) "LIST is Empty" return 1
    }                 //else "LIST is not Empty" return 0
    else return 0;
    
}

int isFull()
{
    if(CURRENTSIZE==MAXSIZE)
    {
      return 1;   // If (Current Size==Max Size) "LIST is FULL" return 1
    }             //else "LIST is not FULL" return 0
    else return 0;
}

void insertToEOL(int *Listptr)
{
     if(isFull())
     {
       printf("List is Full Can’t Insert\n");//If List is full return error message ”List is full. Can’t Insert”.
     }                                       
     else                                    //else
     {                                       //
         int element;
         printf("Enter the Element to Insert\n");//Get the element to insert
         scanf("%d",&element);//Get the position/index to insert new Element
         int index=CURRENTSIZE;//by index=CurrentSize i.e POsition=CurrentSize+1
         Listptr[index]=element;//insert element to position
         CURRENTSIZE=CURRENTSIZE+1;//increase the current size by 1
     }
    
}

void deleteFromEOL(int *List)
{
     if(isEmpty())
     {
       printf("List is Empty. We cant Delete\n"); //If List is empty return error message ”List is empty. Can’t delete”.
     }
     else                                         //else
     {
         int element;
         int index=CURRENTSIZE-1;//Get the position/index to delete element
         element=List[index];//index =currentsize-1 i.e Position=currentSize
         printf("Element %d deleted from postion %d\n",element,index+1);
         CURRENTSIZE=CURRENTSIZE-1;//Decrease the CurrentSize by 1
     }
   
}

void insertToFOL(int *List)
{
     
     if(isFull())
     {
      printf("List is Full... cant insert\n");//If List is full return error message ”List is full. Can’t Insert”.
     }
     else  //else
     {
         int index=0,element;//Get the Element
         printf("Enter the element to insert\n");
         scanf("%d",&element);
         
         for(index=CURRENTSIZE;index>0;index--)   //Free the 1st Position by moving all the elements to one position
         {//forward by New Postion=CurrentPosition +1
              List[index]=List[index-1];
         }
         List[0]=element;//insert the element to Position 1 i.e index 0
         CURRENTSIZE=CURRENTSIZE+1;//increase the current size by 1
     }
    
}

void deleteFromFOL(int *List)
{
     if(isEmpty())
     {
        printf("List is Empty... cant delete\n"); //If List is empty return error message ”List is empty. Can’t delete”.
     }
     else//else
     {
          int index=0,element;
          element=List[0]; //
         for(index=0;index<CURRENTSIZE;index++)
         {//Move all the elements except one in 1st position
           List[index]=List[index+1];//element in 1st Position automatically deleted
         }
         CURRENTSIZE=CURRENTSIZE-1;
         printf("Element %d from Position 1\n",element);
     }
   
}

void insertToNthPos(int *List)
{
     if(isFull()) //If List is Full Cant Insert
     {
       printf("List is Full... Cant insert\n");
     } //else
     else
     {                                                  
         int i,index=0,element,pos;
         printf("Available Postion in LIst\n");
         if(isEmpty())//If List is empty We can insert at Postion 1 only
         {
            printf("List is Empty... only Available Position is 1\n");
            printf("Enter the Element to insert\n");//Get the element to insert
            scanf("%d",&element);
            List[0]=element;//Insert the element at index Zero i.e @ Position 1
            CURRENTSIZE=CURRENTSIZE+1; //Increase the CURRENTSIZE by 1
         }
         else //If List is not empty
         {
           do
            {
             printf("Enter the Position(within %d from 1) in which you want to insert Element\n",CURRENTSIZE);
             scanf("%d",&pos);//We can insert the element only at the Position1 to CURRENTSIZE
            }while(pos>CURRENTSIZE || pos<0); //Continue do loop until get the available position i.e from 1 to CURRENTSIZE
            
            printf("Enter the Element to insert\n");
            scanf("%d",&element);//Get the element
             for(index=CURRENTSIZE;index>pos-1;index--)
             {
              List[index]=List[index-1]; //Move the elements of nth postion to CURRENTSIZE Position to one step forward
             }
          List[pos-1]=element;//Insert the element at Nth Postion
          CURRENTSIZE=CURRENTSIZE+1; //Increase the CURRENTSIZE by 1
         }
        
        
        
     }
    
}

void deleteFromNthPos(int *List)
{
     int element,pos,index;
      if(isEmpty())
     {
        printf("List is Empty... cant delete\n");  //If List is empty return error message ”List is empty. Can’t delete”.
     }
     else //else
     {
          do
            {
             printf("Enter the Position(within %d from 1) from which you want to Delete Element\n",CURRENTSIZE);
             scanf("%d",&pos);//We can delete element at position 1 to CURRENTSIZE
            }while(pos>CURRENTSIZE || pos<0); //Continue do loop until get the available position i.e from 1 to CURRENTSIZE
            element=List[pos-1];
          if(pos==CURRENTSIZE)//If Nth Position=CURRENTSIZE
          { //Decrease the CURRENTSIZE by 1
           CURRENTSIZE=CURRENTSIZE-1;//Element at the Nth Position will be automatically deleted
          }
          else//If Nth Postion not equal to CURRENTSIZE
          {
              for(index=pos-1;index<CURRENTSIZE;index++)
             {//Move the elements of nth postion to CURRENTSIZE Position to one step backward     
              List[index]=List[index+1];
             }
             CURRENTSIZE=CURRENTSIZE-1;//Decrease the CURRENTSIZE by 1
          }
          printf("Element %d deleted from postion %d\n",element,pos);
     }
    printf("deleteFromNthPos\n");
}

void searchInList(int *List)
{
     int element,index,i,found=0;//Initially found=0
     if(isEmpty())//If list is empty retun List is empty
     {
      printf("List is empty\n");
     }
     else //If list is not empty
     {
     printf("Enter the Element to search\n");
     scanf("%d",&element);//Get the element to search
     for(i=0;i<CURRENTSIZE;i++)
     {//Look for elements in the Positions 1 to CURRENTSIZE
      if(List[i]==element)//If Searching element available in List[Postion]
      {
       found=1;//found=1
       break;
      }
     }
     if(found) printf("Element %d available in Position %d\n",element,i+1);//If found=1 element available in List
     else printf("Element %d not available in List\n",element);//If found=0 element not available
     }

}

void printList(int List[])
{
     int i=0;
     if(isEmpty())//If List is Empty return List is Empty
     {
        printf("List is Empty\n");
     }
     else  //If List is not empty
     {
         printf("***Position--->Value***\n");
         for(i=0;i<CURRENTSIZE;i++)          //for Postion 1 to CURRENTSIZE
         {
         printf("   %d--------->%d\n",i+1,List[i]); //Print the element in the Postion
         }
     }
     printf("***List Current Status***\n");
     printf("MAXSIZE    =%d\n",MAXSIZE);
     printf("CURRENTSIZE=%d\n",CURRENTSIZE);

    
}
int main()
{
    int List[MAXSIZE];//List array
    int iChoice=0,r=0;
    do
    {
      printf("--------------Array Implementation Of List-----------\n");
      printf("\t\t1. is Empty\n\t\t2. is Full\n\t\t3. Insert Element to End of LIST\n\t\t4. Delete Element from End of LIST\n\t\t5. Insert Element to Front of LIST\n\t\t6. Delete Element from Front of LIST\n\t\t7. Insert Element to nth Position of LIST\n\t\t8. Delete Element from Nth Postion of LIST\n\t\t9. Search Element in LIST\n\t\t10. print LIST\n\t\t11. Exit\n");
      printf("------------------------------------------------------------\n");
      printf("Please Enter Ur Choice\n");
      scanf("%d",&iChoice);
      switch(iChoice)
      {
       case 1:
             r=isEmpty();
             //printf("r=%d\n",r);
             r==0?printf("List is Not Empty\n"):printf("List is Empty\n");
              printf("------------------------------------------------------------\n");
             break;
       case 2:
            r=isFull();
            r==0?printf("List is Not Full\n"):printf("List is Full\n");
             printf("------------------------------------------------------------\n");
            break;
       case 3:
            insertToEOL(List);
             printf("------------------------------------------------------------\n");
            break;
       case 4:
            deleteFromEOL(List);
             printf("------------------------------------------------------------\n");
            break;
       case 5:
            insertToFOL(List);
             printf("------------------------------------------------------------\n");
            break;
       case 6:
            deleteFromFOL(List);
             printf("------------------------------------------------------------\n");
            break;
       case 7:
            insertToNthPos(List);
             printf("------------------------------------------------------------\n");
            break;
       case 8:
            deleteFromNthPos(List);
             printf("------------------------------------------------------------\n");
            break;
       case 9:
            searchInList(List);
             printf("------------------------------------------------------------\n");
            break;
       case 10:
            printList(List);
             printf("------------------------------------------------------------\n");
            break;
       case 11:
            break;
      
       default:
          printf("===============================================================\n");
          printf("\t\tHi Dude You are not entered the right choice\n");
          printf("===============================================================\n");
          }
       }while(iChoice!=11);
       system("PAUSE");
    }
    

Output::

1. Array Implementation Of List Menu

2. Insert to End of LIST

3. Delete From End of LIST

4. Insert to Front Of List

5. Delete from Front Of List

6. Insert to Nth Position of List
7. Delete from Nth Position of List

8. List is Full

9. List is Empty

No comments:

Post a Comment