I wrote the method of moving back from node to node in double link list. On calling first time back method, node move one step back.
On calling again, it displays same node. Code is here. please check where i did wrong
On calling again, it displays same node. Code is here. please check where i did wrong
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
/* The Node class */
class Node
{
private:
int object;
Node *nextNode; //points to the next node
Node *prevNode; //points to previous node
public:
int get()
{
cout << "Element is: " << object;
return object;
}
void set(int object)
{
this->object = object;
}
Node * getNext()
{
return nextNode;
}
void setNext(Node * nextNode)
{
this->nextNode = nextNode;
}
Node *getPrev()
{
return prevNode;
}
void setPrev(Node *prevNode)
{
this->prevNode = prevNode;
}
};
/* The List class */
class List
{
public:
List();
void start();
void add (int addObject);
void remove();
int get();
bool next();
bool back();
friend void traverse(List list);
// friend List addNodes();
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode;
};
/* Constructor */
List::List()
{
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
lastCurrentNode = NULL;
size = 0;
}
//Position currentNode and lastCurrentNode at first element
void List::start()
{
lastCurrentNode = headNode;
currentNode = headNode;
}
/* add() class method */
void List::add (int addObject)
{
Node *newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{
newNode->setNext(currentNode->getNext());
newNode->setPrev(currentNode);
currentNode->setNext( newNode );
(currentNode->getNext())->setPrev(newNode);
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{
newNode->setNext(NULL);
newNode->setPrev(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
//Remove the current node from the list move currentNode pointer to the next node
void List::remove()
{
if(currentNode != NULL && currentNode != headNode)
{
cout << endl << "Item Removed: " << currentNode->get();
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}
/* get() class method */
int List::get()
{
cout << endl;
if (currentNode != NULL)
//cout << "Element of Node is: ";
return currentNode->get();
}
/* next() class method */
bool List::next()
{
if (currentNode == NULL)
return false;
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
}
bool List::back()
{
if(currentNode == NULL)
return false;
currentNode = lastCurrentNode;
lastCurrentNode = lastCurrentNode->getPrev();
if(currentNode == NULL || lastCurrentNode == NULL)
return false;
else
return true;
}
/* Friend function to traverse linked list */
void traverse(List list)
{
Node* savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Element " << i << " " << list.get();
}
list.currentNode = savedCurrentNode;
}
/* Friend function to add Nodes into the list */
/*List addNodes()
{
List list;
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
cout << "\n List size = " << list.size <<'\n';
return list;
} */
main()
{
List list ; //creating a list object, we may call addNodes method as List list =addNodes()
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
// traverse(list);
// cout << endl << endl;
// list.start(); //currently at headNode
//
// while (list.next())
// {
// cout << list.get() << endl;
// }
cout << endl << endl << endl;
list.start();
list.get();
//cout << endl << endl << endl;
list.next(); //now comes to node with data element 2
list.get();
list.next(); //now comes to node with data element 6
list.get();
list.next();
list.get();
list.back();
list.get();
list.back();
list.get();
list.back();
list.get();
//
// list.remove(); //current data item is removed i.e 6
// list.get(); //now current pointer move to next node i.e node with data 8;
//
//
// // list.add(9);
// // list.add(11);
getche();
#include <stdlib.h>
#include <conio.h>
/* The Node class */
class Node
{
private:
int object;
Node *nextNode; //points to the next node
Node *prevNode; //points to previous node
public:
int get()
{
cout << "Element is: " << object;
return object;
}
void set(int object)
{
this->object = object;
}
Node * getNext()
{
return nextNode;
}
void setNext(Node * nextNode)
{
this->nextNode = nextNode;
}
Node *getPrev()
{
return prevNode;
}
void setPrev(Node *prevNode)
{
this->prevNode = prevNode;
}
};
/* The List class */
class List
{
public:
List();
void start();
void add (int addObject);
void remove();
int get();
bool next();
bool back();
friend void traverse(List list);
// friend List addNodes();
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode;
};
/* Constructor */
List::List()
{
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
lastCurrentNode = NULL;
size = 0;
}
//Position currentNode and lastCurrentNode at first element
void List::start()
{
lastCurrentNode = headNode;
currentNode = headNode;
}
/* add() class method */
void List::add (int addObject)
{
Node *newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{
newNode->setNext(currentNode->getNext());
newNode->setPrev(currentNode);
currentNode->setNext( newNode );
(currentNode->getNext())->setPrev(newNode);
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{
newNode->setNext(NULL);
newNode->setPrev(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
//Remove the current node from the list move currentNode pointer to the next node
void List::remove()
{
if(currentNode != NULL && currentNode != headNode)
{
cout << endl << "Item Removed: " << currentNode->get();
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}
/* get() class method */
int List::get()
{
cout << endl;
if (currentNode != NULL)
//cout << "Element of Node is: ";
return currentNode->get();
}
/* next() class method */
bool List::next()
{
if (currentNode == NULL)
return false;
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
}
bool List::back()
{
if(currentNode == NULL)
return false;
currentNode = lastCurrentNode;
lastCurrentNode = lastCurrentNode->getPrev();
if(currentNode == NULL || lastCurrentNode == NULL)
return false;
else
return true;
}
/* Friend function to traverse linked list */
void traverse(List list)
{
Node* savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Element " << i << " " << list.get();
}
list.currentNode = savedCurrentNode;
}
/* Friend function to add Nodes into the list */
/*List addNodes()
{
List list;
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
cout << "\n List size = " << list.size <<'\n';
return list;
} */
main()
{
List list ; //creating a list object, we may call addNodes method as List list =addNodes()
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
// traverse(list);
// cout << endl << endl;
// list.start(); //currently at headNode
//
// while (list.next())
// {
// cout << list.get() << endl;
// }
cout << endl << endl << endl;
list.start();
list.get();
//cout << endl << endl << endl;
list.next(); //now comes to node with data element 2
list.get();
list.next(); //now comes to node with data element 6
list.get();
list.next();
list.get();
list.back();
list.get();
list.back();
list.get();
list.back();
list.get();
//
// list.remove(); //current data item is removed i.e 6
// list.get(); //now current pointer move to next node i.e node with data 8;
//
//
// // list.add(9);
// // list.add(11);
getche();
There was problem in pointer settings. below is the modified part of program for correct working.
Only add() functions is corrected with modifications identified.
Only add() functions is corrected with modifications identified.
/* add() class method */
void List::add (int addObject)
{
Node *newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{ //********************Instructor modification starts
// newNode->setNext(currentNode->getNext());
newNode->setNext(NULL);
newNode->setPrev(currentNode);
currentNode->setNext( newNode );
//(currentNode->getNext())->setPrev(newNode);
//*********************Instructor modification ends
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{ newNode->setNext(NULL);
//**********************Instructor modification starts
newNode->setPrev(NULL);
newNode->setPrev(headNode);
//**********************Instructor modification ends
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
void List::add (int addObject)
{
Node *newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{ //********************Instructor modification starts
// newNode->setNext(currentNode->getNext());
newNode->setNext(NULL);
newNode->setPrev(currentNode);
currentNode->setNext( newNode );
//(currentNode->getNext())->setPrev(newNode);
//*********************Instructor modification ends
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{ newNode->setNext(NULL);
//**********************Instructor modification starts
newNode->setPrev(NULL);
newNode->setPrev(headNode);
//**********************Instructor modification ends
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
0 Comments
Please add nice comments or answer ....