# Solved Problem using C Language (SUPPOSE IN A SHIP THERE ARE 30 PERSONS AND 15 LIFE JACKETS.ONLY THESE LIFE JACKETS CAN SAVE THE LIFE OF PERSONS.WE HAVE TO SET THE LOOP IN SUCH A WAY THAT ONLY CAPTAIN’S 15’S EMPLOYEES GET LIFE JACKETS AND SAVE THEIR LIFE)

/* SUPPOSE IN A SHIP THERE ARE 30 PERSONS AND 15 LIFE JACKETS.ONLY THESE LIFE
JACKETS CAN SAVE THE LIFE OF PERSONS.WE HAVE TO SET THE LOOP IN SUCH A WAY
THAT ONLY CAPTAIN’S 15’S EMPLOYEES GET LIFE JACKETS AND SAVE THEIR LIFE. */

##### #include<conio.h>

/ * DECLARE SELF-REFRENTIAL STRUCTURE * /

{
int info;

void linked(); //DECLARE PROTOTYPE OF THE FUNCTION
int index; //DECLARE VARIABLE GLOBALLY

/ * MAIN FUNCTION START HERE * /

void main()
{
int jackets=0,temp,loop=1; //DECLARE VARIAIBLE LOCALLY

``````linked();                      //CALL LINKED FUNCTION FOR

/ * WHILE LOOP EXECUTE UNTILL JACKETS NOT EQUAL TO 15***/

``````while(jackets!=15)
{
temp=0;               //SET TEMP VARIABLE TO ZERO

while(temp!=loop)     //WHILE LOOP FOR MOVE POINTER TO
//TO DESIRED LOOP
{
save=ptr;      //SAVE POINTER TO KEEP TRACE
//PREVIOUS NODE
ptr=ptr->next;     //INCREMENT THE POINTER
temp++;
}
if(ptr->info==1)    //IF CONDITION FOR COMPARISON
{
save->next=ptr->next;    //DELETE THE REQURED NODE
save=ptr;             //SET POINTER TO SAVE
ptr=ptr->next;      //INCREMENT THE POINTER
jackets++;          //COUNT TH NO. OF JACKETS
}
else               //IF LOOP IS WRONG THEN ELSE PART
//WILL EXECUTE
{
jackets=0;         //SET JACKTES TO ZERO
loop++;            //INCREMENT THE LOOP
}
}``````

printf(“\nSO THE CAPTAIN SHOULD START COUNTING FROM %d ,”,loop+1);
printf(“ADD CIRCULARLY AND GIVE LIFE JACKLET TO THAT PERSON “);
printf(“BECAUSE %d IS ONLY NUMBER WITH IT HE CAN GIVE JACKET”,loop+1);
printf(” TO ONLY HIS EMPLOYEE”);
printf(“\n\nLOOP IS :%d”,loop+1);
getch();
}

/ * DEFINE THE LINKED FUNCTION*/

{

``````/*<<<<<<<<<<<<<<<<<<INITIALISE THE ARRY>>>>>>>>>>>>>>>>>>>*/

int a[30]={0,0,0,0,0,1,1,1,1,0,0,1,1,0,1,0,0,1,0,1,0,1,1,0,1,1,0,0,1,1};

clrscr();
start=NULL;       //SET NULL TO START``````

/ * <<<<>>>>>>> * /

``````for(index=0;index<30;index++)
{``````

/ * <<<<<<<<<<<<<>>>>>>>>>>>>>>>>> * /

``````    struct linked_list *ptr;

/ * <<<<<<<<<<>>>>>>> * /

``````    if(start==NULL)
{
ptr->info=a[index];    //ASSIGN THE ELEMENT TO POINTER
start=ptr;             //SET START TO POINTER
ptr->next=start;       //SET NODE'NEXT TO START
node=ptr;              //SET NODE TO POINTER
}``````

/ * <<<<<<<<<<>>>>>>> * /

``````    else
{
ptr->info=a[index];       //ASSIGN THE ELEMENT TO POINTER
ptr->next=start;              //SET NODE'NEXT TO START
node->next=ptr;              //SET NODE'NEXT TO POINTER
node=ptr;                    //SET NODE TO POINTER
}
}
ptr=start;                    //SET PTR TO POINTER``````

/ * <<<<<<<<<<<<<<>>>>>>>>>>>>> * /

``````printf("SUPPOSE IN A SHIP THERE ARE 30 PESONS AND 15 LIFE JACKETS.");
printf("ONLY THESE LIFE JACKETS CAN SAVE THE LIFE OF PERSONS.");
printf("WE HAVE TO SET THE LOOP IN SUCH A WAY THAT ONLY CAPTAIN'S");
printf(" 15'S EMPLOYEES GET LIFE JACKETS AND SAVE THEIR LIFE. ");
printf("\n\nPERSONS STANDING IN POSTION AS SHOWN BELOW :");
printf("\n\n'1' SHOW THE EMPLOYEES OF THE CAPTAIN");
printf("\n\n'0' SHOW THE EMPLOYEES OF THE OTHER\n\n");
do
{
printf("%d",ptr->info);
printf("\t");
ptr=ptr->next;           //INCREMENT THE POINTER
}while(ptr!=start);``````

}

This site uses Akismet to reduce spam. Learn how your comment data is processed.