开发者

linked list delete function

The following code snippet is not working right.

void deleteNode(list **start, int pos) {
    int currentPosition=0;
    list *currentNode;
    list *nodToDelete;

    currentNode = *start;
    if (currentNode == NULL) {
        printf("Empty List\n");
    } else if (pos == 0 ) {
        nodToDelete = *start;
        *start = nodToDelete->next;
        free(nodToDelete);
    } else {
        while (currentNode->next != NULL) {
            if (currentPosition >= pos -1) {
                break;
            }
            currentPosition++;
            currentNode = currentNode->next;
        }
        if (currentPosition < pos -1 || currentNode->next == NULL) {
            printf("No node at given position exists\n");
        } else {
            nodToDelete = currentNode->next;
            currentNode = nodToDelete->next;
            free(nodToDelete);
            nodToDelete = NULL;
        }
    }
}

void displayList(list *node) {
    if (node == NULL) {
        printf("Empty List");
    }

    while (node != NULL) {
        printf("%d\t", node->data);
        node = node->next;
    }
    printf("\n");
}

int main()
{
    list *start, *node;
    start = NULL;

    insertNode(&start, 2);
    insertNode(&start, 3);
    insertNode(&start, 4);
    insertNode(&start, 1);
    insertNode(&start, 5);

    deleteNode(&start, 3);

    displayL开发者_Python百科ist(start);
}

When executed the output is

Before Deletion 2 3 4 1 5

After Deletion 2 3 4 0 5

It is supposed to delete 1 but it is inserting 0 at its place.


Here is something that might work -- Replace

currentNode = nodToDelete->next;

with

currentNode->next = nodToDelete->next;

You basically need the node before the nodetodelete to have its next to point to the node that nodetodelete used to point to


Once you've found the node you want to take out of the list, you need to actually take it out. =)

...

nodToDelete = currentNode->next;
currentNode->next = nodToDelete->next;
free(nodToDelete);

...


Besides the problem with currentNode->next = nodToDelete->next; and negative positions you are mixing your ui and your logic. As much as possible you should separate the two.

Sending something to the ui is a way of reporting progress; whether the ui is a command line, a browser or a speaker. Within deleteNode, an empty list or a position that is out of bounds, is not progress. Sequentially both are the same as success - you are done. If you want failure to be to be reported, that should be done where it can lead to a separate sequence...i.e the caller. Also, by mixing in ui, you introduce an unnecessary dependency and failure (what if there's a bug in printf, YOUR function will crash when it doesn't doesn't have to). If you're function returns a defined result, the caller can decide if/how to report that result, including success (your function currently doesn't do so, and the caller has no way telling the difference between sucess or failure).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