954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

josepus problem??????????

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
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"<>count;
int temp;
for(int i=0;i>temp;
list.add(temp);
}
cout<<"\nlist size is="<1){
for(i=1;i<=m;i++)
list.next();
cout<<"remove"<

c++lover
Newbie Poster
5 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

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<strong>h</strong>us, not josepus
chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: