iv taken a list of bakery items from user in doubly linked list.. but remove func isnt working................ :(

Recommended Answers

All 18 Replies

If you're using visual studio check the youtube link in my signature to learn to use VS debugging features.

Member Avatar for MonsieurPointer

We can't read minds - without any code, how are we supposed to help you?

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";



}


}


}

thats the code.. tried alot but useless... remv func isnt workin..

Member Avatar for MonsieurPointer

Please use the code tags

Please use the code tags

means?

Member Avatar for MonsieurPointer

Select the code, then click on the [code] button in the editor.

//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";


}

}



}
//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.

You might want to format your code.... it's pretty hard to read.

so what should i do?

You might want to format your code.... it's pretty hard to read.

iv coded it for ppl convenience.. u can check

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.

iv checked it in debugger but could not get any solution. i just want u ppl to chk my remove function. thats it!

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?

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.

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

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()
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.