Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked list issue - loop iterates over wrong nodes
    primarykey
    data
    text
    <p>If you are not familiar with Josephus problem:</p> <p>There are N soldiers standing in a circle.They all get executed starting by 1 and moving by M.In the end only one of them stands alive. the code below ask for N and M and generated the last player standing.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main() { int N, M; struct node { int player_id; struct node *next; } struct node *p, *q; int i, count; printf("Enter N (number of players): "); scanf("%d", &amp;N); printf("Enter M (every M-th payer gets eliminated): "); scanf("%d", &amp;M); // Create circular linked list containing all the players: p = q = malloc(sizeof(struct node)); p-&gt;player_id = 1; for (i = 2; i &lt;= N; ++i) { p-&gt;next = malloc(sizeof(struct node)); p = p-&gt;next; p-&gt;player_id = i; } p-&gt;next = q;// Close the circular linkedlist by having the last node point to the 1st // Eliminate every M-th player as long as more than one player remains: for (count = N; count &gt; 1; --count) { for (i = 0; i &lt; M - 1; ++i) p = p-&gt;next; p-&gt;next = p-&gt;next-&gt;next; // Remove the eiminated player from the circular linkedl } printf("Last player left standing is %d\n.", p-&gt;player_id); return 0; } </code></pre> <p>Lets say N=17;M=7 the output should be 13 the program above generates 2 (what happens? well it starts the counting from M not from 1 so instead of 1,8,15...... it elimintates 7,14......) This is where i need your help (as linked lists are still a difficult concept for me). How to modify this one? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload