X-Kingdom has trapped n number of soldiers of their opponent. They want to execute them. They created a strategy so that the prisoners will kill each other and at the end one prisoner will be alive and eventually released. As part of the process, they assigned each trapped soldier a sequence number starting from 1 and ending at n.  If n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So, the person at position 3 survives. If n = 7 and k = 3, then the safe position is 4. The people at positions 3, 6, 2, 7, 5, 1 are killed in order, and the person at position 4 survives. Input:n and kOutput:Print the list of prisoners in reverse order from n to 1Then reverse the list to print it in correct order from 1 to nDisplay the position number who will survive.You must have to use circular doubly linked list for your solution. You need to declare appropriate doubly linked list node structure to store a soldier with sequence number as the data value.Functions needed;a.) soldier* create_soldier (int sequence): It takes an integer, dynamically allocate a soldier structure and returns a soldier nodeb.) soldier* create_reverse_circle(int n): It takes an integer and creates a circular doubly linked list with n number of soldiers placed in descending sequence. For example, if n=5 it should produce a circular doubly linked list starting from 5 and ending at 1 as sequence number. After creating the circle, it returns the head of the circular linked list.c.) soldier* rearrange_circle(soldier* head): This function reverse a given circular doubly linked list where head is the head pointer. After reversing the linked list, it returns the head pointer of the updated linked listd.) void display(soldier* head): This function displays a given circular doubly linked list.. head is head pointer of the linked liste.) int kill(soldier* head, int n, int k): This function takes head of the linked list, number of soldiers n, and skip value k as parameter and returns the sequence number of the surviving soldier. Note that in addition to the above functions, you will utilize other linked list functions for insertion, deletion, and you may create more function as you need for your solution.

Question
Asked Oct 20, 2019
684 views

X-Kingdom has trapped n number of soldiers of their opponent. They want to execute them. They created a strategy so that the prisoners will kill each other and at the end one prisoner will be alive and eventually released. As part of the process, they assigned each trapped soldier a sequence number starting from 1 and ending at n. 

 

If n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So, the person at position 3 survives. If n = 7 and k = 3, then the safe position is 4. The people at positions 3, 6, 2, 7, 5, 1 are killed in order, and the person at position 4 survives.

 

Input:
n and k


Output:
Print the list of prisoners in reverse order from n to 1
Then reverse the list to print it in correct order from 1 to n
Display the position number who will survive.

You must have to use circular doubly linked list for your solution. You need to declare appropriate doubly linked list node structure to store a soldier with sequence number as the data value.

Functions needed;

a.) soldier* create_soldier (int sequence): It takes an integer, dynamically allocate a soldier structure and returns a soldier node


b.) soldier* create_reverse_circle(int n): It takes an integer and creates a circular doubly linked list with n number of soldiers placed in descending sequence. For example, if n=5 it should produce a circular doubly linked list starting from 5 and ending at 1 as sequence number. After creating the circle, it returns the head of the circular linked list.


c.) soldier* rearrange_circle(soldier* head): This function reverse a given circular doubly linked list where head is the head pointer. After reversing the linked list, it returns the head pointer of the updated linked list


d.) void display(soldier* head): This function displays a given circular doubly linked list.. head is head pointer of the linked list


e.) int kill(soldier* head, int n, int k): This function takes head of the linked list, number of soldiers n, and skip value k as parameter and returns the sequence number of the surviving soldier. 


Note that in addition to the above functions, you will utilize other linked list functions for insertion, deletion, and you may create more function as you need for your solution.

check_circle

Expert Answer

star
star
star
star
star
1 Rating
Step 1

C Program:

// Include the header files

#include<stdio.h>

#include<stdlib.h>

// Declare and initialize the required variables

int actu_cnt=0;

// Declare the structure variable

typedef struct soldier

{

     struct soldier *prev;

     struct soldier *next;

     int data;

}

soldier;

soldier *head,*lastnode;

// Function declaration

soldier* create_reverse_circle(int n);

soldier* create_soldier(int seq);

soldier* rarrange_circle(soldier *head);

void display(soldier * head);

int kill(soldier *head,int n,int k);

// Main function

void main ()

{

     // Pass the values to the function

     create_reverse_circle(7);

     display(head);

     rarrange_circle(head);

     display(head);

     int safe=kill(head,7,3);

     printf("\nSafe soldier is: %d \n",safe);

     display(head);

}

//creating linked list in reverse order in very first time

//by inserting node at the beginning of the existing linked list

soldier* create_reverse_circle(int n)

{

     for(int i=1;i<=n;i++)

     {

          create_soldier(i);

     }

     printf("\n %d soldier/s inserted\n",n);

     return head;

}

// Define a function that dynamically allocate a soldier structure and returns a soldier node

soldier* create_soldier(int item)

{

     struct soldier *ptr,*temp;  

     // int item;

     ptr = (struct soldier *)malloc(sizeof(struct soldier));

     if(ptr == NULL)

     {

          printf("\nOVERFLOW");

     }

     else

     {

          ptr->data=item;

          if(head==NULL)

          {

              head = ptr;

              ptr -> next = head;  

              ptr -> prev = head;

              lastnode=head;

          }

          else  

          {

              temp = head;  

              while(temp -> next != head)

              {

                   temp = temp -> next;  

              }

              temp -> next = ptr;

              ptr -> prev = temp;

              head -> prev = ptr;

              ptr -> next = head;

              head = ptr;

          }

          actu_cnt++;

          return head;

     }

}

// Define the function that reverse a given circular doubly linked list where head is the head pointer. After reversing the linked list, it returns the head pointer of the updated linked list

soldier* rarrange_circle(soldier *head)

{

     soldier *tmp_h, *tmp_l;

     tmp_h=head;

     tmp_l=lastnode;

     int val;

     for(int i=0;i<actu_cnt/2;i++)

     {

          val=tmp_h-&...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: How do i solve this problem in python? ( has 3 steps)

A: NOTE:The given information is incomplete (Formula to calculate the minutes to walk or run for a give...

question_answer

Q: Hello good afternoon,   Use functional decomposition to write a C++ program that asks the user to en...

A: create three methods with void return type as well as declare input variables as a global variable, ...

question_answer

Q: write a program in c++. You work for a box company, and have been asked to write a program to compar...

A: Note:The C++ program is written as per the given requirements.The code is well commented for better ...

question_answer

Q: Draw a flowchart for a program that shows the logic for a program that generates a random number.  T...

A: The following program will generate a random number 0 or 1. If the generated random number is 0, the...

question_answer

Q: Write a program that will present the user with a main menu of options which the user can choose fro...

A: Program:import java.util.Scanner;public class Main {publicstaticvoid main(String[] args) {char user_...

question_answer

Q: Write a simple polling program that allows users to rate five topics from 1 (least important) to 10 ...

A: Pogram code #1:

question_answer

Q: This program is written in pseudocode."Design a program that generates a 7-ddigit lottery number. Th...

A: This pseudocode explains how to create a 7-digit lottery number. These 7 digits are randomly generat...

question_answer

Q: Use the Design Recipe to write a function called running_average that repeatedly asks the user to in...

A: DESIGN RECIPE:Based on the given problem use meaningful variable and function name. And perform the ...

question_answer

Q: using the web resarch Mafiaboy's exploits. when and how did he comprmise sites? How was he caught?

A: Mafiaboy was arrested by the FBI, who was surveilling him, after he claimed to have brought down the...