#include
#include
#include
#include
#include
#include
typedef
 struct dta{
     char book[30];
     writer[30];
     code[30];
     namb[30];
     publish[30];
   struct dta*link;
   } data;
   typedef
     struct{
       char book[30];
       id1;
       writer[30];
       id2;
       code[30];
       id3;
       numb[30];
       id4;
       publish[30];
       } bnk;
 data *new,*first,*this;
 bnk bank;
void zero_bank(void)
  {
    register int cnt;
    for (cnt=0;cnt    *(char*)(bank+cnt)="'\o";
    }
    void load(void)
    {
    FILE *fp;
     if((fp=fopen("data.bnk","r"))=null)
        {
        printf("\n\n new file.");
        retern;
        }
    fread (&bank,sizeof(bank),1,fp);
    while(!feof(fp))
        {
  if ((new_=mblloc(sizeof(data)))==null
  {
   printf("\n\rnot enough memory ...");
   fclose(fp);
   return;
   }
     new_->link=null;
     if (frist==null)  frist=this_=new_;
     else
 {
 this_=frist;
 while(this_->link!=null)this_=this_->link;
 this_->link=new_;
 this_=new_;
 }
    strcpy(this_->code,bank.code);
    strcpy(this_->book,bank.book);
    strcpy(this_->writer,bank,writer);
    strcpy(this_->numb,bank.publish);
    strcpy(this_->publish,bank.publish);
    fread(&bank,sizeof(bank),1,fp);
       }
    fclose(fp);
     }
    void save(void)
      {
 FILE *fp;
 if((fp=fopen("data.bnk","w"))=null)
   {
   printf("\n\ncan not open file.");
   retern;
   }
  this_=frist;
       while (this!=null)
       {
 zero_bank();
 strcpy(bank.cod,this_->code);
 strcpy(bank.book,this_->book);
 strcpy(bank.writer,this_->writer);
 strcpy(bank.numb,this_->numb);
 strcpy(bank.publish,this_->publish);
 bank.id1=bank.id2=bank.id3=bank.id4='*';
 fwrite(&bank,sizeof(bank),1,fp);
 this_=this_->link;
 }
     fclose(fp);
     }
     void enter(void)
       {
  if ((new_=mblloc(sizeof(data)))==null)
     {
      printf("\n\rnot enough memory...");
      reternl;
      }
      new_->link=null;
      if (frist==null)  frist=this_=new_;
       else
  {
   this_=frist;
   while(this_->link!=null)this_=this_-link;
   this_->link=new_;
   this_=new-;
   }
   printf ("\n enter book code:");
   gets(this_->code);
  printf ("\n enter book name:");
  gets(this_->book);
  printf ("\n enter book writer name:");
  gets (this_->writer);
  printf ("\n enter numberof this book:");
  gets (this_->numb);
  printf ("\n enter publisher name:");
  gets (this_->publish);
  printf ("\n \n\n\r\t\tpress any  key ...\n");
  }
    void display(void)
  {
  int i;
  if(first==null)
  {
  printf("\n\t listis empty.");
  printf("\n\t press any key to continue...");
  getch();
  retern;
  }
  else
     {
      this_=first;
      while (this_!=null)
        {
        printf("\nbook code:");
        printf("%s",this_->code);
        printf("\nbook name:  :");
        printf("%s",this_->book);
        printf("\n book write name:");
        printf("%s",this_->write);
        printf("\n number of this book:");
        printf("%s",this_->numb);
        printf("\n publuisher name:");
        printf("%s",this_->publish);
        printf("\n\n\n\r\t\tpress any key ...\n\n");
        this_=this_->link;
        getch();
        }
  }
  getch();
  }
  void delete_(void)
  {
  char code[30];
  printf("\n\tentre book code for delete:");
  gets(code);
  this_=first;
  while (this_!null)
  {
  if(!strcmp(this_->code,code))
  {
  if(this_==frist) frist=this_->link;
  else               new_->link=_->link;
  free(this_);
  return;
  }
  new_=this_;
  this_=this_->link;
     }
      printf("\n\n\t\t record not found.");
       sleep(3);
      }
      void  sortbycode(void)
      {
        data *q,*p,*q1;
        data *frist2;
        frist2=null;
        q1=frist;
        while(q1!=null)
        {
        new_=mblloc(sizeof(data));
        strcpy(new_->code,q1->code);
        strcpy(new_->book,q1->book);
        strcpy(new_->writer,q1->writer);
        strcpy(new_->numb,q1->numb);
        strcpy(new_->publish,q1->publish);
        if(frist2==null)
  {
  new_->link=null;
  frist2=new_;
  p=q=frist2;
  first->link=null;
  }
  else
    {
    if (atoi(new_->code)code))
     {
        new_->link=first2;
        first2=new_;
        p=q=first2;
        }
        else
        {
        p=q=frist2;
        while(atoi(p->code)code)&&p!=null)
        {
        q=p;
        p=p->link;
  }
      if(p==null)
       {
       new_->link=null;
       q->link=new_;
       new_->link=null;
       }
       else
       {
       new_->link=p;
       q->link=new_;
       }
     }
   }
      q1=q1->link;
      }
    frist=frist2;
    }
    void sortbyname(void)
    {
     data  *q,*p,*q1;
     data *first2;
     first2=null;
     q1=frist;
     while(q1!=null)
     {
        newe_=mblloc(sizeof(data));
        strcpy (new_->code,q1->code);
        strcpy (new_->book,q1->book);
        strcpy (new_->writer,q1->writer);
        strcpy (new_->numb,q1->numb);
        strcpy (new_->publish,q1->publish);
        if(frist==null)
   {
   new_->link=null;
   frist2=new_;
   p=q=frist2;
   frist->link=null;
   }
       else
       {
       if(new_->book[0]book[0])
        {
        new_->link=frist2;
        first2=new_;
        q=p=frist2;
        }
  else
  {
  p=q=frist2;
  while(p->book[0]book[0]&&p!=null)
  {
  q=p;
  p=p->link;
  }
     if (p==null)
      {
      new_->link=null;
      q->link=new_;
      new_->link=null;
      }else
      {

      new_->link=p;
      q->link=new_;
      }
    }
  }
  q1=q1->link;
       }
       frist=frist2;
     }
     void sort by publish(void)
       {
       data *q,*p,*p1;
       data *frist2;
       frist2=null;
       q1=frist;
       while(q1!=null)
       {
       new_=mblloc(sizeof(data));
       strcpy (new_->code,q1->code);
       strcpy(new_->book,q1->book);
       strcpy(new_->writer,q1->writer);
       strcpy(new_->numb,q1->numb);
       strcpy(new_->publish,q1->publish);
       if (first2==null)
 {
 new_->link=null;
  frist2=new_;
  p=q=frist2;
  frist->link=null;
  }
  else
  {
  if(new_->publish[0]publish[0])
   {
   new_->link=frist2;
   first=new_;
   q=p=frist2;
   }
   else
   {
    p=q=frist2;
    while(p->publish[0]publish[0]&&p!=null)
       {
       q=p;
       p=p->link;
       }
     if (p==null)
       {
       new_->link=null;
       q->link=new_;
       new_->link=null;
       }
       else
       {
       new_->link=p;
       q->link=new_;
       }
    }
   }
   q1=q1->link;
  }
  frist=frist2;
  }
   viod sortbywriter(viod)
       {data  *q,*p,*q1;
       data *frist;
       frist2=null;
       q1=frist;
       while(q1!=null)
       new_=mblloc(sizeopf(data));
       strcpy(new_->code,q1->code);
       strcpy(new_->book,q1->book);
       strcpy(new_->writer,q1->writer);
       strcpy(new_->numb,q1->numb);
       strcpy(new_->publish,q1->publish);
       if (frist2==null)
       {
       new_->link=null;
       frist2=new_;
       p=q=frist2;
       frist2->link=null;
       }
       else
       {
       if (new_->writer[0]writer[0])
       {
       new_->link=frist;
       frist=new_;
       q=p=frist2;
       }
       else
       {
       p=q=frist2;
        while(p->writer[0]writer[0]&&p!=null)
      {
      p=q;
      p=p->link;
       }
        if(p=null)
       {
       new_->link=null;
       q->link=new_;
       new_->link=null;
       }
       else
       {
        new_->link=p;
        q->link=new_;
        }
     }
        }
        q1=q1->link;
        }
        frist=frist2;
        }
   void edit(void)
   {
   char code[30];
   char bool=0;
   new_=this_=frist;
   printf("\n\tentr code:");
   gets(code);
   while(this_!=null)
        {
        if(!strcpy(this_->code,code))
     printf("\n book code :");
     printf("%s",this_->code);
     printf("\n book name :");
     printf("%s",this_->book);
     printf("\n book writer name:");
     printf("%s",this_->writer);
     printf("\nnumber of this book :");
     printf("%s",this_->numb);
     printf("\n publish name :");
     printf("%s",this_->publish);
     //this_=this_->link;
     bool=1;
     break;
       }
       else
       {
       new_=this_;
       this_=this_->link;
       continue;
     }
   }
   if(!bool) printf("\t\trecord not found.\n" );
   else
     {
     printf("\t\tenter new data...\n");
     printf("\nenter book code:");
     gets(this_->code);
     printf("\nenter book name:");
     gets(this_->book);
     printf("\nenter book writer name:");
     gets(this_->writer);
     printf("\n enter number of this book:");
     gets(this_->numb);
     printf("\nenter publish name:");
     gets(this_->publish);
     printf("\n\n\n\r\t\tpress any key...\n");
     }
     }
     void searchbycode(void)
       {
       cher code[30];
       cher bool=0;
       new_=this_=first;
       printf("\n\tenter code:");
       gets(code);
       white(this_!=null)
  {
   if (!strcpy (this_->code,code))
     {
     printf("\nbook code:");
     printf("%s",this_->code);
     printf("\n book name:");
     printf("%s",this_->book);
     printf("\nbook write0r name:");
     printf("%s",this_->writer);
     printf("\nnumber of this book");
     printf("%s",this_->numb);
     printf("\npublish name:");
     printf("%s",this_->publish);
     printf("\n\n\n\r\t\tpress any key...");
     this_=this_->link;
     gtch();
     bool=1;
   }
   else
     {
     new_-this_;
     this_=this_->link;
     continue;
  }
       }
       if (!bool)
  {
    printf("\n\t\t record not found.\n");
    sleep(3);
    }
  }
  void  serchbyname(void)
  {
   char code[30];
   char bool=0;
   new_=this_=first;
   printf("\n\t enter book nameL:");
   gets(code);
   while (this_!=null)
      {
      if (!strcmp(this_->book,code))
        {
        printf("\nbook code:");
        printf("%s",this_->code);
        printf("\n book name:");
        printf("%s",this_->book);
        printf("\nbook writer name:");
        printf("%s",this_->writer);
        printf("\nnumber of this book");
        printf("%s",this_->numb);
        printf("\npublish name:");
        printf("%s",this_->publish);
        printf("\n\n\n\r\t\tpress any key...");
        this_=this_->link;
        gtch();
         bool=1;
         }
         else
     {
     new_-this_;
     this_=this_->link;
     continue;
  }
       }
       if (!bool)
  {
    printf("\n\t\t record not found.\n");
    sleep(3);
    }
  }
  void  serchbyname(void)
  {
   char code[30];
   char bool=0;
   new_=this_=first;
   printf("\n\t enter book nameL:");
   gets(code);
   while (this_!=null)
      {
      if (!strcmp(this_->book,code))
        {
        printf("\nbook code:");
        printf("%s",this_->code);
        printf("\n book name:");
        printf("%s",this_->book);
        printf("\nbook writer name:");
        printf("%s",this_->writer);
        printf("\nnumber of this book");
        printf("%s",this_->numb);
        printf("\npublish name:");
        printf("%s",this_->publish);
        printf("\n\n\n\r\t\tpress any key...");
        this_=this_->link;
        gtch();
         bool=1;
         }
         else
     {
     new_-this_;
     this_=this_->link;
     continue;
  }
       }
       if (!bool)
  {
    printf("\n\t\t record not found.\n");
    sleep(3);
    }
  }
  void  serchbyname(void)
  {
   char code[30];
   char bool=0;
   new_=this_=first;
   printf("\n\t enter book nameL:");
   gets(code);
   while (this_!=null)
      {
      if (!strcmp(this_->book,code))
        {
        printf("\nbook code:");
        printf("%s",this_->code);
        printf("\n book name:");
        printf("%s",this_->book);
        printf("\nbook writer name:");
        printf("%s",this_->writer);
        printf("\nnumber of this book");
        printf("%s",this_->numb);
        printf("\npublish name:");
        printf("%s",this_->publish);
        printf("\n\n\n\r\t\tpress any key...");
        this_=this_->link;
        gtch();
         bool=1;
         }
         else
     {
     new_-this_;
     this_=this_->link;
     continue;
  }
       }
       if (!bool)
  {
    printf("\n\t\t record not found.\n");
    sleep(3);
    }
  }
  void   main(void)
     {
     char ch;
     clrscr();
     frist=null;
     printf("\n\rloading in the file...");
      sleep(1);
      for(;;)
        {
        clrscr();
        printf ("\n\t\t\t 1-entry");
        printf ("\n\t\t\t 2-edit");
        printf ("\n\t\t\t 3-delete");
        printf ("\n\t\t\t 4-search by code");
        printf ("\n\t\t\t 5-search by name");
        printf ("\n\t\t\t 6-search by writer");
        printf ("\n\t\t\t 7-search by publish");
        printf ("\n\t\t\t 8-sort by code");
        printf ("\n\t\t\t 9-sort by name");
        printf ("\n\t\t\t a-sort by writer name");
        printf ("\n\t\t\t b-sort by publisher name");
        printf ("\n\t\t\t c-sort by name & writer");
        printf ("\n\t\t\t d-display");
        printf ("\n\t\t\t q-quit");
        ch=getch();
        sitch(tolower(ch))
         {
    case'1':
       enter();
       break;
    case'2':
        edit();
        break;
   case'3':
        delete_();
        break;
         case '4':
        searchbycode();
        break;
        case'5':
         searchbyname();
        break;
        case'6':
         searchbywriter();
        break;
        case'7':
         searchbypublish();
        break;
       case'8':
       sortbycode();
       break;
        case'9':
       sortbyname();
       break;
        case'a':
       sortbywriter();
       break;
        case'b':
       sortbypublish();
       break;
       case'c':
      sortbyname();
      sortbywriter();
      break;
      case'd':
      display();
      break;
      case'q':
      clrscr();
      printf("\n\rsaving in the file...");
      save();
      exit(0);
     }
  }
     }