Data structure Problem | Josephus Circle in Data structure using C |The easiest way you never thought
In our programming website we teach computer coding to kids and pros for free online in easy way and gives knowledge about Data structure and alogorithm in C,Java,Python
and other programming languages that may help you land an online job and make your programmers day successfull.
We use best data structure notes and best programming resources. Here goes the article,dont forget to share with your friends 😃
Josephus Circle:
people are standing in a circle waiting to be executed.counting begins at a specified point in a circle and proceeds around the circle in a specified direction. After a specified number of people skipped,the next person is executed. The procedure is repeatedwith the remaining people,starting with the next person,going in the same direction and skipping the same number of people,untill only one person remains,and is freed.
The c program to implement this problem is given below:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void skipexecute(int,struct node *);
struct node *root=NULL,*p=NULL;
main()
{
int count=1,number,m;
struct node *temp;
printf("enter the value of number:\n"); //total number of elements in list
scanf("%d",&number);
printf("enter the value of m:\n"); // we have to remove m+1th element each time
scanf("%d",&m);
while(count<=number)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=count++;
if(root==NULL)
{
root=temp;
temp->link=root;
}
else
{
p=root;
while(p->link!=root)
p=p->link;
p->link=temp;
temp->link=root;
}
}
printf("List in circuler order:\n");
p=root;
while(p->link!=root)
{
printf("%d->",p->data);
p=p->link;
}
printf("%d",p->data);
printf("\n");
skipexecute(m,root);
}
void skipexecute(int m,struct node *front)
{
struct node *flag;
int count1=1;
while(front->link!=front)
{
count1=1;
while(count1!=m+1)
{
flag=front;
front=front->link;
count1++;
}
flag->next=front->link;
printf("%d->",front->data); //nodes which we have to delete
front=flag->link;
}
printf("\n");
printf(" who is freed is:%d\n",front->data);
return;
}
0 Comments