![]() |
|
#1
|
|||
|
|||
linked list problemThis is my list declaration
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Last edited by dsmith : 10-Nov-2004 at 07:01.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
|||
|
|||
|
another question:
P is a pointer is there any diffrences between free(P); with P = NULL; |
|
#3
|
||||
|
||||
|
Hi if13121. I will take a look at your question and see if I can come up with something. However, it is very important that you start using the code tags to post your code. It makes it very difficult to read your code without them and therefore you may not be getting anyone responding.
It is very simple to do. Simply enclose your c code between [c] and [/c] tags. If that doesn't make sense, hit the reply button on your first post and you will see how I editted your post with these markup codes. This will be a big help to me, but more importantly to you as more people will take the time to read your posts (help me to help you... )Thanks! __________________
The best damn Sports Blog period. |
|
#4
|
||||
|
||||
|
Quote:
Yes, most definitely. with free(P) what you are doing is releasing the memory that had been allocated for P back into the memory pool for future use. By setting P to NULL, you do not free this memory. The only time that you should do this, is when you haven't allocated memory for P and you want it marked to know that. This is actually a good idea as a call to free(P) will fail if the memory is not allocated unless, P points to NULL. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
Hi,
Pls show us how you are calling "DeleteLast()" in parent function and how you are passing the values and how those values are declared. This will give us more insite on problem. Also, if you can post the sample output of your code when you run it for deleting, that would be great. Thanks, |
|
#6
|
|||
|
|||
|
Hi,
I developed the parent function using my own idea. It worked for me.Following is the code for my parent function main(). I modified the return type of your DeleteLast() function to return true or false based on sucess or failure. This is not a final solution. This is just to give some idea about it. CPP / C++ / C Code:
It could be the problem with the way your are passing the values in parent function. Just a guess. Following is my output Quote:
Thanks, P.S. Use lots of printf() in your code for debugging purpose. Its a good tool to understand the flow of your program and pin point the location in your code where its not working. |
|
#7
|
|||
|
|||
|
Quote:
if I assign null to a pointer, does it mean that every other pointer that points to the same location as that pointer pointed to will become null? |
|
#8
|
|||
|
|||
|
Quote:
CPP / C++ / C Code:
when i changed function DeleteLast above become: CPP / C++ / C Code:
![]() |
|
#9
|
||||
|
||||
|
Quote:
No. When you sent a pointer to NULL, you are just pointing it to nothing. The memory that you have defined at that position stays allocated with the same value, etc. Here is a little program that may explain it better. CPP / C++ / C Code:
HTH. __________________
The best damn Sports Blog period. |
|
#10
|
|||
|
|||
|
It still prints the deleted value because you are not updating tail of list when you delete the last item in the list. It still keeps pointing to the allocated memory. The previous node to the last is still pointing to the last node. You are only NULL terminating the last node. You still need to update previous node to the last to point to NULL so that your print function stops there (after hitting NULL).
Also your earlier DeleteLast() function could cause a memory leak becuase in DeleteLast() you are only printing out the last value not freeing it. Memory is not released by the program as it is supposed to be, after deleting the element. But since you are not actually deleting and freeing the memory , you are safe cause that part of memory is still owned by your program. But its a waste since you are no longer going to refer to it. Otherwise you could have seen the crash. I have updated your DeleteLast() function to incorporate the above required changes. CPP / C++ / C Code:
Following is the output I got when I compiled and ran the program with above function changes. Quote:
|
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with the function creating updating a linked list | nkhambal | C Programming Language | 3 | 28-Oct-2004 20:45 |
| bus error in linked list | picknicker187 | C Programming Language | 2 | 08-Oct-2004 10:44 |
| Insert problem in Linked list | Kay Chan | C Programming Language | 1 | 03-Sep-2004 17:06 |
| Insert problem in linked list with two function code | Kay Chan | C++ Forum | 1 | 03-Sep-2004 09:52 |
| help on linked lists any1????? | nick4 | C Programming Language | 1 | 17-May-2004 09:32 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The