943,627 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3150
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 22nd, 2008
0

Re: Linked List Delete

From NodeClass.h:

class node
{
	public:
		node();
		Contributor infoIn;
		node *next;
		node (Contributor i, node *pnext) 
		{
			infoIn=i;
			next=pnext;
		}
		~node();

};

You have no data member in the node class called pnext or pNext. The pnext in red above is NOT a data member of the node class.

From NodeClass.cpp:

node::node()
{
	pNext=NULL;
}

This line above is assuming that there is a data member in the node class called pNext. There is not, so I assume you are getting an error here when compiling.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 22nd, 2008
0

Re: Linked List Delete

okay, I made pnext a data member of node, and I corrected all the pNexts and made them pnext.

Now, it will print all the inserts fine, but then the program ends and gives me an error dialog box killing the program
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 22nd, 2008
0

Re: Linked List Delete

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
okay, I made pnext a data member of node, and I corrected all the pNexts and made them pnext.

Now, it will print all the inserts fine, but then the program ends and gives me an error dialog box killing the program

So you added line 7?

If so, I hope you are not still using the name pnext in lines 8 and 11 because that could be confusing to have a function parameter with the same name as a data member. You should probably post your updated code.

C++ Syntax (Toggle Plain Text)
  1. class node
  2. {
  3. public:
  4. node();
  5. Contributor infoIn;
  6. node *next;
  7. node *pnext;
  8. node (Contributor i, node *pnext)
  9. {
  10. infoIn=i;
  11. next=pnext;
  12. }
  13. ~node();
  14.  
  15. };
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 22nd, 2008
0

Re: Linked List Delete

well...yes...all I did was add line 7.

I'm really making a mess of this aren't I? Could that be what's killing the program when it runs?

The more I change, the more lost I get

C++ Syntax (Toggle Plain Text)
  1. #include "Contributor Class.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class node
  7. {
  8. public:
  9. node();
  10. node (Contributor i, node *pnext);
  11. ~node();
  12.  
  13. Contributor infoIn;
  14. node *next;
  15. node *pnext;
  16.  
  17. };

This is now the header, but after your last post, I'm sure it's not right

and the node cpp

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "NodeClass.h"
  3.  
  4. node::node()
  5. {
  6. pnext=NULL;
  7. }
  8.  
  9. node::node(Contributor i, node *pnext)
  10. {
  11. infoIn=i;
  12. next=pnext;
  13. }
  14.  
  15. node::~node()
  16. {
  17. pnext=NULL;
  18. }

Here is the list header

C++ Syntax (Toggle Plain Text)
  1. #include "NodeClass.h"
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class list
  7. {
  8. public:
  9. list();
  10. list(const list &rhs);
  11. bool empty();
  12. bool insert(Contributor In);
  13. bool del(Contributor Del);
  14. friend ostream & operator << (ostream &out ,list & listin);
  15. ~list();
  16. node *plist;
  17. };
Last edited by henpecked1; Aug 22nd, 2008 at 1:25 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 22nd, 2008
0

Re: Linked List Delete

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
well...yes...all I did was add line 7.

I'm really making a mess of this aren't I? Could that be what's killing the program when it runs?

The more I change, the more lost I get

C++ Syntax (Toggle Plain Text)
  1. #include "Contributor Class.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class node
  7. {
  8. public:
  9. node();
  10. node (Contributor i, node *pnext);
  11. ~node();
  12.  
  13. Contributor infoIn;
  14. node *next;
  15. node *pnext;
  16.  
  17. };

This is now the header, but after your last post, I'm sure it's not right

and the node cpp

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "NodeClass.h"
  3.  
  4. node::node()
  5. {
  6. pnext=NULL;
  7. }
  8.  
  9. node::node(Contributor i, node *pnext)
  10. {
  11. infoIn=i;
  12. next=pnext;
  13. }
  14.  
  15. node::~node()
  16. {
  17. pnext=NULL;
  18. }
