//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//student name:abrar al-ali.
//ID:0135020.
//disc file name:Assignment 1.
//instructor:Amal.
//lab:9.40 - 11.30
//this program:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct books
{
	char name[15],author[10];
    long serial_no;
	books *nxt;
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//...................FUNCTIONS PROTOTYPE...................

//main funcyions:
//---------------
void insert(books *&,books *&,books *&);
void search(books *,books *);
void display_list(books *,books *);
void delet(books *&,books *&,books *&);
void update(books *&,books *&,books *&);
void exit(char &);//exit from program

//secondray function:
//-------------------
void s_b_s(books *,books *);//search by serial number 
void s_b_b(books *,books *);//search by book name
void s_b_a(books *,books *);//search by author name
void del_all(books *&);//delete all books
void del_node(books *&,books *&);//delete one book
void disp_all(books *,books *);//display all books
void disp_one(books *,books *);//display author's book
void sort(books *&,books *&,books *&);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void main()
{
	books *head=NULL;
    books *curr=NULL;
    books *tail=NULL;
	char ans='n';
	int c;
	do{
		system("cls");
		cout<<"\n\t_________________________________________________\n\n";
		cout<<endl<<"\t\t\t*~~~~ MAIN MENU ~~~*"<<endl;
		cout<<endl<<"\t\t1.INSERT BOOK'S RECORDS"<<endl;
		cout<<endl<<"\t\t2.SEARCH FOR A BOOKS RECORD"<<endl;
		cout<<endl<<"\t\t3.UPDATE THE RECORDS FOR books"<<endl;
		cout<<endl<<"\t\t4.DISPLAY KOOK'S LIST"<<endl;
		cout<<endl<<"\t\t5.DELETE BOOK"<<endl;
		cout<<endl<<"\t\t6.EXIT FROM PROGRAM"<<endl<<endl;
		cout<<"\t_________________________________________________\n";
		cout<<"\t\tenter number for your chioce:";
			cin>>c;
			
		switch(c)
		{
		case 1:system("cls");
			   insert(head,tail,curr);
			   break;
			
		case 2:system("cls");
			   search(head,curr);
		       break;
			
			
		case 3:system("cls");
			   update(head,curr,tail);
			   break;

		case 4:system("cls");
			   display_list(head,curr);
			   break;
			
		case 5:system("cls");
			   delet(curr,head,tail);
		       break;
			
		case 6 :system("cls");
			    exit(ans);
				break;

		default:
			cout<<endl<<"\n     error!! this number is not in menu"<<endl;
			cout<<"press enter to continue"<<endl;
			    getchar();
		}//swich
		if((ans!='y')&&(ans!='Y'))
		{cout<<"press any key to continue"<<endl;
			getch();
			system("cls");
				}
	}while((ans!='y')&&(ans!='Y'));
	del_all(head);//delete list when exit from program
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//..........................FUNCTIONS DIFFENITION......................

//main functions:
//---------------
void insert(books *&head,books *&tail,books *&curr)
{
	
	
	do{
	curr=new books;
	cout<<endl<<"enter informations to stop enter($)"<<endl;
	cout<<"book name:";
	cin>>curr->name;
	if(strcmp(curr->name,"$")==0)
		break;
	cout<<"author name:";
	cin>>curr->author;
	cout<<"serial number:";
	cin>>curr->serial_no;
	curr->nxt=NULL;
	
	sort(head,curr,tail);
	
	
	}while(1);

}//insert

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void sort(books *&head,books *&curr,books *&tail)
{
	books *temp=NULL,*befor=NULL;
	if(head==NULL)
	{
		head=curr;
		tail=curr;
		tail->nxt=NULL;
	}//if

	else
	{
		if(curr->serial_no<head->serial_no)
		{
			curr->nxt=head;
			head=curr;
		}//if
		else if(curr->serial_no>tail->serial_no)
		{
			tail->nxt=curr;
			tail=curr;
		}//elseif
		else
		{
			temp=head;
			while(curr->serial_no>temp->serial_no)
			{
				befor=temp;
				temp=temp->nxt;
			}//while
			curr->nxt=temp;
			befor->nxt=curr;

		}//else2

  	}//else1

} //sort
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void search(books *head,books *curr)
{
	if(head==NULL)
		cout<<"list is empty"<<endl;
	else
	{
	int s;
	do{
		system("cls");	
	  	 cout<<"\t____________________________________\n\n";
	     cout<<endl<<"\t\t~~~ SEARCH MENU ~~~"<<endl;
	     cout<<endl<<"\t\t1.search by the serial number"<<endl;
	     cout<<endl<<"\t\t2.search by the book name"<<endl;
	     cout<<endl<<"\t\t3.search by the author name"<<endl;
       	 cout<<endl<<"\t\t4.exit to main menu"<<endl<<endl;
		 cout<<"\n\t____________________________________\n\n";
	     cout<<"\tchoice number :";
	     cin>>s;
	     switch(s)
		 {
	        case 1:system("cls");
				   s_b_s(head,curr);
		           break;

	       case 2:system("cls");
			      s_b_b(head,curr);
			      break;
		          
		   case 3:system("cls");
			      s_b_a(head,curr);
		          break;
		          
           case 4:break;
	       default: cout<<endl<<"this choice not in menu!!"<<endl;
					cout<<"press any key to return to search menu"<<endl;
							 getch();
		 }//switch
		 
		
	}while(s!=4);
	
	}//else
}//search

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void display_list(books *head,books *curr)
{
	
	
	int di;
    if(head==NULL)
	
		cout<<"list is empty!! "<<endl;
	
	else
	{
		
		do
		{system("cls");	
		cout<<"\t____________________________________\n\n";	
		cout<<endl<<"\t\t~~~display menu~~~"<<endl;
		cout<<endl<<"\t\t1.display all books"<<endl;
		cout<<endl<<"\t\t2.display author's book"<<endl;
		cout<<endl<<"\t\t3.rturn to main menu"<<endl;
        cout<<"\t____________________________________\n\n";	
        cout<<"\tenter number of your chioce:";
		cin>>di;
		switch(di)
		{
		case 1:system("cls");
			disp_all(head,curr);
			break;
		case 2:system("cls");
			disp_one(head,curr);
			break;
		case 3:break;
		default:cout<<endl<<"this choice not in menu!!"<<endl;
					cout<<"press any key to return to display menu"<<endl;
							 getch();
	
		}//switch
	
	}while(di!=3);
	}//else
}//disp	

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

void update(books *&head,books *&curr,books *&tail)
{
	if(head==NULL)
		cout<<"list is impty"<<endl;
	else
	{
		char ob[15],nb[15],oa[10],na[10];
		long onum,nnum;
		int s=0;
		 books *bef=NULL;
		curr=head;
		cout<<"enter book name to update records it:";
		cin>>ob;
	    while(curr!=NULL)
		{
			if(strcmp(curr->name,ob)==0)
			{
			  s=1;
			  strcpy(ob,curr->name);
			  strcpy(oa,curr->author);
              onum=curr->serial_no;
              cout<<"enter new records";
			  cout<<"((if you don't need modify enter 0)):\n";
			  cout<<"enter new book name:";
			  cin>>nb;
			  if(strcmp(nb,"0")!=0)
				      strcpy(curr->name,nb);
				  else
				      strcpy(curr->name,ob);
				  
			  cout<<"enter new author name:";
			  cin>>na;
              if(strcmp(na,"0")!=0)
                     strcpy(curr->author,na);
				 else
					 strcpy(curr->author,oa);
				 
			  cout<<"enter new serial number:";
			  cin>>nnum;
			     if((onum!=nnum)&&(nnum!=0))
					 {
                         curr->serial_no=nnum;
						 if(curr==head)
						 {
							 head=head->nxt;
		                     curr->nxt=NULL;
						 }//if
						 else if(curr==tail)
						 {

							bef=head;
			                 while(bef->nxt!=tail)
			        	          bef=bef->nxt;
		                     tail=bef;
			                 tail->nxt=NULL;
						 }//elseif
						 else
						 {
									bef=head;
						  while(bef->nxt!=curr)
							  bef=bef->nxt;
						  bef->nxt=curr->nxt;
						  curr->nxt=NULL;
						 }//else
                      sort(head,curr,tail);
					 }//if
				 else
						 curr->serial_no=onum;
				 
			}//if1
			
				curr=curr->nxt;

		}//while
		if(s==0)
			 cout<<"this book name not in the list!!\n";
	}//else

}//update

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void delet(books *&curr,books *&head,books *&tail)
{
	int d;
	do{
		cout<<"\t____________________________________\n\n";	
		cout<<endl<<"       ~~~ delete menu ~~~endl;";
		cout<<endl<<"      1.all records in the list"<<endl;
		cout<<endl<<"      2.specific record by name"<<endl;
		cout<<endl<<"      3.exit to main menu"<<endl<<endl;
		cout<<"\t____________________________________\n\n";	
		cout<<"enter number of choice:";
		cin>>d;
		switch(d)
		{
		    case 1:system("cls");	
				   del_all(head);
				   break;  
			
			case 2:system("cls");	
				    del_node(head,tail);
	       			break;
				
			case 3:break;
			default: cout<<endl<<"this choice not in menu!!"<<endl;
					cout<<"press any key to return to delete menu"<<endl;
							 getch();
			}//switch
		
	}while(d!=3);
}//delet

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void exit(char &ans)
{
	cout<<"\n\n\n\n\n\n\n\t\t\t*((are you sure))*\n\t\t\t\n\t\t\t     y / n ?";
	cin>>ans;
}//exit

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//secondray functions:
//--------------------
void s_b_s(books *head,books *curr)
{
	long num;
	int g=0;//counter
	cout<<"enter serial number:";
		           cin>>num;
		           curr=head;
		           while(curr!=NULL)
				   {
			          if(curr->serial_no==num)
					  {
			            cout<<endl<<"book name:"<<curr->name;
                        cout<<"\tauthor name:"<<curr->author;
			            cout<<"\tserial number:"<<curr->serial_no<<endl;
						g=1;
					  break;
					  }//if  
			          curr=curr->nxt;
				   }//while
				   if(g==0)
					   cout<<"this serial number not in list!!"<<endl;
                   cout<<"press any key to return to search menu:"<<endl;
					   getch();
					   system("cls");
}//s_b_s

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void s_b_b(books *head,books *curr)
{
	char book[15];
	int a=0;//counter
     cout<<endl<<"enter book name:";
		           cin>>book;
		           curr=head;
		           while(curr!=NULL)
				   {
			         if(strcmp(curr->name,book)==0)
					 {
			          cout<<endl<<"book name:"<<curr->name;
                      cout<<"\tauthor name:"<<curr->author;
			          cout<<"\tserial number:"<<curr->serial_no<<endl;
					  a=1;
					 }//if
				     
			          curr=curr->nxt;
				   }//while
				   if(a==0)
					   cout<<"this book name not in the list!!\n";
				    cout<<"press any key to return to search menu:"<<endl;
					   getch();
					   system("cls");
}//s_b_b

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void s_b_a(books *head,books *curr)
{
	char aut[10];
	int v=0;//counter
	cout<<endl<<"enter author name:";
		           cin>>aut;
		           curr=head;
                   while(curr!=NULL)
				   {
			           if(strcmp(aut,curr->author)==0)
					   {
			              cout<<endl<<"book name:"<<curr->name;
                          cout<<"\tauthor name:"<<curr->author;
			              cout<<"\tserial number:"<<curr->serial_no<<endl;
						  v=1;
					   }//if
			          curr=curr->nxt;
				   }//while
				   if(v==0)
					   cout<<"this author name not in the list!!\n";
				    cout<<"press any key to return to serach menu:"<<endl;
					   getch();
					   system("cls");
}//s_b_a

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void del_all(books *&head)
{
	
	books *temp=NULL;
    temp=head;
	int x=0;//counter
	         if(head==NULL)
	        	cout<<"list is empty!!"<<endl;
             else
			 {
               while(head!=NULL)
			   {
	        	head=head->nxt;
		        delete temp;
	        	temp=head;
	        	x=1;
			   }
			 }//else
	        if(x==1)
		        cout<<endl<<"list is deleted"<<endl;
	        else
	        	cout<<"list not deleted"<<endl;
			
			
}//del_all

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void del_node(books *&head,books *&tail)
{
	
	 char book[15];
	 int c=0;//counter
	        if(head==NULL)
            cout<<"list is empty!"<<endl;
	        else{
                 cout<<"to delete one books you must enter book name:"<<endl;
	             cout<<" book name:";
	             cin>>book;

	             books *ser=NULL;//pointer to search about name.
	             books *bef=NULL;//to point on befor ser.
         
	             ser= head;
	    	do{
	            if(strcmp(ser->name,book)==0)//name is found
				{ 
		          c=1;
	    	      if(ser==head)//delete from start
				  {
				    head=head->nxt;
		            ser->nxt=NULL;
			        delete ser;
			        cout<<endl<<"deleted successfull"<<endl;
			       break;
				  }//if
                 else if(ser==tail)//delete from last.
				 {
			       bef=head;
			        while(bef->nxt!=tail)//to point on befor last
			        	bef=bef->nxt;
		            tail=bef;
			        tail->nxt=NULL;
			        delete ser;
			        cout<<"deleted successfull"<<endl;
			        break;
				 }//else if
	
		         else//delete from middle
				 {
			      bef=head;
			      while(bef->nxt!=ser)
			          bef=bef->nxt;
			      bef->nxt=ser->nxt;
			      ser->nxt=NULL;
			      delete ser;
			      cout<<"deleted successfull"<<endl;
			      break;
				 }//else
				}//if
		      else
		      ser=ser->nxt;
			}while(ser!=NULL);

	           if(c==0)
		          cout<<"this name not in list"<<endl;
			}//else
			cout<<"press any key to return to delete menu:"<<endl;
					   getch();
					   system("cls");
		
}//del_node

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void disp_all(books *head,books *curr)
{
	int count=0;//counter books
      curr=head;
	  system("cls");
	  cout<<"\tBOOK NAME\tAUTHOR NAME\tSERIAL NUMBER\n";
	  cout<<"\t__________________________________________\n";
			   while(curr!=NULL)
			   {
                 cout<<"\t  "<<curr->name<<"\t\t  ";
                 
		         cout<<curr->author<<"\t\t  ";
		         
	     	     cout<<curr->serial_no<<"\t\t  ";
	    	     cout<<endl;
			     curr=curr->nxt;
			     count++;
			     if(count==20)
				 {
		            cout<<"press any kay to complete display in new page"<<endl;
	                getch();
	                system("cls");
				 }//if
			   }//while
			  cout<<"\t__________________________________________\n\n";
			  cout<<"the numbers of books is: "<<count<<endl;
			  cout<<"press any key to return to display menu:"<<endl;
			  getch();
			  system("cls");
			  
}//disp
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void disp_one(books *head,books *curr)
{
	int z=0;//counter
      char bo[15];
			curr=head;
			system("cls");
			cout<<"enter author name:";
				   cin>>bo;
			while(curr!=NULL)
			   {
				   if(strcmp(curr->author,bo)==0)
				   {
                      cout<<"\tbook name:";
		              cout<<curr->name;
		              cout<<"\tserial number:";
	     	          cout<<curr->serial_no;
	    	          cout<<endl;
					  z=1;
				   }//if
				   curr=curr->nxt;
			   }//while
			if(z==0)
				cout<<"this name not insert\n";
			cout<<"press any key to return to display menu:"<<endl;
			  getch();
			  system("cls");

}//disp_all
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
