c:自由函数

c:自由函数,c,linked-list,C,Linked List,我想删除列表(释放内存)时出现问题。 如果列表是序列,则代码是快捷方式列表。 顺序=相同的数字,然后三个数字等于。 找到序列后返回新的长度 例如: Before: 3,3,3,3,2 After: -4,3,2 New length : 3 我编写的代码给出了一个很好的结果,但是当我使用Delete\u List函数时,程序崩溃了 node *Find_Sequence(node *L) { node *headSeq = NULL, *tailSeq = NULL, *currL =

我想删除列表(释放内存)时出现问题。 如果列表是序列,则代码是快捷方式列表。 顺序=相同的数字,然后三个数字等于。 找到序列后返回新的长度

例如:

Before: 3,3,3,3,2
After: -4,3,2
New length : 3
我编写的代码给出了一个很好的结果,但是当我使用
Delete\u List
函数时,程序崩溃了

node *Find_Sequence(node *L) {
    node *headSeq = NULL, *tailSeq = NULL, *currL = L;
    int flag = 0, cnt = 3;
    while (currL != NULL) {
        if (currL->next != NULL && currL->data == currL->next->data) {
            if (currL->next->data == currL->next->next->data) {
                flag = 1; // if flag = 1 it means that have a sequence, 
                          // sequence: same number up and equal to 3.
                headSeq = currL;
                tailSeq = currL->next->next;
                break;
            }   
        }
        currL = currL->next;
    }

    while (tailSeq != NULL) { // to find if hace more then 3 in the sequence and to know how many nodes to delete
        if (tailSeq->next != NULL && tailSeq->data == tailSeq->next->data)
            cnt++;
        else {
            //tailSeq->next = NULL;
            break;
        }
        tailSeq = tailSeq->next;
    }

    if (headSeq != NULL) {
        headSeq = Delete_Node(headSeq, tailSeq, cnt - 2);  // cnt-2 because i want to stay a place for the head(-k) and for the tail (x)
        headSeq->data = -1 * cnt;
        if (headSeq != NULL)
            headSeq = tailSeq;
    }
    return L;
}

node *Delete_Node(node *head, node *tail, int length) {
    node *h = head, *t = tail, *curr = head, *temp = NULL;
    int i;

    for (i = 0; i < length; i++) {
        temp = h;
        h = h->next;
        free(temp);
    }

    if (head != NULL)
        head->next = tail;

    return head;
}

int new_length(node *L) {
    node *new_list = Find_Sequence(L);
    int counter = 0;
    while (new_list != NULL) {
        counter++;
        new_list = new_list->next;
    }
    return counter;
}

node *Delete_List(node *L) {
    node *next;
    while (L != NULL) {
        next = L;
        L = L->next;
        free(next);
    }
    return L;
}

int main() {
    node *list1 = NULLL;   // empty lists
    int len;

    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 5);
    list1 = Creat_List(list1, 4);

    printf("Before:");
    Print_List(list1);
    len = new_length(list1);
    printf("The new length is: %d\n", len);
    printf("After:");
    Print_List(list1);
    printf("\n");

    Delete_List(list1);
}
node*Find_序列(node*L){
节点*headSeq=NULL,*tailSeq=NULL,*currL=L;
int flag=0,cnt=3;
while(currL!=NULL){
如果(currL->next!=NULL&&currL->data==currL->next->data){
如果(当前->下一步->数据==当前->下一步->下一步->数据){
flag=1;//如果flag=1,则表示有一个序列,
//顺序:相同的数字,等于3。
headSeq=电流;
tailSeq=当前->下一步->下一步;
打破
}   
}
currL=currL->next;
}
while(tailSeq!=NULL){//查找序列中是否有多于3个的节点,并知道要删除多少个节点
if(tailSeq->next!=NULL&&tailSeq->data==tailSeq->next->data)
cnt++;
否则{
//tailSeq->next=NULL;
打破
}
tailSeq=tailSeq->next;
}
如果(headSeq!=NULL){
headSeq=Delete_节点(headSeq,tailSeq,cnt-2);//cnt-2因为我想为头部(-k)和尾部(x)保留一个位置
headSeq->data=-1*cnt;
如果(headSeq!=NULL)
headSeq=tailSeq;
}
返回L;
}
节点*删除\节点(节点*头,节点*尾,整数长度){
节点*h=头部,*t=尾部,*curr=头部,*temp=NULL;
int i;
对于(i=0;inext;
免费(临时);
}
if(head!=NULL)
头部->下一个=尾部;
回流头;
}
int新长度(节点*L){
节点*new_list=查找_序列(L);
int计数器=0;
while(新列表!=NULL){
计数器++;
新建列表=新建列表->下一步;
}
返回计数器;
}
节点*Delete_列表(节点*L){
节点*下一步;
while(L!=NULL){
next=L;
L=L->next;
免费(下一个);
}
返回L;
}
int main(){
node*list1=null;//空列表
内伦;
列表1=创建列表(列表1,3);
列表1=创建列表(列表1,3);
列表1=创建列表(列表1,3);
列表1=创建列表(列表1,5);
列表1=创建列表(列表1,4);
printf(“之前:”);
打印列表(列表1);
len=新长度(列表1);
printf(“新长度为:%d\n”,len);
printf(“之后:”);
打印列表(列表1);
printf(“\n”);
删除列表(列表1);
}

