When I start to run my code, it will stop anyway, why? Thanks for any help!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _list_item {
int value;
struct _list_item* next;
} list_item;
void del_item(list_item **root, int val);
void print_list(list_item *root);
int main(void)
{
list_item list[6],*lptr;
list[0].value=1; list[0].next=list+1;
list[1].value=2; list[1].next=list+2;
list[2].value=3; list[2].next=list+3;
list[3].value=4; list[3].next=list+4;
list[4].value=5; list[4].next=list+5;
list[5].value=6; list[5].next=0;
printf("test to delete list item whose value is 3\n");
del_item(&list, 3); // Wrong?
print_list(list);
return 0;
}
void del_item(list_item **root, int val)
{
list_item *currP, *prevP;
/* For 1st node, indicate there is no previous. */
prevP = NULL;
/*
* Visit each node, maintaining a pointer to
* the previous node we just visited.
*/
for (currP = *root;
currP != NULL;
prevP = currP, currP = currP->next) {
if (currP->value == val) { /* Found it. */
if (prevP == NULL) {
/* Fix beginning pointer. */
*root = currP->next;
} else {
/* Fix previous node's next to
* skip over the removed node.
*/
prevP->next = currP->next;
}
/* Deallocate the node.*/
free(currP);
/* Done searching. */
return;
}
}
}
void print_list(list_item *root)
{
list_item* lp=root;
while(lp) {
printf("%d\n", lp->value);
lp=lp->next;
}
}