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");
}

In your for loop in the traverse function you have the condition list.next()<=list.length() , but list.next returns a bool value, meaning that it is either 1 or 0. And since the length never changes inside that loop, that loop is infinite unless you enter the loop with the list having a length of 0.

Use code tags, and its Josep[B]h[/B]us, not josepus
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.