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 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 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.

Question

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.

Expert Answer

1 Rating

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Related Computer Science Q&A

Find answers to questions asked by students like you.

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...

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, ...

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 ...

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...

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_...

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

A: Pogram code #1:

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...

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 ...

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...

Q: Using the initial dependency diagram (attached), remove all partial dependencies, draw the new depen...

A: According to the given dependency diagram, there is no partial dependency but there are transitive d...

Q: java write a app that prompts the user for the number of miles and outputs the conversion to kilomet...

A: Program DescriptionA java JavaX application is written which accepts user input in miles and display...

Q: In C++ Create an array of random 10 random numbers. Use the "range-based for loop" (sometimes called...

A: Program designuse a reference type loop variable in the range-based for loop to assign elements to a...

Q: How can I fix that Traceback (most recent call last):  File "C:\Users\Richard Seegerer\Documents\Pyt...

A: Here you are trying to open a file name module.txt in reading only format. You are utilizing a relat...

Q: Given three int variables that have been given values, areaCode, exchange, and lastFour, write a str...

A: As you have not specified the programming language, we are solving this question using C++ programmi...

Q: in python Write a program that creates a list of your friends. Start with an empty list and then us...

A: List:  A list in python is a data structure which stores data or items of various data types. A list...

Q: I am getting a floating point exception in my code. Which is to divide matrix 1 by matrix 2 (striaig...

A: Program Instructions:A floating-point exception occurs when an attempt is made to divide a number by...

Q: I need help tackling this program:     Write a C++ console application that allows your user to ente...

A: For solving this problem following variables and data type are used to execute this file :1.Variable...

Q: In python, write a program to find the prime factors of a number given by the user.

A: Program Instructions:Import the math module to use the sqrt() function.Take user input in a variable...

Q: D

A: D) The language of the DFA is R(3) 13 . By using the values from question C, we can write the regula...

Q: Write an application starting with ProjectTemplate. Name the class AgeCalcApp. The app needs to prom...

A: To write an application name ProjectTemplate with the class name AgeCalcApp. This application prompt...

Q: Programming Question: Write a program in MIPS assembly language that asks the users to enter a value...

A:  Code which is written in MIPS assembly language is shown below-

Q: in python 2.2Write a program that prompts the user for two inputs: the unit cost of an item and the ...

A: round() function is used to round the number.Syntax of round function:round(number [, ndigit])number...

Q: Databse SQL Queries. Need to write the SQL code for pulling the total cost of our inventory. We have...

A: A SQL query is to be written that will give the total cost of inventory. Two tables are given named ...

Q: If you are not able to specify the output of the above program exactly, explain why.

A: The sample output of the given program is:

Q: Convert the following machine language instructions into assembly language, EXPLAIN ALL STEPS, assum...

A: 82B7DE:The given machine language instruction is 82B7DE.Its equivalent binary representation is 1000...

Q: In a file called RemoveVowels.java, write a program that: Asks the user to enter a word of text. Pr...

A:  First of all to import java.util.scanner object to use Scanner class. In java, you need to use the ...

Q: In C++ 1.  Create function called DoubleFillArray that takes in an empty array of size 100 and fills...

A: Program Instructions:In function DoubleFillArray, use rand() method to fill random numbers with doub...

Q: I need this for my programming lab. It’s python.   Given three String Variables that have Been given...

A: String in python are arrays of bytes representing Unicode characters. however, Python does not have ...

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Program AlgorithmStartCreate a java project named AtmSimulator and then create a class named as AtmS...

Q: Arrays Write three statements to print the first three elements of array runTimes. Follow each state...

A: Note: As the user given index value is constant and so the value of index will not be changed while ...

Q: hello good afternoon,   please, I need help with the following problem c++: ASSIGNMENT (CONTROL STRU...

A: A C++  program to display the song time and total time in a table format and a message indicating re...

Q: in python 1. Write a program named filemaker.py that will be used to store the first name and age of...

A: Program code:

Q: Python 3.7.4: Write a loop that reads positive integers from standard input and that terminates when...

A: Following is the program code that takes input from the user all positive integer and terminates if ...

Q: Form boolean equations in POS and SOP forms from the truth table attached (PLEASE EXPLAIN ALL STEPS)...

A: The Boolean expression from the truth table can be converted into SOP form by:In the final equation ...

Q: How would I do this program? I am stuck.

A: Program DesignPrompt user for inputs and use Scanner object to read the inputs into double variables...

Q: given 0011 0000 0000 0000 0000 0000 1111 0011 what is the answer when multipy by 2

A: Multiplying a binary number with 2 means performing left shift operation. The given binary number 00...

Q: Python 3.7.4 Use the file object output to write the string "3.14159" to a file called pi.

A: Following is the program code which writes the string "3.14159" to a file called pi.To open a file c...

Q: Which lines of code above trap into the operating system? list the letters identifying the lines. Wh...

A: Line H, K, N and O are trap related lines as they interact with OS directly for I/O. These are synch...

Q: PLEASE EXPLAIN ALL STEPSUsing the boolean equation (x + ~xy) XOR ~xz Create a circuit diagram Creat...

A: We’ll answer the first three subparts of the question since the exact one wasn’t specified. Please s...

Q: Python 3.7.4 Write the code to call the function named send_signal. There are no parameters for this...

A: To call the given function without parameter we need to write it as send_signal(). The following is ...

Q: EXPLAIN ALL STEPS: Convert the following machine language instructions into assembly language, assum...

A: a.82B7DEConvert the above machine language into binary format1000 0010 1011 0111 1101 11101000 raaaT...

Q: In C++ Create a function called CopyAndInsertArray.  It should take in two arrays (the first array s...

A: Program designThe overall program design is as follows:The function CopyAndInsertArray would accept ...

Q: This program is written in pseudocode."Design a program that asks the user to enter a store's sales ...

A: Program code #1:

Q: program6_1.pyWrite a program that the owner of Chica Chic could use to store data about her inventor...

A: Since you have not provided the sample text file for this problem, we have created a text file inven...

Q: I am completely stumped on this homework question and cannot figure out how to make the range count ...

A: Program Instructions:Use -1 to decrement down the count.The text can be made 5 characters wide by us...

Q: Please put into SOP form~xyz + yz + x~y

A: It is said that a Boolean expression consisting solely of Minterms (product terms) is in the form of...

Q: How would I go about writing this program?

A: Create a class and get int type data from a user, to print the number of lines on console.Use three ...

Q: In C++  1. Create function called DoubleFillArray that takes in an empty array of size 100 and fills...

A: Program Instructions:In function DoubleFillArray, use rand() method to fill random numbers with doub...

Q: This is my code for an MIPS assignment where im taking numbers into an array and reversing within th...

A: Note:For the given code and attached image, the following explanation is briefly described about the...

Q: Using the types of entities found in the Colonial Adventure Tours database (trips, guides, clients, ...

A: The Set of entities provided for Colonial Adventure Tours Database includes Trips, Guides, Clients a...