Implementing Concept of Files, Linked List and Structures all together in C


C has always been a tedious task to learn.Understanding concepts with simple implementation, makes it easy for new programmers . Here , I am giving you the practical implementation for the Advanced C Concepts of Structures, File and Linked List 

# include <stdio.h>
struct employee
{
     int emp_code;
     char emp_name[20];
};

struct slist
{
   int ecode;
   char ename[20];
    struct slist *next;
};

struct slist *fp,*ep,*p,*temp,s;
struct employee  emp;

main()
{
   int ch;
   FILE *fp1;
   fp=NULL; // when the list has not been created yet, pointer to first node is set to NULL
   ep= NULL; // Last Node Pointer also set to NULL
   fp1=fopen("emp.dat","r");
   if (fp1==NULL)
   {
        printf("Error Opening File");
        getch();
        return;
    }
//start reading from file and creating linked list to hold data  
  while((fread(&emp,sizeof(emp),1,fp1))==1)
    {
        temp=(struct slist *)(malloc(sizeof(sizeof(s)));
        if (temp==NULL)
        { 
            printf("Error Fetching Data from FILE, Operation Aborted");
            getch();
             return;
         }
         temp->ecode=emp.emp_code;
         strcpy((temp->ename,emp.emp_name);
         if(fp==NULL)
         {
              fp=temp;
              ep=temp;
              fp->next=NULL;
              ep->next=NULL;
         }
         else
         {
                ep->next=temp;
                temp->next=NULL;
                ep=temp;
           }
      }
printf("Data Fetched successfully, press any Key to Continue");
getch();
fclose(fp1);


 // menu display
while(1)
{
     clrscr();
      printf("\n   1. Show   List");
      printf("\n   2. Add New Data");
      printf("\n   3. Delete Data");    
      printf("\n   4. Sort Data");
      printf("\n   5. Exit");
      printf ("\n Enter Your Choice");
      scanf("%d",&ch);
      
      if (ch==1)
          show_list();
      
      if (ch==2)
         add_data();

      if (ch==3)
         delete_data();

      if(ch==4)
         sort_data();

      if (ch==5)
      {
          write_back();
          break();
       }
}
}

show_list()
{
     p=fp;
     while(1)
      {
         // checks whether you have reached end of list or not
          if (p==NULL)
          {
              printf("List Empty or Data Finished");
              getch();
              return;
           }
            printf("\n Employee Code  %d",p->ecode);
            printf("\n Employee Name  %s",p->ename);
            p=p->next;  // advance pointer to next node reference
        }
     }

add_data()
{
     temp=(struct slist *) (malloc(sizeof(s))); // creates memory space for new node
     if (temp==NULL) // checks if memory allocation failure
     { 
         printf("Memory Allocation Failure");
         getch();
         return;
     }
     clrscr();
     printf("\n Enter   Employee  Code:");
     scanf("%d",&temp->ecode);
     printf("Enter Employee Name:");
     fflush(stdin);
     gets(temp->ename);
     ep->next=temp;
     temp->next=NULL;
     ep=temp;
 }

delete_data()
{
   int x;
   struct slist *prev;
   printf("Enter Employee Code to Delete Data");
   scanf("%d", &x);

   p=fp;
   prev=fp;
   while(1)
   {
      if (p==NULL)
      {
          printf("List  Empty, or Search Failed");
          getch();
          return;
      }

      if((p->ecode)==x)            // checks if current node contains the data to delete
      {
           if ((p==fp) // checks whether matched data is located at First Position in List
           {
               fp=fp->next;
                printf("Data deleted");
                getch();
                return;
            }
            prev->next=p->next;
            printf("Data Deleted");
            getch();
            return;
       }
       prev=p;
       p=p->next;
}

sort_data()
{
    struct slist *q;
    int x;
    char str[20];
     p=fp;
     while(p!=NULL)
     {
          q=fp;
           while((q->next)!=NULL)
            {
                 if ((q->ecode)> ((q->next)->ecode))
                 {
                     x=q->ecode;
                     q->ecode= (q->next)->ecode;
                      (q->next)->ecode=x;
                      strcpy(str, q->ename);
                      strcpy(q->ename,(q->next)->ename);
                      strcpy((q->next)->ename,str);
                   }
              }

              q=q->next;
     }
    
      show_list();

}

write_back()
{
     FILE  *fp1;
     fp1=fopen("emp.dat", "w");

     if (fp1==NULL)
     {
          printf("Write Back Failed, Error Saving FIle, Try Again Later");
          getch();
          return;
      }
      
      printf("Writing back Data, Please Wait........");

      p=fp;
      while(1)
      {
           if (p==NULL)
           {
                printf("Write Back Complete");
                getch();
                break;
            }
            emp.emp_code=p->ecode;
             strcpy(emp.emp_name, p->ename);
            fwrite (&emp,sizeof(emp),1,fp1);
            p=p->next;
        }
        printf("Press Any Key to Continue");
        getch();
        fclose(fp);
}





Comments

Popular posts from this blog

Understanding Working of Text & Binary File in C

List of C Programs that makes u understand concepts in an easy fashion(Part1)

Starting C : A series of Chapters to learn C