释放链接列表的顺序应为:

node * next;
while (L != NULL)
{
    next = L->next;
    free(L);
    L = next;
}
事实上,当您在Delete_节点或Delete_列表函数中执行以下代码行时(在while循环中):

你实际上是在释放下一个指向L的点,下一个指向L->next,所以你永远不会释放列表的标题。

问题(1)

Delete_节点
将已
免费
'd节点作为
返回
例如,更改为保留第一个元素,如下所示

node *Delete_Node(node * head, node * tail , int length){
    node *h = head->next, *temp;
    int i;

    for (i = 0; h &&  i < length; i++){
        temp = h;
        h = h->next;
        free(temp);
    }

    head->next = tail;

    return head;
}

“我有个问题”。你需要告诉我们到底是什么问题。程序崩溃了吗?它是否产生了正确的结果?ETC程序崩溃,并在调试程序@Kaylumse处给出良好的结果。如果您需要这里的帮助,请提供帮助。也就是说,将代码减少到再现问题所需的最小行数。你甚至可以在这个过程中自己发现问题。我发现调试器有问题,但不明白为什么。这个节目免费送我(下一个);在Delete_List(node*L)函数中。您的
Delete_节点
函数将释放节点,但不会将它们从列表中取消链接。因此,
Delete\u List
很可能再次释放相同的节点。
node *Delete_Node(node * head, node * tail , int length){
    node *h = head->next, *temp;
    int i;

    for (i = 0; h &&  i < length; i++){
        temp = h;
        h = h->next;
        free(temp);
    }

    head->next = tail;

    return head;
}
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
} node;

node *Creat_List(node *L, int value){
    node *current = L , *new_node = malloc(sizeof(node));
    new_node->data = value;
    new_node->next = NULL;

    if (L == NULL) {
        L = new_node;
    } else {
        while (current->next != NULL)
            current = current->next;
        current = current->next = new_node;
    }
    return L;
}

node *Delete_Node(node *head, node *end){
    //top node not delete
    node *curr = head->next;

    while(curr != end){
        node *temp = curr;
        curr = curr->next;
        free(temp);
    }
    head->next = end;

    return head;
}

int sameLength(node *L, node **next){
    node *curr = L;
    int count = 0;
    while(curr && curr->data == L->data){
        ++count;
        curr = curr->next;
    }
    *next = curr;
    return count;
}

node *Find_Sequence(node *L){
    node *curr = L, *next = NULL;
    int cnt;

    while (curr){
        cnt = sameLength(curr, &next);
        if(cnt > 2){
            Delete_Node(curr->next, next);//Leave curr->next
            curr->data = -cnt;
        }
        curr = next;
    }

    return L;
}

int new_length(node *L){
    node *new_list = Find_Sequence(L);
    int counter = 0;
    while (new_list){
        counter++;
        new_list = new_list->next;
    }

    return counter;
}

node *Delete_List(node * L){
    node *temp;
    while (L != NULL){
        temp = L;
        L = L->next;
        free(temp);
    }

    return NULL;
}

void Print_List(node * head){
    while (head != NULL){
        printf("%4d", head->data);
        head = head->next;
    }
}

int main(void){
    node *list1=NULL, *list2=NULL , *list3=NULL;
    int len;
    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 3);
    list1 = Creat_List(list1, 5);
    list1 = Creat_List(list1, 4);

    printf("Before:");
    Print_List(list1);
    len = new_length(list1);
    printf("\nThe new length is: %d\n", len);
    printf("After:");
    Print_List(list1);
    printf("\n");
    Delete_List(list1);

    list2 = Creat_List(list2, 2);
    list2 = Creat_List(list2, 3);
    list2 = Creat_List(list2, 3);
    list2 = Creat_List(list2, 3);
    list2 = Creat_List(list2, 3);
    list2 = Creat_List(list2, 4);

    printf("Before:");
    Print_List(list2);
    len = new_length(list2);
    printf("\nThe new length is: %d\n", len);
    printf("After:");
    Print_List(list2);
    printf("\n");

    list3 = Creat_List(list3, 3);
    list3 = Creat_List(list3, 3);
    list3 = Creat_List(list3, 5);
    list3 = Creat_List(list3, 5);
    list3 = Creat_List(list3, 5);
    list3 = Creat_List(list3, 5);
    list3 = Creat_List(list3, 5);

    printf("Before:");
    Print_List(list3);
    len = new_length(list3);
    printf("\nThe new length is: %d\n", len);
    printf("After:");
    Print_List(list3);
    printf("\n");
}