Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct circle
- {
- int id;
- circle *next;
- };
- struct circle *ptr,*node,*start,*prev,*temp;
- int main()
- {
- int n,k,elimination=0,counter=0;
- cin>>n;
- cin>>k;
- ptr=new circle; //First Node
- ptr->id=1;
- start=ptr;
- for(int i=2;i<=n;i++) //Creating of Circualar queue
- {
- node=new circle;
- node->id=i;
- ptr->next=node;
- ptr=node;
- }
- ptr->next=start;
- ptr=start;
- while(elimination!=n-1)
- {
- counter++;
- if(counter==k)
- {
- prev->next=ptr->next;
- temp=ptr->next;
- delete ptr;
- ptr=temp;
- elimination++;
- counter=0;
- }
- else
- {
- prev=ptr;
- ptr=ptr->next;
- }
- }
- cout<<ptr->id;
- }
Add Comment
Please, Sign In to add comment