Question

Need different C code then what has been posted here Problem In this assignment, you have...

Need different C code then what has been posted here

Problem

In this assignment, you have to simulate the Josephus problem. There are n number of prisoners standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.

Example

For example, 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 persons at positions 3, 6, 2, 7, 5, 1 are killed in order, and person at position 4 survives.

Input: n and k

Output:

The position number who will survive.

Requirement:

You must use the concept of circular queue while inserting and it should be implemented using circular doubly linked list. You don’t need to use front and rear as you are using linked list. Root and last node automatically handle that.

Hint: After getting the value of n, generate n numbers (1, 2, 3., …, n) and insert into the doubly circular linked list. Then start deleting nodes based on the value of k until the list has only one node remaining.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* this code is just for your reference and it is working on Circulary linked list concept

*/

#include <stdio.h>
#include <stdlib.h>

struct node // create the linked list
{
int num;
struct node *next;
struct node *prev;
};

void create(struct node **); // it will create the linked list
int survivor(struct node **, int); // it will get the index of survivor

int main()
{
struct node *head = NULL;
int survive, skip;

create(&head); // go to the creation of the linked list
printf("Enter the number of persons to be skipped: "); // the number of persons(k) which we have to skip
scanf("%d", &skip);
survive = survivor(&head, skip); // will return the index of survivour
printf("The person to survive is : %d\n", survive); // print out the name
free(head); // free all memory whatever we store in malloc

return 0;
}

int survivor(struct node **head, int k)
{
struct node *p, *q;
int i;

q = p = *head;
while (p->next != p)
{
for (i = 0; i < k - 1; i++) // process till (k-1) person
{
q = p;
p = p->next;
}
q->next = p->next;   
free(p); // free or execute the k person
p = q->next; // linked list will store next address
}
*head = p;

return (p->num);
}

void create (struct node **head)
{
struct node *temp, *rear;
int n, k;
printf("Enter a number of prisoners : "); // number of prisoners as n
scanf("%d", &n);
for(int i=1;i<=n;i++){ // run for loop till n time to create the linked list of n persons
temp = (struct node *)malloc(sizeof(struct node));
temp->num = i;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
}
rear->next = *head;
}

Add a comment
Know the answer?
Add Answer to:
Need different C code then what has been posted here Problem In this assignment, you have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical prob- lem...

    In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical prob- lem related to a certain counting-out game.) People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a spec- ified number of people are skipped, the next person is executed. The procedure is repeated with the remain- ing people, starting with the next person, going in...

  • C++ void CLL::push(char data) { // You write - if the size is 0, add a...

    C++ void CLL::push(char data) { // You write - if the size is 0, add a first node in the linked list // by calling addFirst. Otherwise add a new node to the end of the linked list. Note that this // linked list is circular, so you must make the last node's next pointer point to the first node. } void CLL::addFirst(char data) { // you write - add the very first node to the linked list } void...

  • The source code I have is what I'm trying to fix for the assignment at the...

    The source code I have is what I'm trying to fix for the assignment at the bottom. Source code: //Group 1 - Jodie Butterworth, Brandon Kidd, Matt Heckler //11-21-19 //CSC 201 // Exercise to practice the basic manipulations of a linked list // Note: Uses nullptr, so need to make sure your compiler is set to use C++11 // In Code::Blocks this is under Settings==>Compiler #include <iostream> using namespace std; // Structure to contain data for a node (Could be...

  • C++ Assume that you have a structure Node as follows: struct Node { int value; Node*...

    C++ Assume that you have a structure Node as follows: struct Node { int value; Node* next; Node(int v, Node* n = nullptr) { // constructor value = v; next = n; } }; Question 1 (20 points) Write a function "count" that counts the number of elements in a given linked list. The prototype of this function is as follows: int count_elements(Node*); Question 2 (20 points) Write a function “average" that calculates the average of all the elements in...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignmet you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT