Program error

I have problem in this program, Did not get how I fix this run time error, I am trying to make Josephus game BUT run time error is occurring, please help me to fix where is the problem,

Thanks.

#include <iostream>

using namespace std;

/*Class Node*/
class Node {
public:

/*method for set, get object*/
void setobj(int object) {
this->object = object;
} //set object function
int get() {
return object;
}

/*method for set get address of node*/
void setNext (Node *nextNode) {
this->nextNode = nextNode;
} //set addres function
Node *getnext() {
return nextNode;
}

/*method for set get previous position in listed memory*/
void setprev (Node *prev){
this->prev = prev;
}
Node *getprev() {
return prev;
} //--------------------Public part end

private:
int object;
Node *nextNode;
Node *prev;
}; //class definition complete



/*class List*/
class List {
private:
int size;
Node *cnode, *lcnode, *hnode;

public:
List();
void add(int object);
int get();
bool next();
friend void traverse(List list); //will traverse list
friend List addNode();
void start(); //will help us to go to start of list
void remove(); //will help us to remove node from list
int legnth();
};
/*Cunstructor*/
List::List() {
hnode = new Node(); //getting node from class node or requesting to class node to make Node() constructor for us
hnode->setNext(NULL); //next node of head node being NULL
cnode = NULL;
lcnode = NULL;
size = 0; //when every thing is NULL size is zero
}

/*add method*/
void List::add(int object) {
Node *newNode = new Node(); //getting node and storing it into new node pointer
newNode->setobj(object); //adding object to new node
if (cnode != NULL) { //if current node is not NULL (this is NULL when there is no Node in list)
newNode->setNext(cnode->getnext()); //set new node on the front of current node
cnode->setNext(newNode);
newNode->setprev(cnode);
(cnode->getnext())->setprev(newNode);
lcnode = cnode;
cnode = newNode;
}
else {
newNode->setNext(NULL); //setting next node to new node to Null
hnode->setNext(newNode); //setting new node next of head node
lcnode = hnode; //now as we added a new node, head node will be last current node
cnode = newNode; //current pointer will alawys points to new node

}
size++;
}

/*get method*/

int List::get() {
if (cnode != NULL) {
return cnode->get(); //return address of the node pointed by current node

}
}

/*Next() method*/
bool List::next() {
if(cnode == NULL) return false; //if there is no element in the list current node is equal to NULL and will return false

lcnode = cnode; //did not get
cnode = cnode->getnext();
if (cnode == NULL || size == 0)
return false;
else
return true;
}

/*traverse() method*/
void traverse(List list) {
Node *scnode = list.cnode; //current node is saving into scnode
list.cnode = list.hnode; //head node is being current node

for (int i =1; list.next(); i++) {
cout << "\n Element" << i << " " << list.get();
}
list.cnode = scnode; //did not get
cout << endl;
}
/*add nodes method*/
List addNode() {
List list;
list.add(1);
list.add(2);
list.add(3);
cout << "\n List size =" << list.size << "\n";

return list;
cout <<endl;
}

/*start method*/
void List::start() {
lcnode = hnode;
cnode = hnode;
}

/*remove() method*/
void List::remove() {
if(cnode != NULL && cnode != hnode) {
lcnode->setNext(cnode->getnext());
delete cnode;
cnode = lcnode;
size--;
} //------if
}

/*lenght method*/
int List::legnth() {
return size;
}

/*main*/
int main (int argc, char *argv) {
List list;
int i, N = 10, M = 3;
for (i = 0; i <= N; i++) list.add(i);


list.start();
while(list.legnth() > 1) {
for(i = 0; i < M; i++) list.next();

cout <<"Remove:" << list.get() << endl;
list.remove();

}
cout << "Leader is:" << list.get() << endl;

system("pause");
}

Last edited on
Topic archived. No new replies allowed.