hi, im working on josepus problem but this programe is not working well it is working in unlimited loop any one can point out the mistakes?????????
#include<iostream>
using namespace std;
class node
{
private:
int object;
node *nextnode;
public:
int get(){return object;};
void set(int object){this->object=object;};
node *getnext(){return nextnode;};
void setnext(node *nextnode){this->nextnode=nextnode;};
};
class List
{
private:
int size;
node *headnode;
node *currentnode;
node *lastcurrentnode;
public:
bool next();
int get();
void add(int addobject);
List();
friend void traverse(List list);
friend List addnodes();
void remove();
void start();
int length();
};
List::List()
{
headnode=new node();
headnode->setnext(NULL);
currentnode=NULL;
lastcurrentnode=NULL;
size=0;
}
void List:: start()
{
lastcurrentnode=headnode;
currentnode=headnode->getnext();
}
void List::add(int addobject)
{
node *newnode=new node();
newnode->set(addobject);
if(currentnode!=NULL)
{
newnode->setnext(headnode->getnext());
currentnode->setnext(newnode);
lastcurrentnode=currentnode;
currentnode=newnode;
}
else
{
newnode->setnext(headnode->getnext());
headnode->setnext(newnode);
lastcurrentnode=headnode;
currentnode=newnode;
}
size++;
}
int List::get()
{
if(currentnode!=NULL)
return currentnode->get();
}
bool List:: next()
{
if(currentnode==NULL) return false;
lastcurrentnode=currentnode;
currentnode=currentnode->getnext();
if(currentnode==NULL||size==0)
return false;
else
return true;
}
void List:: remove()
{
if(currentnode!=NULL&& currentnode!=headnode)
{
lastcurrentnode->setnext(currentnode->getnext());
delete currentnode;
if (lastcurrentnode->getnext() == headnode)
currentnode = lastcurrentnode;
else
currentnode=lastcurrentnode->getnext();
size--;
}
}
int List:: length()
{
return size;
}
void traverse(List list)
{
node *savedcurrentnode=list.currentnode;
list.currentnode=list.headnode;
for(int i;list.next()<=list.length();i++)
{
cout<<"\nElement"<<list.get();
}
list.currentnode=savedcurrentnode;
}
List addnodes()
{
List list;
int count=0;
cout<<"\n please specify the list length";
cin>>count;
int temp;
for(int i=0;i<count;i++)
{
cout<<"please enter element no"<<i+1<<":";
cin>>temp;
list.add(temp);
}
cout<<"\nlist size is="<<list.size<<endl;
return list;
}
main()
{
List list;
int i,m=3;
for(i=1;i<=10;i++)list.add(i);
traverse(list);
list.start();
while(list.length()>1){
for(i=1;i<=m;i++)
list.next();
cout<<"remove"<<list.get()<<endl;
list.remove();
}
cout<<"leader is"<<list.get()<<endl;
system("PAUSE");
}