Saturday, May 22, 2010

Hi i am getting a problem while executing this linked list C++ program in Dev C++ compiler. pls correct this.?

#include %26lt;iostream%26gt;


using namespace std;


typedef struct Node{


public:Node(int data){


this-%26gt;data = data;


previous = NULL;


next = NULL;}


int data;


struct Node* previous;


struct Node* next;


}NODE;


class mylinkedlist{


NODE* front;


NODE* back;


public:


mylinkedlist();


void appendNode(int data1);


int deleteNode();};


mylinkedlist::mylinkedlist(){


front = NULL;


back = NULL;}


void mylinkedlist::appendNode(int listdata){


Node* mynode = new Node(listdata);


if(back == NULL){


back = mynode;


front = mynode;}


else{


back-%26gt;next = mynode;


mynode-%26gt;previous = back;


back = mynode;}}


int mylinkedlist::deleteNode(){


cout%26lt;%26lt;"data is"%26lt;%26lt;back-%26gt;data%26lt;%26lt;endl;


if(back== NULL){


cout%26lt;%26lt;"List is Empty";}


else{


Node *mynode;


mynode = back;


back = back-%26gt;previous;


back-%26gt;next = NULL;}


return 0;}


int main(){


mylinkedlist myllist;


myllist.appendNode(10);


myllist.appendNode(20);


myllist.deleteNode();


myllist.deleteNode();


myllist.deleteNode();


return 0;


}

Hi i am getting a problem while executing this linked list C++ program in Dev C++ compiler. pls correct this.?
First, it's really hard to read that without the proper formatting. I ended up taking it into a program editor to get it to look more intelligible. Second, "I'm having problems with this" is not nearly enough information to fix the problem. Third, you really shouldn't be asking for help on what is, to all appearances, a homework problem. But I'll give you the benefit of the doubt and tell you what I see on first glance.





You're missing the point of object orientation. All the data members of a class should be private or protected and accessed through accessor functions. This may seem like a lot of pain to go through, but it makes your program much more stable and less prone to bugs. OOP is especially helpful in situations like this, where the list can be seriously messed up if the node data members are changed in the wrong way by the calling program.





Now on to specifics.





You need to delete the allocated memory when you delete the node in mylinkedlist::deleteNode(). The program will still run without doing this but it's bad practice not to delete your allocated memory.





I'm not sure why mylinkedlist::deleteNode() returns a value. You neither use nor vary the return value.





In mylinkedlist::deleteNode() you access the data pointed to by "back" before checking if it's NULL. This will cause an access violation on your last call to myllist.deleteNode() from main(). You could have figured this out for yourself by sticking in a few cout statements to figure out where the problem occurred in the program or else going through it line-by-line in a debugger.





I'm not sure I found everything -- like I said, I just glanced at it. Good luck.

magnolia

No comments:

Post a Comment