我目前正试图从双向链表中删除一个节点,但是当只剩下一个项目时,它会在尝试在第11行(*sPtr)->prevPtr = NULL;
上删除它时引发访问冲突异常。这是我目前的删除功能:如何从双向链表中删除节点
char del(ListNodePtr *sPtr, 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 */
(*sPtr)->prevPtr = NULL;
if ((*sPtr)->nextPtr != NULL) {
free(tempPtr); /* free the de-threaded node */
}
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;
free(tempPtr);
return value;
} /* end if */
} /* end else */
return '\0';
}
编辑:我要补充我的主要功能和我的打印功能在低于为了什么,我试图这样做,我的问题都可以重新打开更好的背景:
这里是我与我的listNode结构主要功能:
struct listNode {
char data; /* each listNode contains a character */
struct listNode *nextPtr; /* pointer to next node*/
struct listNode *prevPtr; /* pointer to previous node*/
}; /* end structure listNode */
typedef struct listNode ListNode; /* synonym for struct listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */
/* prototypes */
void insert(ListNodePtr *sPtr, char value);
char del(ListNodePtr *sPtr, char value);
int isEmpty(ListNodePtr sPtr);
void printList(ListNodePtr currentPtr);
void printReverse(ListNodePtr currentPtr);
void instructions(void);
int main(void)
{
ListNodePtr startPtr = NULL; /* initially there are no nodes */
int choice; /* user's choice */
char item; /* char entered by user */
instructions(); /* display the menu */
printf("? ");
scanf("%d", &choice);
/* loop while user does not choose 3 */
while (choice != 3) {
switch (choice) {
case 1:
printf("Enter a character: ");
scanf("\n%c", &item);
insert(&startPtr, item); /* insert item in list */
printList(startPtr);
printReverse(startPtr);
break;
case 2:
/* if list is not empty */
if (!isEmpty(startPtr)) {
printf("Enter character to be deleted: ");
scanf("\n%c", &item);
/* if character is found, remove it */
if (del(&startPtr, item)) { /* remove item */
printf("%c deleted.\n", item);
printList(startPtr);
printReverse(startPtr);
} /* end if */
else {
printf("%c not found.\n\n", item);
} /* end else */
} /* end if */
else {
printf("List is empty.\n\n");
} /* end else */
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
} /* end switch */
printf("? ");
scanf("%d", &choice);
} /* end while */
printf("End of run.\n");
return 0; /* indicates successful termination */
} /* end main */
,这里是我的printReverse和的printList功能:
void printList(ListNodePtr currentPtr)
{
/* if list is empty */
if (currentPtr == NULL) {
printf("List is empty.\n\n");
} /* end if */
else {
printf("The list is:\n");
/* while not the end of the list */
while (currentPtr != NULL) {
printf("%c --> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
} /* end while */
printf("NULL\n\n");
} /* end else */
} /* end function printList */
void printReverse(ListNodePtr currentPtr)
{
/* if list is empty */
if (currentPtr == NULL) {
printf("List is empty.\n\n");
} /* end if */
else {
printf("The list in reverse is:\n");
while (currentPtr->nextPtr != NULL)
currentPtr = currentPtr->nextPtr;
/* while not the beginning of the list */
while (currentPtr != NULL) {
printf("%c --> ", currentPtr->data);
currentPtr = currentPtr->prevPtr;
} /* end while */
printf("NULL\n\n");
} /* end else */
} /* end function printList */
我真的希望这样可以更清楚地了解发生的一切事情,因为过去3天我一直在这个问题上停滞不前,而且我在网上可以找到很少或没有的主题,谈论如何去做我正在做的事情,因为该列表在插入和删除时按字母顺序排序。
因此,如果任何人可以尝试告诉我什么是错误的,以及为什么它会在尝试删除列表中的最后一项时在第11行抛出访问冲突异常,我将永远如此感激。谢谢!
未选中,很可能是因为'(*特征码) - > nextPtr',这是assinged到'* sPtr',是'NULL'因为只有一个节点离开了。 – MikeCAT
@MikeCAT我该在哪里解决这个问题? –
如果你没有头(单链表)和尾(都是双链表)指针,我会强烈推荐它们。我不知道没有他们的人如何链接列表。当列表中有0个或1个元素时,插入和删除节点是一种特殊情况,当维护头部和尾部指针时,我认为这更容易。虽然说过,我确信这里有人可以看中并在没有他们的情况下做链表。 – yano