Respuesta :
Answer:
C code explained below
Explanation:
#include <stdio.h>
#include <stdlib.h>
// Define a structure
typedef struct soldier{
struct soldier* prev;
struct soldier* next;
int data;
}soldier;
// Initialize count variable
int count=0;
// Define necessary variable
soldier *head, *lastnode;
// Define the necessary methods
soldier* create_soldier(int sequence);
soldier* create_reverse_circle(int n);
soldier* rearrange_circle(soldier* head);
void display(soldier* head);
int kill(soldier* head, int n, int k);
int main()
{
// Define two int variables
int n, k;
// Prompt user to enter number of soldiers
printf("Enter the number of soldiers: \n");
scanf("%d",&n);
// Prompt user to enter skips
printf("Enter number of skip: \n");
scanf("%d",&k);
// Display list of prisoners from n to 1
printf("Reverse Circle:\n");
// Call display() method with create_reverse_circle() method as its parameter
display(create_reverse_circle(n));
// Display the reverse list from 1 to n
printf("Rearranged circle:\n");
// Call display() method with rearrange_circle() method as its parameter
display(rearrange_circle(head));
// Display the soldier that survives
// Call the kill() method
printf("The soldier that survives: %d \n",kill(head, n, k));
}
// Define the create_soldier() method
soldier* create_soldier(int sequence)
{
// Declare necessary pointers
struct soldier* ptr,*temp;
// Dynamically allocate memory
ptr=(struct soldier*)malloc(sizeof(soldier));
// Check if pointer is null
if(ptr==NULL)
{
// Print overflow
printf("OVERFLOW");
}
else
{
// Assign ptr->data equal to sequence
ptr->data=sequence;
// Check if head is null
if(head==NULL)
{
// Assign head equal to ptr
head=ptr;
// Assign head to ptr->next
ptr->next=head;
// Assign head to ptr->prev
ptr->prev=head;
// Assign head to lastnode
// To keep track of the lastnode
lastnode=head;
}
else
{
// Assign head to temp
temp=head;
while(temp->next!=head)
{
// Assign temp->next to temp
temp=temp->next;
}
// Assign ptr to temp->next
temp->next=ptr;
// Assign temp to ptr->prev
ptr->prev=temp;
// Assign ptr to head->prev
head->prev=ptr;
// Assign head to ptr->next
ptr->next=head;
// Assign head equal to ptr
head=ptr;
}
// Increment count
count++;
}
// Return head
return head;
}
// Define the create_reverse_circle() method
soldier* create_reverse_circle(int n)
{
for(int i=1; i<=n; i++)
{
// Call the create_soldier() method
create_soldier(i);
}
// Return head
return head;
}
// Define the rearrange_circle() method
soldier* rearrange_circle(soldier* head)
{
// Declare the necessary pointers
soldier *temp_head, *temp_last;
// Assign temp_head equal to head
temp_head=head;
// Assign temp_last equal to lastnode
temp_last=lastnode;
// Declare necessary int variables
int value;
// Start a for loop to rearrange the list
for(int i=0; i<(count/2);i++)
{
// Assign temp_head->data to value
value=temp_head->data;
// Assign temp_last->data to temp_head
temp_head->data=temp_last->data;
// Assign value to temp_last->data
temp_last->data=value;
// Assign temp_head equal to next element
temp_head=temp_head->next;
// Assign temp_last equal to previous element
temp_last=temp_last->prev;
}
// Return head
return head;
}
// Define the display() method
void display(soldier* head)
{
// Declare the necessary variable
soldier* start;
// Assign start equal to head
start=head;
// Check if head is null
if(head==NULL)
{
// Print no values
printf("\n no values present! \n");
}
while(start->next!=head)
{
// Display start->data
printf("%d", start->data);
printf("->");
// Assign start equal to start->next
start=start->next;
}
// Print the last element
printf("%d \t \n",start->data);
}
// Define the kill() method
int kill(soldier* head, int n, int k)
{
// Declare the necessary pointer
struct soldier *ptr, *t;
// Initialize trace equal to zero
int trace=1;
// Assign ptr equal to head
ptr=head;
// Assign t equal to head
t=head;
// Initialize counter equal to 1
int counter=1;
while(counter<n)
{
// Check if head equal to null
if(head==NULL)
{
// Print underflow
printf("UNDERFLOW");
break;
}
else
{
trace=1;
while(trace<k)
{
// Assign ptr equal to t
ptr=t;
// Assign t equal to next element
t=t->next;
// Increment trace
trace++;
}
// Assign t->next to ptr->next
ptr->next=t->next;
// Assign ptr t->next->prev
t->next->prev=ptr;
free(t);
// Assign ptr->next to t
t=ptr->next;
}
// Increment counter
counter++;
}
// Assign head->data equal to ptr->data
head->data=ptr->data;
// Assign head->next equal to head
head->next=head;
// Assign head->prev equal to head
head->prev=head;
// Return head->data
return (head->data);
}