• About WordPress
    • WordPress.org
    • Documentation
    • Support
    • Feedback
  • Log In
  • Register
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment
Cuitutorial
  • Home
  • Courses
  • Past Paper
  • FYP
  • Interview Questions
  • University Events
  • Contact
  • Quiz & Assignment

Data Structure

Home » Blog » Josephus Problem in C++

Josephus Problem in C++

  • Posted by saqib
  • Categories Data Structure
  • Date November 11, 2022
  • Comments 0 comment

C++ Code for Josephus Problem

C++ Code for Josephus Problem

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

struct Josephus
{
char name[20];
struct Josephus *next;
};

typedef Josephus node;

node *New,*head = NULL,*ptr;

void create(char *n)
{
New = new node;
strcpy(New -> name,n);
if(head == NULL)
{
head = New;
head -> next = head;
ptr = head;
}

else
{
ptr -> next = New;
New -> next = head;
ptr = New;
}
}

node* remove(char *n)
{
node *temp = head,*deleted;
while(strcmp(temp -> next -> name,n) != 0)
temp = temp -> next;

deleted = temp -> next;
temp -> next = temp -> next -> next;
return deleted;
}

int main()
{

int m,n,i,j;
char nme[15],sname[15];
do
{
cout << “enter total person = “;
cin >> m;
if(m <= 0)
cout << “\nPlease choose number greater than ” << m << endl;
}while(m <= 0);

do
{
cout << “\nEnter Number = “;
cin >> n;
if(n <= 0)
cout << “\nPlease enter the number greater than ” << m;
}while(n <= 0);

for(i = 0;i < m;i++)
{
cout << “\nEnter the Name = “;
cin >> nme;
create(nme);
}

cout << “\n\n your Starting name With : “;
cin >> sname;

node *temp = head,*nd;
while(strcmp(temp -> next -> name,sname) != 0)
{
temp = temp -> next;
if(strcmp(temp -> name,””) == 0)
{
cout << “\nSorry, there is no person in the list “;
getch();
return 0;
}
}

cout << endl << endl;
for(int k = 0;k < m;k++)
{
j = 0;
for(i = 1;i <= n;i++)
{
temp = temp -> next;
j++;
if(strcmp(temp -> name,””) == 0)
{
j–;
i–;
}

if(j == n)
{
nd = remove(temp -> name);
if(k != m-1)
cout << “Eliminated person : ” << nd -> name << endl;

else
cout << “\to take Hourse and Eleminate is ” << nd -> name << endl;

break;
}
}
}

getch();
return 0;
}

Click on the link to download the presentation

Josephus Problem

Related links 

Single link list                 Stack              AVL Trees             Binary search          Counting Sort

Doubly link list               Queue              Graphs                  Bubble Sort               Radix sort

Circular link list              Binary search tree       Hashing         Insertion Sort         Bucket Sort

Josephus Problem          Tree Traversal              Heaps                Quick Sort              Merge Sort

 

At Cui tutorial, courses, past papers and final year projects

#tutorial #cui #pastpaper #courses

  • Share:
author avatar
saqib

Previous post

Doubly Link List
November 11, 2022

Next post

Stack Complete Code and Application
November 11, 2022

You may also like

Counting Sort
6 December, 2022

Counting sort is a type of Linear Time Sort Counting Sort was invented by H.H.Seward in 1954 All the sorting algorithms introduced so far share an                              …

Insertion Sort
6 December, 2022

Insertion Sort using loop and recursive function complete code On the ith pass we “insert” the ith element A[i] into its rightful place among A[1],A[2],…A[i-1] which were placed in sorted order. After this insertion A[1],A[2],…A[i] are in sorted order. Time …

Bubble Sort using C++
6 December, 2022

Bubble sort using loop and bubble sort in link list One of the simplest sorting methods. The basic idea is to move required value (smallest or highest ) to the top. Compare adjacent values (all elements) Swap values if required …

Leave A Reply Cancel reply

You must be logged in to post a comment.

admin@cuitutorial.com
Facebook-f Twitter Youtube Linkedin Instagram Stack-overflow Pinterest Github Quora Whatsapp
Courses
  • All Courses
  • Past Paper
  • Final year projects
  • Interview Questions
  • Contact
Important Pages
  • Privacy Policy
  • Terms of Service
  • Cookie Policy
Links
  • University Events
  • Team
Education & learning platform for All Computer science subjects
Final year projects
Past Paper
Interview questions
Programming, C/C++, Asp.net/MVC. Android, MySql, Jquery, Ajax, javascript, Php, Html5, Bootstrap4.
NTS, GAT, PPSC, FPSC

Copyright © 2021 | Cuitutorial