/////////////// DUOBLE LINK LIST OPERATIONS ///////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//-------------------------- >>> a structuer <<<
typedef struct
{
	int id;
	float gpa;
}data;    
//-------------------------->>> a defanition of a duoble link list <<<
typedef struct node
{
	data d;
	struct node *next;
	struct node *prev;
}recnode, *ptr;
//-------------------------->>> insert first in a duoble link list <<<
void ins_first(ptr *h,data newe)
{
	ptr p;
	p=(ptr) malloc(sizeof(recnode));
	p->d = newe;
	p->prev=NULL;

		if(*h==NULL)
		{
			p->next=NULL;
			(*h)=p;
		}
		else
		{
			p->next=*h;
			(*h)->prev=p;
			(*h)=p;
		}

}
//-------------------------->>> insert last in a duoble link list <<<
void ins_last(ptr *h,data newe) 
{
	
	ptr p,q;
	p=(ptr)malloc(sizeof(recnode));
	p->d = newe;
	p->next=NULL;
		if((*h)==NULL)
		{
			(*h)=p;
			(*h)->prev=NULL;
		}
		else
		{
			q=*h;
			while(q->next!=NULL)
				q=q->next;
			q->next=p;
			p->prev=q;
		}
}
//-------------------------->>> new link <<<
void new_D(ptr *h)   
{
	*h=NULL;
}
//-------------------------->>> sertch for an element <<<
void sertch(ptr h,int t,ptr *cur) 
{

	*cur=NULL;

		while(h!=NULL && *cur==NULL)
		{
			if(h->d.id==t)
				*cur=h;
			else h=h->next;
		}
}
//-------------------------->>> delete first <<<
void del_first(ptr *h,data *z)  
{
	ptr p;

		if(*h==NULL)printf("\nSory no list...");
		else
		{
			if((*h)->next==NULL)
			{
				*z = (*h)->d;
				free(*h);
				(*h) = NULL;
			}
			else
			{
				p=*h;
				*z=p->d;
				(*h)=(*h)->next;
				(*h)->prev=NULL;
				free(p);
			}
		}
}
//-------------------------->>> delete last <<<
void del_last(ptr *h,data *z) 
{
	ptr p;
	
		if((*h)==NULL) printf("\nSory no list...");
		else
		{
			if((*h)->next==NULL)
			{
				*z=(*h)->d;
				free(*h);
				*h = NULL;
			}
			else
			{
				p=*h;
				while(p->next!=NULL)
					p=p->next;
				*z=p->d;
				p->prev->next=NULL;
				free(p);
			}
		}
}
//-------------------------->>> delete target <<<
void del_target(ptr *h, int t, data *z) 
{
	ptr cur;
		sertch(*h,t,&cur);
		if(cur==NULL)  printf("\nSory target not found...");
		else
		{
			if(cur==(*h))
				del_first(h,z);
			else
			{
				if(cur->next==NULL)
					del_last(h,z);
				else
				{
					*z=cur->d;
					cur->prev->next=cur->next;
					cur->next->prev=cur->prev;
					free(cur);
				}
			}
		}
}
//-------------------------->>> read recored <<<
void read_rec(data *r)  
{
	printf("\nEnter the ID:");
	scanf("%d",&r->id);
	printf("\nEnter the GPA:");
	scanf("%f",&r->gpa);
}
//-------------------------->>> display the link <<<
void display(ptr h) 
{
	printf("\n-------------------------------");
	printf("\nID                        GPA  ");
	printf("\n-------------------------------");
		while(h!=NULL)
		{
			printf("\n%d\t\t\t%.2f",h->d.id,h->d.gpa);
			h=h->next;
		}
}
//------------------------------------------
int Num(ptr h)
{
	int i;
	i = 0 ;
	while( h != NULL )
	{
	   i++;
	   h = h->next;
	}

	return i;
}
//////////////////////////////////////////////////////////////////////////
//>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN PROGRAMM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//////////////////////////////////////////////////////////////////////////
main()  
{
	int c,t;
	data r;
	ptr h,cur;
	new_D(&h);

	do{ 
	printf("\n\n\n\n              +-------------------------------------------------+");
	printf("\n              |(1) insret first.                                |");
	printf("\n              |                                                 |");
	printf("\n              |(2) insert last.                                 |");
	printf("\n              |                                                 |");
	printf("\n              |(3) delete first.                                |");
	printf("\n              |                                                 |");
	printf("\n              |(4) delete last.                                 |");
	printf("\n              |                                                 |");
	printf("\n              |(5) delete a gevin id.                           |");
	printf("\n              |                                                 |");
	printf("\n              |(6) sertch for ID.                               |");
	printf("\n              |                                                 |");
	printf("\n              |(7) deisplay the list.                           |");
	printf("\n              |                                                 |");
	printf("\n              |(8) Exit.                                        |");
	printf("\n              +-------------------------------------------------+");
	printf("%d", Num(h));
	scanf("%d",&c);

	
		
			switch(c)
			{
			    	
					case 1:read_rec(&r);
						   ins_first(&h,r);system("cls"); 
						break;
					case 2:read_rec(&r);
						   ins_last(&h,r);system("cls"); 
						break;
					case 3:del_first(&h,&r); 
						break;
					case 4:del_last(&h,&r); 
						break;
					case 5:printf("\nEnter the ID:");
						   scanf("%d",&t);
						   del_target(&h,t,&r);
						break;
					case 6:printf("\nEnter the ID:"); scanf("%d",&t);
						sertch(h,t,&cur);
						if(cur==NULL)printf("\nno such ID...");
						else
						printf("\nID:%d\t\t\tGPA:%f",cur->d.id,cur->d.gpa);
						break;
					case 7:display(h);
						break;
					default:printf("\nNO SUTCH CHOIS....TRY AGEN...");
			}
		}while(c!=8);
		system("cls");



		//printf("%d",Num(h));
		printf("%d          THANK YOU FOR USEING MY PROGRAMM...",Num(h));
		getch();
}
