Hi
Quote:
The code that is between the **** ****. If I swap the two lines , will it change anything?
Code:
if ( isEmpty() )
firstPtr = lastPtr = newPtr;
else {
**** newPtr->nextPtr = firstPtr;****
****firstPtr = newPtr;****
}
Yes it is a BIG problem!
if you interchange them then the value of the old 'firstPtr' will be lost forever with all the elemnts in the list too along it. Also the newPtr item will point to itself causing endelss loop when you try to traverce the list next time.
Quote:
''''''''lastPtr->nextPtr = newPtr;''''''''
'''''''lastPtr = newPtr;'''''''
Same differencece, except lastPtr now points to the new last node of the list?
Yes. Agian a BIG problem. Now the reason is that you will broke the list at that point. the old lastPtr->nextPtr element will point to nowhere and when you try to traverce the list again you will not complete it and also the last element will point to itself again and there will not be any reference to it at all.
Quote:
If I delete the line:##### value = tempPtr->data;######
the reason in it is to return the value that is kept by the NODETYPE when that list node is removed from the list. The user like to get back it's belongings when he remove the item from the list. :-)
there is no other way (as it is implemented) to retrive the elements stored in the list. The variable is passed by reference so the user will get it back at the end.
Quote:
+++++currentPtr->nextPtr = 0;++++++++++. If I remove this line what would happen? I know this sets the nextPtr to 0 in the last node of the list. Will this cause a printing problem?ie, print past the end of the list.
At this point the currentPtr->nextPtr points to the element that a few lines below, will be destroyed by delete opearot. so if you didn't detach it from the list, next time when you access the element pointed by currentPtr->nextPtr, it will point to a freed memory that do not belogs to any item in the list.
with the 0 assignment you indicate that there are no more elements in the list and the iteration thought the list elements should stop at that point.
Quote:
If I swap these 2 lines of code, I haven't the foggiest.
~~~ cout << currentPtr->data << ' ';~~~
~~~currentPtr = currentPtr->nextPtr;~~~
Imagine at the moment that you are at the very first element in the list (e.g. currentPtr is equal to firstPtr). then you will ommit the contents of the first element jumping to the next and printing it's contents.
The second reason is when the currentPtr is equal to lastPtr. When this happen, currentPtr will become 0 (since the value of the lastPtr->nextPtr is always 0 indicating the end of list) the following line
cout << currentPtr->data <<' ';
will cause GPF tryng to access memory at addres 0 which is not allowed.
Hope this helps.
damyan