now i am working on the delete function but no matter what how many things i try i always end up with pointers pointing to deleted nodes making errors in the memory. uhh, i honestly understand what i have to do and how to code it, but i cant figure out where to update the pointers at.
sorry for the wall of text, code is below: delete function is commented and is near the bottom
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct listNode {
char data;
struct listNode *nextPtr;
struct listNode *prevPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
void insert( ListNodePtr *sPtr, ListNodePtr *tPtr, char value );
char deletechar( ListNodePtr *sPtr, ListNodePtr *tPtr, char value);
int isEmpty( ListNodePtr sPtr);
void printList( ListNodePtr currentPtr );
void printBack( ListNodePtr currentPtr );
void instructions(void );
int main(int argc, char *argv[])
{
ListNodePtr startPtr=NULL;
ListNodePtr tailPtr=NULL;
int choice;
char item;
instructions();
printf("? ");
scanf("%d", &choice);
while(choice !=3){
switch(choice){
case 1:
printf("Enter a character: ");
scanf("\n%c", &item);
insert(&startPtr, &tailPtr, item);
printList(startPtr);
printBack(tailPtr);
break;
case 2:
if(!isEmpty(startPtr)){
printf("Enter a character to be deleted: ");
scanf("\n%c", &item);
if(deletechar(&startPtr, &tailPtr, item)){
printf("%c deleted.\n", item);
printList(startPtr);
printBack(tailPtr);
}
else{
printf("%c not found.\n\n", item);
}
}
else{
printf("List is empty.\n\n");
}
break;
default:
printf("invalid choice. \n\n");
instructions();
break;
}
printf("? ");
scanf("%d", &choice);
}
printf("end of run.\n");
return 0;
}
void instructions(void){
printf("Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n");
}
void insert( ListNodePtr *sPtr, ListNodePtr *tPtr, char value){
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = (ListNodePtr)malloc( sizeof( ListNode ) );
if(newPtr!=NULL){
newPtr->data=value;
newPtr->nextPtr=NULL;
previousPtr=NULL;
currentPtr=*sPtr;
newPtr->prevPtr=NULL;
while(currentPtr !=NULL&&value > currentPtr->data){
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(previousPtr==NULL){ // inserted at beginning of list
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
*tPtr=newPtr;
}
else{
if(currentPtr!=NULL){ // inserted in the middle
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
currentPtr->prevPtr=newPtr;
newPtr->prevPtr=previousPtr;
}
else{//inserted at end of list
*tPtr=newPtr;
previousPtr->nextPtr=newPtr;
newPtr->prevPtr=previousPtr;
}
}
}
else{
printf("%c not inserted. no memory available.\n", value);
}
}
char deletechar( ListNodePtr *sPtr, ListNodePtr *tPtr, char value){
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */
ListNodePtr tempPtr; /* temporary node pointer */
/* delete first node */
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr; /* hold onto node being removed */
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
free( tempPtr ); /* free the de-threaded node */
//*tPtr=NULL; not sure if this needs a if statement...or what??
return value;
} /* end if */
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
/* loop to find the correct location in the 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;
//previousPtr->prevPtr = currentPtr->prevPtr; this should be correct as well, but its not working bc i think im missing something
free( tempPtr );
return value;
}/* end if */
} /* end else */
return '\0';
} /* end function delete */
int isEmpty(ListNodePtr sPtr){
return sPtr==NULL;
}
void printList(ListNodePtr currentPtr){
if(currentPtr==NULL){
printf("List is empty.\n\n");
}
else{
printf("the list is:\n");
while(currentPtr !=NULL){
printf("%c --> ", currentPtr->data );
currentPtr=currentPtr->nextPtr;
}
printf("NULL\n\n");
}
}
void printBack(ListNodePtr currentPtr){
if(currentPtr==NULL){
printf("List is empty.\n\n");
}
else{
printf("the list in reverse is:\n");
while(currentPtr !=NULL){
printf("%c --> ", currentPtr->data );
currentPtr=currentPtr->prevPtr;
}
printf("NULL\n\n");
}
}

New Topic/Question
Reply



MultiQuote





|