| | |
Help with Linked list Delete function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
Hello, I'm in the middle of writing a doubly linked list, I have the print in reverse working fine and my insert function working fine, I just can't seem to find what I'm doing wrong with the delete function.. WHen i try to delete the last letter in the node.. The reverse will display the list as empty, and as of right now only the first character will delete correctly, I just can't seem to figure out what to add into my function
Here's what I have so far:
char delete ( ListNodePtr *sPtr, ListNodePtr *tailPtr, char value )
{
ListNodePtr previousPtr; /* pointer to previous node on list */
ListNodePtr currentPtr; /* pointer to current node on list */
ListNodePtr tempPtr; /* temperary node pointer */
/* delete first node */
if (value == ( *sPtr )->data) {
tempPtr = *sPtr; /* hold onto node being removed */
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
*tailPtr = ( *tailPtr)->prevPtr;
free( tempPtr ); /* free the de-threaded node */
return value;
} /* end if */
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
previousPtr = *tailPtr;
currentPtr->prevPtr = previousPtr;
/* loop to find correct location on list */
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr; /* walk to ....*/
currentPtr = currentPtr->nextPtr; /*....next node*/
} /* end while */
/* delete node at currentPtr */
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
currentPtr = previousPtr;
free ( tempPtr );
return value;
} /* end if */
}
Here's what I have so far:
char delete ( ListNodePtr *sPtr, ListNodePtr *tailPtr, char value )
{
ListNodePtr previousPtr; /* pointer to previous node on list */
ListNodePtr currentPtr; /* pointer to current node on list */
ListNodePtr tempPtr; /* temperary node pointer */
/* delete first node */
if (value == ( *sPtr )->data) {
tempPtr = *sPtr; /* hold onto node being removed */
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
*tailPtr = ( *tailPtr)->prevPtr;
free( tempPtr ); /* free the de-threaded node */
return value;
} /* end if */
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
previousPtr = *tailPtr;
currentPtr->prevPtr = previousPtr;
/* loop to find correct location on list */
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr; /* walk to ....*/
currentPtr = currentPtr->nextPtr; /*....next node*/
} /* end while */
/* delete node at currentPtr */
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
currentPtr = previousPtr;
free ( tempPtr );
return value;
} /* end if */
}
Wouldn't it be simpler to just use a pointer to structure instead of a pointer to a pointer to a structure?
I think your problem lies here:
What's that piece of code doing?
I think your problem lies here:
•
•
•
•
Originally Posted by dampecram
c Syntax (Toggle Plain Text)
previousPtr = *tailPtr; currentPtr->prevPtr = previousPtr;
c Syntax (Toggle Plain Text)
tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; currentPtr->nextPtr->prevPtr = previousPtr //It's a doubly linked list, remember? so gotta assign prevPtr too
Last edited by devnar; Nov 1st, 2008 at 4:09 am.
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
previousPtr = *tailPtr;
currentPtr->prevPtr = previousPtr;
that line oringinally when in my code deleted the last character i thought when i printed the list out in reverse, i just messed around with that code and it ended up not doing any good at all, so you were right., When i put your line in the code.. You can delete from the middle of the list which is working great.. but now I'm having trouble delting the first and last node of the list when i go to print the list out in reverse. That line i had just gives an error so I don't think its needed in the code anymore, I got rid of it.
currentPtr->prevPtr = previousPtr;
that line oringinally when in my code deleted the last character i thought when i printed the list out in reverse, i just messed around with that code and it ended up not doing any good at all, so you were right., When i put your line in the code.. You can delete from the middle of the list which is working great.. but now I'm having trouble delting the first and last node of the list when i go to print the list out in reverse. That line i had just gives an error so I don't think its needed in the code anymore, I got rid of it.
Last edited by dampecram; Nov 1st, 2008 at 1:16 pm.
•
•
Join Date: Sep 2008
Posts: 1,647
Reputation:
Solved Threads: 206
Wait until someone verifies this advice, but back in the day, I did a doubly linked list program. I made the delete function somewhat like you did, and my program didn't work correctly. I think it should be declared as
char delete ( ListNodePtr **sPtr, ListNodePtr **tailPtr, char value )
Anyone... verify or contradict that suggestion?
(Don't take this advice until someone experienced elaborates)
char delete ( ListNodePtr **sPtr, ListNodePtr **tailPtr, char value )
Anyone... verify or contradict that suggestion?
(Don't take this advice until someone experienced elaborates)
•
•
•
•
Originally Posted by BestJewSinceJC
char delete ( ListNodePtr **sPtr, ListNodePtr **tailPtr, char value )
c Syntax (Toggle Plain Text)
struct node { char data; struct node* nextPtr; struct node* prevPtr; }; typedef struct node* ListNodePtr;
•
•
•
•
Originally Posted by dampecram
but now I'm having trouble delting the first and last node of the list when i go to print the list out in reverse.
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
yes that's how my structure is defined, and deleting is disrupting how my list is displayed because I'm missing a link to my prevPtr. My prevPtr works fine for deleting from the middle of the list, but when i try to delete the first node or last node in the list.. When my list goes to display in reverse, I get an error.
here is my printbackwards
void printBackwards ( ListNodePtr currentPtr )
{
ListNodePtr temp = NULL;
while ( currentPtr != NULL ) {
temp = currentPtr;
currentPtr = currentPtr->nextPtr;
}
printf( "\nThe list in reverse is:\n" );
printf( "NULL" );
currentPtr = temp;
while ( currentPtr != NULL) {
printf( " <-- %c", currentPtr->data );
currentPtr = currentPtr->prevPtr;
}
printf("\n\n");
} /* end function printBackwards */
here is my printbackwards
void printBackwards ( ListNodePtr currentPtr )
{
ListNodePtr temp = NULL;
while ( currentPtr != NULL ) {
temp = currentPtr;
currentPtr = currentPtr->nextPtr;
}
printf( "\nThe list in reverse is:\n" );
printf( "NULL" );
currentPtr = temp;
while ( currentPtr != NULL) {
printf( " <-- %c", currentPtr->data );
currentPtr = currentPtr->prevPtr;
}
printf("\n\n");
} /* end function printBackwards */
I don't see any fault in your code, but make sure you've assigned the prevPtr of the first node to null while inserting for the first time. Maybe someone more experienced would take a look at your code if you had used the freakin CODE TAGS the second time at least.
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
I made sure that prevPtr was assigned to NULL in the insert function. The problem now is when I delete the first node in the list. When the list is printed out in reverse after I choose to delete the first node in the list.. The list comes up empty.. and when I try to delete the last node in the list.. The program freezes. but the program works fine when i delete from anywhere in the middle of the list.
here is my delete and print backwards function again ( sorry about previous post without code tags )
here is my delete and print backwards function again ( sorry about previous post without code tags )
C Syntax (Toggle Plain Text)
char delete ( ListNodePtr *sPtr, ListNodePtr *tailPtr, char value ) { ListNodePtr previousPtr; /* pointer to previous node on list */ ListNodePtr currentPtr; /* pointer to current node on list */ ListNodePtr tempPtr; /* temperary node pointer */ /* delete first node */ if (value == ( *sPtr )->data) { tempPtr = *sPtr; /* hold onto node being removed */ *sPtr = ( *sPtr )->nextPtr; /* de-thread the node */ *tailPtr = ( *tailPtr)->prevPtr; free( tempPtr ); /* free the de-threaded node */ return value; } /* end if */ else { previousPtr = *sPtr; currentPtr = ( *sPtr )->nextPtr; /* loop to find correct location on list */ while ( currentPtr != NULL && currentPtr->data != value ) { previousPtr = currentPtr; /* walk to ....*/ currentPtr = currentPtr->nextPtr; /*....next node*/ } /* end while */ /* delete node at currentPtr */ if ( currentPtr != NULL ) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; currentPtr = previousPtr; currentPtr->nextPtr->prevPtr = previousPtr; free ( tempPtr ); return value; } /* end if */ }
C Syntax (Toggle Plain Text)
void printBackwards ( ListNodePtr currentPtr ) { ListNodePtr temp = NULL; while ( currentPtr != NULL ) { temp = currentPtr; currentPtr = currentPtr->nextPtr; } printf( "\nThe list in reverse is:\n" ); printf( "NULL" ); currentPtr = temp; while ( currentPtr != NULL) { printf( " <-- %c", currentPtr->data ); currentPtr = currentPtr->prevPtr; } printf("\n\n"); } /* end function printBackwards */
In your delete function what does the code in line 12 do? That looks like trouble. In the printBackwards function, currentPtr points to the starting node when you call the function, right? If you've forgotten to assign that, it'll cause trouble. Other than that, it looks to work fine to me.
On another note, when using code tags, it's [CODE=C], not [CODE=SYNTAX].
On another note, when using code tags, it's [CODE=C], not [CODE=SYNTAX].
![]() |
Similar Threads
- Q: in Link List (Daubly Linked List) (C++)
- adding and deleting node in a linked list (C++)
- Sorted linked list help (C++)
- The C++ LINKED LIST (C++)
- remove method linked list (C)
- linked list small problem with Insert Function (C++)
- Seg Fault ~ Linked List Delete (C)
- I need help with a linked list program (C++)
- Linked List (C++)
Other Threads in the C Forum
- Previous Thread: please explain
- Next Thread: Vector Calculator issues (I got the code)
Views: 1646 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






