Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi ,
Please help me to make me know if there is any bug in writing a function in deleting the nodes of a linked list.

C#
#include <iostream>
#include <cstdlib>


///////////////////////////////////////////////////////////////////////////////////////////
typedef struct Clist                                            // Double linked list    //
{
        struct Clist *Prior;
        char         cByte;
        struct Clist *Next;
} Clist;
///////////////////////////////////////////////////////////////////////////////////////////


void DeleteFrom( Clist** clpInput )
{
        ///////////////////////////////////////////////////////////////////////////////////
        Clist* clpTmpA, * clpTmpB, * clpTmpC;
        ///////////////////////////////////////////////////////////////////////////////////
        clpTmpA = *clpInput;
        while( clpTmpA != NULL )
        {
                clpTmpB = clpTmpA->Next;
                free(clpTmpA->Next);
                clpTmpA = clpTmpB;
        }
}
Posted
Comments
Legor 6-Sep-11 7:06am    
Have you tested it? Shouldn't take too long to figure out if its working correctly.
[no name] 6-Sep-11 7:09am    
i have tested it and it does not work in deleting the nodes.
Philippe Mori 6-Sep-11 20:51pm    
Uses a debugger, draw a graph of what you have... and then step by step check if what you are doing it right.

This smells a lot like homework to me, which we try not to give easy answers to.

But I'll try and hint a bit.

If you have list nodes A<->B<->C, and you're removing B, then you need a pointer to A and C, delete B, then connect A->C and C->A.

Other than that, I'd recommend putting a break point at the beginning of your code, and following it line by line.

Good luck with your studies,

Iain.
 
Share this answer
 
You need to delete all nodes? Then change
C++
clpTmpB = clpTmpA->Next;
free(clpTmpA->Next);
clpTmpA = clpTmpB;

to
C++
clpTmpB = clpTmpA->Next;
free(clpTmpA);
clpTmpA = clpTmpB;
 
Share this answer
 
Comments
[no name] 6-Sep-11 7:11am    
but still it does not work!!! please help
Timberbird 6-Sep-11 7:15am    
What are your input and output?
[no name] 6-Sep-11 7:30am    
its just a linked list i have assigned values after which i m passing to this function for deleting the nodes.
Timberbird 6-Sep-11 7:44am    
"Then just delete them" would be an answer :). How do you fill the list? Do you assign both Prior and Next node? How do you check whether there are still items in the list?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900