讨论社区

在设计链表中出现内存越界,c新手,不知道哪里出问题,希望大家能帮我看看

我自己在c-free里也调试过了,可以通

typedef struct Node{

    int value;

    struct Node *next;

}Node;



typedef struct {

    struct Node *head;

} MyLinkedList;


/** Initialize your data structure here. */


MyLinkedList* myLinkedListCreate() {

    MyLinkedList *list = (MyLinkedList*)malloc(sizeof(MyLinkedList));

    list->head = NULL;

    return list;

}


/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */

int myLinkedListGet(MyLinkedList* obj, int index) {

    if(index<0) return -1;

    Node *current = obj->head;

    int i=0;

    while(current)

    {

        if(index==i)

            return current->value;

        current = current->next;

        i++;

    }

    return -1;

}


/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */

void myLinkedListAddAtHead(MyLinkedList* obj, int val) {

    Node *node = (Node *)malloc(sizeof(Node));

    if(node == NULL) return;

    node->value = val;

    node->next = obj->head;

    obj->head = node;

}


/** Append a node of value val to the last element of the linked list. */

void myLinkedListAddAtTail(MyLinkedList* obj, int val) {

    Node *node = (Node *)malloc(sizeof(Node));

    if(node == NULL) return;

    node->value = val;

    node->next = NULL;

    Node *current = obj->head;

    while(current)

    {

        if(current->next == NULL)

        {

            current->next = node;

            return;

        }

        current = current->next;

        

    }

}


/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */

void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {

    int i;

    if(index<0) return;

else if(index == 0) myLinkedListAddAtHead(obj,val);

 else 

    {

        Node *current = obj->head;

        Node *tmp = (Node *)malloc(sizeof(Node));

        tmp->value = val;

        tmp->next = NULL;

        for(i=1;i<index;i++)

        {

            current = current->next;

            if(current->next == NULL&&(i+1)<index)

                return;     

        }

        if(current->next == NULL)

        {

           current->next = tmp;

           return;

        }   

        tmp->next = current->next;

        current->next = tmp;

    }

}


/** Delete the index-th node in the linked list, if the index is valid. */

void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {

    int i;

    if(index<0) return;

    if(index == 0)

    {

        Node *tmp = obj->head;

        obj->head = tmp->next;

        free(tmp);

        tmp = NULL;

        return;

    }

   else 

   {

       Node *current = obj->head;

       Node *front = NULL;

       for(i=0;i<index;i++)

       {

           front = current;

           current = current->next;

           if(current->next == NULL&&(i+1)<index)

                return;

       }

       if(current->next == NULL)

        {

           front->next = NULL;

           free(current);

           current = NULL;

       }

      else

       {

           front->next = current->next;

           free(current);

           current = NULL;

       }

       

   }

}


void myLinkedListFree(MyLinkedList* obj) {

    Node *current = obj->head;

    while(current)

    {

        Node *tmp = current;

        free(tmp);

        tmp = NULL;

        current = current->next;

    }

}


/**

 * Your MyLinkedList struct will be instantiated and called as such:

 * MyLinkedList* obj = myLinkedListCreate();

 * int param_1 = myLinkedListGet(obj, index);

 

 * myLinkedListAddAtHead(obj, val);

 

 * myLinkedListAddAtTail(obj, val);

 

 * myLinkedListAddAtIndex(obj, index, val);

 

 * myLinkedListDeleteAtIndex(obj, index);

 

 * myLinkedListFree(obj);

*/


错误提示:

AddressSanitizer: heap-use-after-free on address 0x6020000002f8 at pc 0x000000402139 bp 0x7ffef2d8d3d0 sp 0x7ffef2d8d3c8


0 人关注了该问题 关注

0

力扣 (LeetCode) • 2周前

刷题学习中遇到问题,可以通过阅读「题解」解决做题中的一些困惑,也可至 力扣圈子 进行题目交流,以获得更好的支持哟~

0 个讨论

您需要登录后才可回复
您需要登录后才可以回复