JOSEPHUS PROBLEM
JOSEPHUS PROBLEM CODE
#include <iostream>
using namespace std;
int counter=0,i,k=0;
struct Node{
int data;
Node *next;
};
class circular
{
public:
int d;
Node *rear,*prev,*next,*first;
circular()
{
rear=NULL;
}
void insertNode(int d)
{
Node *New=new Node;
//cin>>d;
if (rear==NULL)
{
New->data=d;
New->next=New;
first=New;
prev=New;
first=New;
rear=New;
}
else
{
New->data=d;
New->next=first;
prev->next=New;
prev=New;
rear=New;
}
}
void display()
{
Node *current=first;
while(current !=rear)
{
cout<<current->data<<" ";
current=current->next;
}
if(current==rear)
{
cout<<current->data<<endl;;
}
}
int delete_last() {
Node*prev;
Node *current=first;
while(current!=rear)
{
prev=current;
current=current->next;
}
if(current==rear){
prev->next=first;
rear=prev;
}
}
int delete_Node(){
Node*prev,*c;
Node *current=first;
while(current !=rear)
{ counter ++;
// counter++;
if(counter==4){
c=current->next;
prev->next=c;
current=current->next;
counter==0;
Node *current=first;
}
else{
prev=current;
current=current->next;}
}
k++;
}
};
int main()
{int l;
circular c;
cout<<"Enter values of ur Circularlinked list";
cout<<endl;
for( i=0; i<9; i++)
{
cin>>l;
c.insertNode(l);
}
c.display();
for(int i=0; i<9; i++)
{c.delete_Node();
counter=0;
}
c.display();
c.delete_last();
c.display();
cout<<"According to Problem:"<<endl;
c.delete_last();
c.delete_last();
c.display();
return 0;
}
Comments
Post a Comment