06
May
Josephus Problem in C
Related Blog Items
- When to use pointer, when to use reference?
- Different signature problem with extern variables
- How Bus Error occurs
- IEEE 754 Binary Floating Point Representation
- Bad Standard APIs
Q:Josephus Flavius was a famous Jewish historian of the first century at the time of the Second Temple destruction. During the Jewish-Roman war he got trapped in a cave with a group of 40 soldiers surrounded by romans. The legend has it that preferring suicide to capture, the Jews decided to form a circle and, proceeding around it, to kill every third remaining person until no one was left. Josephus, not keen to die, quickly found the safe spot in the circle and thus stayed alive.
Requirements:
- Circular linked list must be used.
- The sequence starts from 1, therefore you won’t see a 0 appear in the sequence.The number of people and number of skips must be larger than 0
(>0).
Program:
1fd8c682f6738ab78467e3c94278d881000
void display() { if (head == NULL) { printf("\nNo Soilders in the Queue"); return; } printf("%d",head->soilder); printf("%c ",2); current = head->next; while( current != head) { printf("%d";,current->soilder); printf("%c ",2); current = current->next; } return; } q *tail() { q *temp; current = head->next; while (current != head) { temp = current; current = current->next; } return(temp); } int left_after_sucide(int by_n) { int i=1,j,dead_sol; current = head; save = tail(); while (i<tot_soilders) { for (j=1;j<by_n;j++) { save = current; current = current->next; } save->next = current->next; if (current == head) head = current->next; dead_sol = current->soilder; free(current); display(); printf("\n\n%d%c is Dead \n%c RIP",dead_sol,1,5); getch(); current = save->next; i++; } head = current; display(); tot_soilders = 1; return(head->soilder); }
main() { int ch,n; head = NULL; do { printf("\n1. For soilder list creation"); printf("\n2. For Displaying soilder list"); printf("\n3. For Sucide"); printf("\n0. For Exit"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the total no. of soilders"); scanf("%d",&n); create_list(n); break; case 2: display(); getch(); break; case 3: if (tot_soilders <= 1) printf("There Should Be Atleast 2 Soilders in the List"); else { printf("\nEnter the no by which sucide is to be commited"); scanf("%d",&n); if (n<1) printf("\nInvalid Number!"); else printf("\nThe only Soilder left after " "sucide session is %d%c",left_after_sucide(n),2); } getch(); break; case 0: return; default : printf("\nINVALID CHOICE"); getch(); } } while (ch!=0); freelist(); }
download entire file in C source code…
Popularity: 17%
You need to log on to convert this article into PDF
Related Blog Items - When to use pointer, when to use reference?
- Different signature problem with extern variables
- How Bus Error occurs
- IEEE 754 Binary Floating Point Representation
- Bad Standard APIs
Related Blog Items
- When to use pointer, when to use reference?
- Different signature problem with extern variables
- How Bus Error occurs
- IEEE 754 Binary Floating Point Representation
- Bad Standard APIs
good implementation of circular linked list
May 21st, 2007 at 11:31 am