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
/*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";
/*get method*/
int List::get()
{ if (cnode != NULL)
{ return cnode->get(); //return address of the node pointed by current node
}
assert (false); // This statement is hit
}
What happens when cnode is null? Adding an assert statement after the if statement, causes the assert statement to be hit, so you're calling get() with an empty list.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
The only way you could be hitting that assert statement is if cnode is null, which means you're calling get() with an empty list.
What do you expect List::get() to return if the list is empty?
You have no return statement for that condition.
To me, that's a logical error since you have no unambiguous way to indicate to the caller that the list is empty. Returning 0 is ambiguous since 0 could be an entry in the list.
Yes But when I putted adress of last node pointer into head node's prev pointer and next pointer of the last node into the first node, this is still giving error, what should I do ?
Please give me any idea, how I can make it circularly list ?
I made up Doubly linked list and that is working well, but how do I make it Circular linked list, please give me idea, please give me idea, thanks you so much !