Well, what does pnext represent? Generally a linked list will store two things. One, it stores some data, which I assume is what infoIn is. Two, it stores a pointer to the next node in the list, which I assume is what next represents. I don't know what pnext represents. Generally, and this can vary, you'll have a node class that looks something like this (to make things easy, I'm going to make everything public, even though usually some things are private.

C++ Syntax (Toggle Plain Text)
  1. class node
  2. {
  3. public:
  4. // data members
  5. Data data;
  6. node* next;
  7.  
  8. // constructors and other methods
  9. };

Data could be any class or struct of some type. It's the data that you are storing. In your case you would probably replace:
C++ Syntax (Toggle Plain Text)
  1. Data data;

with:

C++ Syntax (Toggle Plain Text)
  1. Contributor infoIn;

You can have a variety of different insertion and deletion functions and constructors, all of which will be passed zero or more parameters. The names of these parameters should NEVER be next or infoIn to avoid confusion.

For example, if you wanted to insert a new node with data at the end of the list, you might have a function like this:

C++ Syntax (Toggle Plain Text)
  1. node* node::Insert (node* frontNode, Contributor infoin);

Note infoin versus infoIn . C++ is case-sensitive, so the names are different. Also notice that the node class DOES NOT contain the front node in the linked list. Somewhere this needs to be kept track of, but not in the node class. If the node class has an Insert method, it needs to have that information passed to it or have access to it in some other way. In this case, it's passed. On the other hand, you could have a list class (not a node class) that does store the front node:

C++ Syntax (Toggle Plain Text)
  1. class list
  2. {
  3. public:
  4. node* frontnode;
  5. // constructors and other methods
  6. };

The point of all this is to point out that what you shouldn't store more than you have to in the node class and any parameters you pass to any constructor or any other method must have a different name than any data member of the node class to avoid confusion.

Hopefully this helps clarify rather than confuse.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 22nd, 2008
0

Re: Linked List Delete

so as usual..I overcomplicated it for myself...ookay...try again I guess...lol

do I even need a copy constructor in the node class?

and I took out the references to next in the node class.

to explain my pointer variables...

plist is the head of the list

ptrav is the front, head, temp pointer that traverses the nodes looking for the correct insertion/deletion point

pprev is the rear, tail, temp2 pointer that keeps track of the previously visited node

pnext is the pointer to the next node

Here is the revised header class
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "Contributor Class.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class node
  8. {
  9. public:
  10. node();
  11. node (Contributor i, node *pnext); //do I need this? is it even written correctly?
  12. ~node();
  13.  
  14. Contributor infoIn; // data to be stored in the node
  15. node *pnext; // to store info to next node
  16.  
  17. };

and the cpp file

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "NodeClass.h"
  3.  
  4. node::node()
  5. {
  6. pnext=NULL;
  7. }
  8.  
  9. node::node(Contributor i, node *pnext)
  10. {
  11. infoIn=i;
  12. }
  13.  
  14. node::~node()
  15. {
  16. }
Last edited by henpecked1; Aug 22nd, 2008 at 2:25 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 22nd, 2008
0

Re: Linked List Delete

Is there any order to the Contributor class? If so, is this to be an ordered linked list, as in

C++ Syntax (Toggle Plain Text)
  1. 1 --> 6 --> 14 --> 20 --> 25

the order being determined by the "value" of Contributor? In that case, when you insert, you could be inserting into the middle of the list. Or are you always inserting to the beginning or the end of the list. I saw earlier that you have a list class in addition to the node class. So consider having your Delete and Insert functions be list functions. list has access to plist, where node does not unless it is passed plist as a parameter. Regarding what you should have in your node class, quite possibly have only constructors and a destructor (assuming data members are public). Which constructors you need will depend on the program, but I'd consider these could work:

C++ Syntax (Toggle Plain Text)
  1. node (); // default constructor
  2. node (const Contributor& con); // constructor

You may want to just go with these two and not have a node copy constructor. I think at this stage it's easier to just assign pnext to be null in both of these constructors.

If you make things private later, you'll need to add some methods, but simplify life and keep them public for now, in my opinion.

For sure, do not have this function:
C++ Syntax (Toggle Plain Text)
  1. node::node(Contributor i, node *pnext)

Remember the rule about not naming your parameters with the same name as the data member name. If you did want to keep it (I probably wouldn't), rename the parameter:

C++ Syntax (Toggle Plain Text)
  1. node::node(Contributor i, node *pNext)
  2. {
  3. pnext = pNext;
  4. infoIn = new Contributor(i); // or some other code
  5. // depending on need
  6. }

Frankly, I'd keep my node class minimal and have the list::Insert and list::Delete function handle the responsibility of making sure plist and pnext point to the correct node, so I might just have the list class have the two constructors and destructor, and later if you make things private, some "get" and "set" functions in the node class.

The important thing is to decide what class has what responsibility and stick with it. I'd have the list class do most of the work. There's no right or wrong answer, except if you can't decide what class does what, in which case you'll almost certainly have problems. You need to decide whether this is an ordered or unordered linked list. If it's unordered, stick any new node into the front or the back of the list. Sticking it in the front would mean less traversal. If it's ordered and you're inserting, traverse till you find a node whose Contributor value is greater than or equal to the new node's Contributor value and stick it in front of that node. If there is no node that's greater than or equal, stick the new node in the back. Any insertion or deletion will require tidying up of the plist and/or pnext pointers.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 22nd, 2008
0

Re: Linked List Delete

The list class does indeed do most of the work. All my list manipulation happens there (insert etc)

It is an ordered list, and the insert puts everything in the correct order. With the current files, the delete gives no outward appearance that it's doing anything and then the OS kills the file. I'm quite sure this has to do with what you're telling me.

I'll change the node class again and we'll see where we end up this time. Just a passing thought, could my contributor class files be interfering some how?
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 22nd, 2008
0

Re: Linked List Delete

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
The list class does indeed do most of the work. All my list manipulation happens there (insert etc)

It is an ordered list, and the insert puts everything in the correct order. With the current files, the delete gives no outward appearance that it's doing anything and then the OS kills the file. I'm quite sure this has to do with what you're telling me.

I'll change the node class again and we'll see where we end up this time. Just a passing thought, could my contributor class files be interfering some how?
Anything's possible, but I haven't seen your updated node and list classes with the constructors and Insert and Delete functions. It's one of those things where you have to either post the new code each time or explain that you've done nothing to change the old code. Fastest way is probably to just zip all of the files each time and then post the zip file each time you post if you've changed anything at all.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 28th, 2008
0

Re: Linked List Delete

Vernon, thank you for your help. I finally figured it out. I was using assignment in a couple places where I meant to be doing comparison/equality
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: trying to print my Queue
Next Thread in C++ Forum Timeline: cirular arrys queue has me running in circles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC