iv taken a list of bakery items from user in doubly linked list.. but remove func isnt working................ :(
sunn shine 0 Light Poster
pseudorandom21 166 Practically a Posting Shark
If you're using visual studio check the youtube link in my signature to learn to use VS debugging features.
MonsieurPointer
We can't read minds - without any code, how are we supposed to help you?
sunn shine 0 Light Poster
We can't read minds - without any code, how are we supposed to help you?
//class node
#include <iostream>
#include <string>
using namespace std;
//CLASS NODE
class Node
{
public:
string object;
Node *nextNode; //next part of node
Node *prevNode;//
public:
//get object
string get()
{
return object;
};
//set object
void set(string obj)
{
this ->object =obj;
};
//get next i.e adress of next node.......
Node *getNext()
{
return nextNode;
};
//set next i.e fill next par of current node with adress of next node
void setNext(Node *nextnode)
{
this->nextNode=nextnode; //nextnode is adress of next node
};
//set previous node
void setPrev(Node *prevnode)
{
this->prevNode=prevnode;
};
//get previous node
Node *getPrev()
{
return prevNode;
};
};//////CLASS NODE END HERE
//CLASS LIST
class List
{
private:
int size;
Node *headNode;
Node *currentNode;
public:
//constructor
List()
{
headNode=new Node();
headNode->setNext(NULL);
currentNode =NULL;
size=0;
};
//1.........start places c.n point to the start of list
void start()
{
currentNode=headNode;
};
//2.... add fnc.add nodes in list
void add(string addobject)
{
Node* newNode=new Node();
newNode->set(addobject);//add object by calling set function of Node class
if(currentNode!=NULL)
{
newNode->setNext(currentNode->getNext());
newNode->setPrev(currentNode);
currentNode->setNext(newNode);
(currentNode->getNext())->setPrev(newNode);
currentNode=newNode;
}
else //if c.n is null i.e head node
{
newNode->setNext(NULL);
newNode->setPrev(NULL);
headNode->setNext(newNode);
currentNode=newNode;
}
size++;
};
//3........remove fnc.remove node from list
void remove()
{
cout<<"in remove";
Node *delNode;
if(currentNode !=NULL && currentNode !=headNode)
{
(currentNode->getPrev())->setNext(currentNode->getNext());
(currentNode->getNext())->setPrev(currentNode->getPrev());
//currentNode=delNode;
delNode= currentNode;
delete delNode;
currentNode=currentNode->getNext();
size--;
}
else
headNode=currentNode->getNext();
};
void deletefromlist(string target)
{
if(search_list(target))
{
remove();
}
}
//BOOL SEARCH
bool search_list(string target)
{
bool found;
currentNode=headNode->getNext();
//if(currentNode!=NULL)
//{
while(currentNode!=NULL)//(strcmp(currentNode->get(),target)!=0)
{ string c= currentNode->get();
//char * d=c;
int x=(currentNode->get()).compare(target);
if (x==0)
//break;
{
cout<<"found";
found =true;
return found;
}
currentNode=currentNode->getNext();
//
}
return false;
//}
/* found=true;
else
found=false;
return found;
*/};
//4.......get list elements....
string get()
{
if(currentNode!=NULL)
return currentNode->get();
};
//5......next => tell us that our list is empty or ended
bool next()
{
if(currentNode==NULL)
return false;
currentNode=currentNode->getNext();
if(currentNode==NULL || size==0)
return false;
else
return true;
};
//6....length
int length()
{
return size;
};
};//LIST CLASS END............
void main()
{
List bakery;
string item;
int i=1;
cout<<"ENTER stop when you don't want to add more items"<<endl;
while(item!="stop")
{
cout<<"ENTER LIST ITEM NO "<<i<<": ";
cin>>item;
if(item=="stop")
break;
bakery.add(item);
cout<<endl;
i++;
}
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
int a=0;
cout<<" *_* CUSTOMER MENU *_*"<<endl;
cout<<" press 1 ->remove item"<<endl;
cout<<" press 2 ->final list "<<endl;
cout<<" press 3 ->exit"<<endl;
while(a!=3)
{
cout<<"WHAT DO U WANT CUSTOMER"<<endl;
cin>>a;
if(a==1)
{
char r[50];
cout<<"ENTER ITEM U WAT TO REMOVE :";
cin>>r;
bakery.deletefromlist(r);
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
}
if(a==2)
{
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<" **"<<bakery.get()<<endl;
}
cout<<endl;
cout<<" YOU BUY ** "<<bakery.length()<<" ** ITEMS FrOM MY BAKERY \n THANK U FOR SHOPING HERE \n";
}
}
}
Edited by Nick Evan because: Fixed formatting
sunn shine 0 Light Poster
thats the code.. tried alot but useless... remv func isnt workin..
Edited by sunn shine because: n/a
MonsieurPointer
Please use the code tags
sunn shine 0 Light Poster
Please use the code tags
means?
MonsieurPointer
Select the code, then click on the [code] button in the editor.
sunn shine 0 Light Poster
//class node
#include <iostream>
#include <string>
using namespace std;
//CLASS NODE
class Node
{
public:
string object;
Node *nextNode; //next part of node
Node *prevNode;//
public:
//get object
string get()
{
return object;
};
//set object
void set(string obj)
{
this ->object =obj;
};
//get next i.e adress of next node.......
Node *getNext()
{
return nextNode;
};
//set next i.e fill next par of current node with adress of next node
void setNext(Node *nextnode)
{
this->nextNode=nextnode; //nextnode is adress of next node
};
//set previous node
void setPrev(Node *prevnode)
{
this->prevNode=prevnode;
};
//get previous node
Node *getPrev()
{
return prevNode;
};
};//////CLASS NODE END HERE
//CLASS LIST
class List
{
private:
int size;
Node *headNode;
Node *currentNode;
public:
//constructor
List()
{
headNode=new Node();
headNode->setNext(NULL);
currentNode =NULL;
size=0;
};
//1.........start places c.n point to the start of list
void start()
{
currentNode=headNode;
};
//2.... add fnc.add nodes in list
void add(string addobject)
{
Node* newNode=new Node();
newNode->set(addobject);//add object by calling set function of Node class
if(currentNode!=NULL)
{
newNode->setNext(currentNode->getNext());
newNode->setPrev(currentNode);
currentNode->setNext(newNode);
(currentNode->getNext())->setPrev(newNode);
currentNode=newNode;
}
else //if c.n is null i.e head node
{
newNode->setNext(NULL);
newNode->setPrev(NULL);
headNode->setNext(newNode);
currentNode=newNode;
}
size++;
};
//3........remove fnc.remove node from list
void remove()
{
cout<<"in remove";
Node *delNode;
if(currentNode !=NULL && currentNode !=headNode)
{
(currentNode->getPrev())->setNext(currentNode->getNext());
(currentNode->getNext())->setPrev(currentNode->getPrev());
//currentNode=delNode;
delNode= currentNode;
delete delNode;
currentNode=currentNode->getNext();
size--;
}
else
headNode=currentNode->getNext();
};
void deletefromlist(string target)
{
if(search_list(target))
{
remove();
}
}
//BOOL SEARCH
bool search_list(string target)
{
bool found;
currentNode=headNode->getNext();
//if(currentNode!=NULL)
//{
while(currentNode!=NULL)//(strcmp(currentNode->get(),target)!=0)
{ string c= currentNode->get();
//char * d=c;
int x=(currentNode->get()).compare(target);
if (x==0)
//break;
{
cout<<"found";
found =true;
return found;
}
currentNode=currentNode->getNext();
//
}
return false;
//}
/* found=true;
else
found=false;
return found;
*/};
//4.......get list elements....
string get()
{
if(currentNode!=NULL)
return currentNode->get();
};
//5......next => tell us that our list is empty or ended
bool next()
{
if(currentNode==NULL)
return false;
currentNode=currentNode->getNext();
if(currentNode==NULL || size==0)
return false;
else
return true;
};
//6....length
int length()
{
return size;
};
};//LIST CLASS END............
void main()
{
List bakery;
string item;
int i=1;
cout<<"ENTER stop when you don't want to add more items"<<endl;
while(item!="stop")
{
cout<<"ENTER LIST ITEM NO "<<i<<": ";
cin>>item;
if(item=="stop")
break;
bakery.add(item);
cout<<endl;
i++;
}
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
int a=0;
cout<<" *_* CUSTOMER MENU *_*"<<endl;
cout<<" press 1 ->remove item"<<endl;
cout<<" press 2 ->final list "<<endl;
cout<<" press 3 ->exit"<<endl;
while(a!=3)
{
cout<<"WHAT DO U WANT CUSTOMER"<<endl;
cin>>a;
if(a==1)
{
char r[50];
cout<<"ENTER ITEM U WAT TO REMOVE :";
cin>>r;
bakery.deletefromlist(r);
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
}
if(a==2)
{
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next())
{
cout<<" **"<<bakery.get()<<endl;
}
cout<<endl;
cout<<" YOU BUY ** "<<bakery.length()<<" ** ITEMS FrOM MY BAKERY \n THANK U FOR SHOPING HERE \n";
}
}
}
Greywolf333 11 Junior Poster
//class node #include <iostream> #include <string> using namespace std; //CLASS NODE class Node { public: string object; Node *nextNode; //next part of node Node *prevNode;// public: //get object string get() { return object; }; //set object void set(string obj) { this ->object =obj; }; //get next i.e adress of next node....... Node *getNext() { return nextNode; }; //set next i.e fill next par of current node with adress of next node void setNext(Node *nextnode) { this->nextNode=nextnode; //nextnode is adress of next node }; //set previous node void setPrev(Node *prevnode) { this->prevNode=prevnode; }; //get previous node Node *getPrev() { return prevNode; }; };//////CLASS NODE END HERE //CLASS LIST class List { private: int size; Node *headNode; Node *currentNode; public: //constructor List() { headNode=new Node(); headNode->setNext(NULL); currentNode =NULL; size=0; }; //1.........start places c.n point to the start of list void start() { currentNode=headNode; }; //2.... add fnc.add nodes in list void add(string addobject) { Node* newNode=new Node(); newNode->set(addobject);//add object by calling set function of Node class if(currentNode!=NULL) { newNode->setNext(currentNode->getNext()); newNode->setPrev(currentNode); currentNode->setNext(newNode); (currentNode->getNext())->setPrev(newNode); currentNode=newNode; } else //if c.n is null i.e head node { newNode->setNext(NULL); newNode->setPrev(NULL); headNode->setNext(newNode); currentNode=newNode; } size++; }; //3........remove fnc.remove node from list void remove() { cout<<"in remove"; Node *delNode; if(currentNode !=NULL && currentNode !=headNode) { (currentNode->getPrev())->setNext(currentNode->getNext()); (currentNode->getNext())->setPrev(currentNode->getPrev()); //currentNode=delNode; delNode= currentNode; delete delNode; currentNode=currentNode->getNext(); size--; } else headNode=currentNode->getNext(); }; void deletefromlist(string target) { if(search_list(target)) { remove(); } } //BOOL SEARCH bool search_list(string target) { bool found; currentNode=headNode->getNext(); //if(currentNode!=NULL) //{ while(currentNode!=NULL)//(strcmp(currentNode->get(),target)!=0) { string c= currentNode->get(); //char * d=c; int x=(currentNode->get()).compare(target); if (x==0) //break; { cout<<"found"; found =true; return found; } currentNode=currentNode->getNext(); // } return false; //} /* found=true; else found=false; return found; */}; //4.......get list elements.... string get() { if(currentNode!=NULL) return currentNode->get(); }; //5......next => tell us that our list is empty or ended bool next() { if(currentNode==NULL) return false; currentNode=currentNode->getNext(); if(currentNode==NULL || size==0) return false; else return true; }; //6....length int length() { return size; }; };//LIST CLASS END............ void main() { List bakery; string item; int i=1; cout<<"ENTER stop when you don't want to add more items"<<endl; while(item!="stop") { cout<<"ENTER LIST ITEM NO "<<i<<": "; cin>>item; if(item=="stop") break; bakery.add(item); cout<<endl; i++; } bakery.start(); cout<<"Your List Items Are "<<endl; while(bakery.next()) { cout<<"**"<<bakery.get()<<endl; } cout<<endl; int a=0; cout<<" *_* CUSTOMER MENU *_*"<<endl; cout<<" press 1 ->remove item"<<endl; cout<<" press 2 ->final list "<<endl; cout<<" press 3 ->exit"<<endl; while(a!=3) { cout<<"WHAT DO U WANT CUSTOMER"<<endl; cin>>a; if(a==1) { char r[50]; cout<<"ENTER ITEM U WAT TO REMOVE :"; cin>>r; bakery.deletefromlist(r); bakery.start(); cout<<"Your List Items Are "<<endl; while(bakery.next()) { cout<<"**"<<bakery.get()<<endl; } cout<<endl; } if(a==2) { bakery.start(); cout<<"Your List Items Are "<<endl; while(bakery.next()) { cout<<" **"<<bakery.get()<<endl; } cout<<endl; cout<<" YOU BUY ** "<<bakery.length()<<" ** ITEMS FrOM MY BAKERY \n THANK U FOR SHOPING HERE \n"; } } }
I found the problem--remember that delete "erases" everything at a particular memory address or addresses. So if both delNode and currentNode are pointing to the same location in memory (because you said delNode = currentNode), then "delete delNode" and "delete currentNode" will do the same thing. So your program fails when you try to use ->getNext on a deallocated pointer.
delNode= currentNode;
delete delNode; //1
currentNode=currentNode->getNext(); //2
Switch lines 1 and 2.
homeryansta 36 Junior Poster in Training
You might want to format your code.... it's pretty hard to read.
sunn shine 0 Light Poster
so what should i do?
sunn shine 0 Light Poster
You might want to format your code.... it's pretty hard to read.
iv coded it for ppl convenience.. u can check
pseudorandom21 166 Practically a Posting Shark
iv coded it for ppl convenience.. u can check
No you haven't, you didn't indent it properly or your paste got messed up by not using the code tags and then you copied the unformatted text and pasted it into the code tags.
Usually people on help forums have a difficult time helping people that post a whole class and expect us to debug it for them, so tell us, what do you think is wrong with it?
I would ask you to look into the debugging features of your current compiler/ide toolkit.
sunn shine 0 Light Poster
iv checked it in debugger but could not get any solution. i just want u ppl to chk my remove function. thats it!
sunn shine 0 Light Poster
im new here , so i dont know how to CODE effectively.. i just copied my c++ code frm my computer, pasted it here and pressed the CODE option.. is that right or wrong?
Edited by happygeek because: fixed formatting
Narue 5,707 Bad Cop Team Colleague
im new here , so i dont know how to CODE effectively.. i just copied my c++ code frm my computer, pasted it here and pressed the
option.. is that right or wrong?[/QUOTE] Click Me. The most important part is that your code actually have formatting. If there's zero indentation, code tags won't help: [code] // This is farking hard to read!!!!!! #include <iostream> int main() { for (int i = 0; i < 10; i++) { std::cout << "Counting to " << i << '\n'; } }
With properly formatted code, the code tags will preserve your indentation:
// This is easy to read #include <iostream> int main() { for (int i = 0; i < 10; i++) { std::cout << "Counting to " << i << '\n'; } }
People will be far less inclined to help you if your code is difficult to read, so it's in your best interests to make it as clear as possible both in style and formatting. There's also a post preview feature that you should make use of to ensure that your posts will be displayed exactly as you want.
If you're using the quick post box at the bottom of the thread, simply click on the Use Advanced Editor button. This will take you to the advanced editor and automatically preview what you already have in the quick post box. From there you can use the Preview Post button to do the same thing.
sunn shine 0 Light Poster
Click Me . The most important part is that your code actually have formatting. If there's zero indentation, code tags won't help:
// This is farking hard to read!!!!!! #include <iostream> int main() { for (int i = 0; i < 10; i++) { std::cout << "Counting to " << i << '\n'; } }
With properly formatted code, the code tags will preserve your indentation:
// This is easy to read #include <iostream> int main() { for (int i = 0; i < 10; i++) { std::cout << "Counting to " << i << '\n'; } }
People will be far less inclined to help you if your code is difficult to read, so it's in your best interests to make it as clear as possible both in style and formatting. There's also a post preview feature that you should make use of to ensure that your posts will be displayed exactly as you want.
If you're using the quick post box at the bottom of the thread, simply click on the Use Advanced Editor button. This will take you to the advanced editor and automatically preview what you already have in the quick post box. From there you can use the Preview Post button to do the same thing.
o thanx.. i got that
Taywin 312 Posting Virtuoso
This is too long code... You could read my comment inside your codes... You need some modification and may have to redesign certain parts of your code.
//class node
#include <iostream>
#include <string>
using namespace std;
//CLASS NODE
class Node {
public:
string object;
Node *nextNode; //next part of node
Node *prevNode;//
public:
// *** Should have a constructor here for convenient use
// and set the nextNode & prevNode to NULL as well,
// so caller doesn't need to explicitly set them to NULL.
// It could create a problem if the caller forgets to
// set the values to NULL. ***
//get object
string get() {
return object;
};
//set object
void set(string obj) {
this ->object =obj;
};
//get next i.e adress of next node.......
Node *getNext() {
return nextNode;
};
//set next i.e fill next par of current node with adress of next node
void setNext(Node *nextnode) {
this->nextNode=nextnode; //nextnode is adress of next node
};
//set previous node
void setPrev(Node *prevnode) {
this->prevNode=prevnode;
};
//get previous node
Node *getPrev() {
return prevNode;
};
};//////CLASS NODE END HERE
//CLASS LIST
class List {
private:
int size;
Node *headNode;
// *** Actually it is not a good idea to keep currentNode as a class
// variable. If you do, you will have to do book keeping and ensure
// that this currentNode is really the currentNode! ***
Node *currentNode;
public:
//constructor
List() {
// *** Unless you would never use "headNode" to store value, you should
// set the node itself to NULL first.
// If you create a new Node here, you may have trouble distinguish
// between a created node and an initiated node. ***
headNode=new Node();
// *** If you have a constructor to set the next/prev to NULL,
// you should not need this line. ***
headNode->setNext(NULL);
currentNode = NULL;
size=0;
};
//1.........start places c.n point to the start of list
void start() {
currentNode=headNode;
};
//2.... add fnc.add nodes in list
void add(string addobject) {
Node* newNode=new Node();
newNode->set(addobject);//add object by calling set function of Node class
// *** Now you try to check if your list is an empty list.
// But remember that you have already initilize "headNode" and copy
// the pointer to "currentNode," so your "currentNode" will never
// be null at the start! ***
if(currentNode!=NULL) {
newNode->setNext(currentNode->getNext());
newNode->setPrev(currentNode);
currentNode->setNext(newNode);
(currentNode->getNext())->setPrev(newNode);
currentNode=newNode;
}
else { //if c.n is null i.e head node
// *** If you have the constructor to set them to NULL for you,
// you do not need to explicitly set them yourself. ***
newNode->setNext(NULL);
newNode->setPrev(NULL);
// *** This is a mixed type of what you are doing. In your if/else
// statement, you are checking for NULL value of headNode.
// But the way you implement, you seem to use the "headNode"
// as a place holder only.
// Which way are you going here? ***
headNode->setNext(newNode);
currentNode=newNode;
}
size++;
};
//3........remove fnc.remove node from list
void remove() {
cout<<"in remove";
Node *delNode;
// *** Are you sure that the node you are removing is what you want?
// You are attempt to delete only the "currentNode" so you must
// be sure that the currentNode is the node you want to delete.
// If it is not, you may be deleting a wrong node here. ***
if(currentNode !=NULL && currentNode !=headNode) {
(currentNode->getPrev())->setNext(currentNode->getNext());
(currentNode->getNext())->setPrev(currentNode->getPrev());
//currentNode=delNode;
delNode = currentNode;
delete delNode;
// *** Are you sure? If the currentNode now is the tail node (last node
// in the list), you will get a NULL instead??? ***
currentNode=currentNode->getNext();
size--;
}
else
// *** Hmm... If the "currentNode" is NULL, you will be in trouble.
// Also, if the currentNode is the last node in the list,
// you will be in trouble as well. And why don't you free
// the deleted node from the memory as well? ***
headNode=currentNode->getNext();
};
void deletefromlist(string target) {
if(search_list(target)) {
remove();
}
}
//BOOL SEARCH
bool search_list(string target) {
bool found;
// *** This is the remnant of what you did... You are using the
// headNode as a place holder only. ***
currentNode=headNode->getNext();
//if(currentNode!=NULL)
//{
while(currentNode!=NULL) {
//(strcmp(currentNode->get(),target)!=0)
string c= currentNode->get();
//char * d=c;
int x=(currentNode->get()).compare(target);
if (x==0) {
//break;
cout<<"found";
// *** You could simply use "return true;" instead ***
found =true;
return found;
}
currentNode=currentNode->getNext();
}
return false;
//}
/* found=true;
else
found=false;
return found;
*/
};
//4.......get list elements....
string get() {
if(currentNode!=NULL)
return currentNode->get();
};
//5......next => tell us that our list is empty or ended
bool next() {
if(currentNode==NULL)
return false;
currentNode=currentNode->getNext();
// *** This is also the remnant of using headNode as a place holder. ***
if(currentNode==NULL || size==0)
return false;
else
return true;
};
//6....length
int length() {
return size;
};
};//LIST CLASS END............
void main() {
List bakery;
string item;
int i=1;
cout<<"ENTER stop when you don't want to add more items"<<endl;
while(item!="stop") {
cout<<"ENTER LIST ITEM NO "<<i<<": ";
cin>>item;
if(item=="stop")
break;
bakery.add(item);
cout<<endl;
i++;
}
bakery.start();
cout<<"Your List Items Are "<<endl;
// *** This while condition could be NULL because you try to check for
// the "next" node which could always be NULL. ***
while(bakery.next()) {
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
int a=0;
cout<<" *_* CUSTOMER MENU *_*"<<endl;
cout<<" press 1 ->remove item"<<endl;
cout<<" press 2 ->final list "<<endl;
cout<<" press 3 ->exit"<<endl;
while(a!=3) {
cout<<"WHAT DO U WANT CUSTOMER"<<endl;
cin>>a;
if(a==1) {
char r[50];
cout<<"ENTER ITEM U WAT TO REMOVE :";
cin>>r;
bakery.deletefromlist(r);
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next()) {
cout<<"**"<<bakery.get()<<endl;
}
cout<<endl;
}
// *** Should use "else if" instead of "if" because the value of "a"
// is mutual exclusive -- never execute more than 1 scope under
// the checking condition. ***
if(a==2) {
bakery.start();
cout<<"Your List Items Are "<<endl;
while(bakery.next()) {
cout<<" **"<<bakery.get()<<endl;
}
cout<<endl;
cout<<" YOU BUY ** "<<bakery.length()<<" ** ITEMS FrOM MY BAKERY \n THANK U FOR SHOPING HERE \n";
}
} // while a!=3
} // main()
Edited by Taywin because: n/a
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.