0

我一直被困在很长一段时间的分段错误上。我声明了一个带有指向字符串的指针的结构。我写了两个函数,create 和 remove 来操作值。结构如下:

#include "filename.h"  
//*in filename.h:* typedef struct linkNode linkNode_t;

struct linkNode{
    struct linkNode *next;
    char *value;
};

create 函数会先为节点分配内存,然后为值分配内存,然后将输入的值复制到值字段中:

linkNode_t* create(char* stuff){
    linkNode_t *ptr=malloc(sizeof(linkNode_t));
    if(ptr==NULL){
        printf("malloc failure");
        return NULL;
    }
    char* tempvalu=malloc(sizeof(char)*strlen(stuff)+1);
    if(tempvalu==NULL){
        printf("malloc failure");
        return NULL;
    }
    strcpy(tempvalu,stuff);
    ptr->next=NULL;
    ptr->value=tempvalu;
    return ptr;
}

一个函数用于将一个节点插入到链表中:

linkNode_t* insertLast(linkNode_t* start, linkNode_t* newNode){
    linkNode_t* current=start;
    while(current->next!=NULL){
        current=current->next;
    }
//now current points to the last element in the linked list
    current->next=newNode;
    return start;
}

引起我问题的部分如下:

linkNode_t* removebyValue(linkNode_t* start, char* valu){
/**removes the first instance of a node with a certain value. Return *start after     removing.
    if linked list becomes empty, return NULL*/

    linkNode_t *current=start;
    linkNode_t *previous=start;
    while(current!=NULL){
        if(strcmp(valu,current->value)==0) {//found the node to delete
            if(current==start){//removing the head
                linkNode_t* retvalue= current->next;
                free(current->value);
                free(current);
                return retvalue;
            }
            else{   //removing other elements in the linked list
                previous->next=current->next;
                free(current->value);
                free(current);
                return start;
            }
        }
        else{
            previous=current;
            current=current->next;
        }
    }
    return start;
}

在 Main 中,我创建了一个包含两个元素 1 和 2 的链表,并在发生分段错误时尝试释放元素 1。

int main(){
    linkNode_t *pt1=create("1");
    pt1=insertLast(pt1,create("2"));
    removebyValue(pt1,"1"); //Causes seg fault. If I replace "1" by "2" nothing happens 

有人可以对此提出一些建议吗?提前致谢

编辑:我放了所有可能相关的代码,因为有人说我放的部分没有错误

4

2 回答 2

2

我认为您在正确维护开始指针的同时过度考虑删除节点。考虑一种希望更简单的方法。

typedef struct node_t 
{
    struct node_t* next;
    char* value;
} node_t;

node_t* remove(node_t *start, const char* valu)
{
    node_t* current=start;
    node_t* prev=NULL;

    while(current && strcmp(current->value, valu))
    {
        prev = current;
        current = current->next;
    }

    if (current)
    {
        if (prev) // we're not deleting start node
            prev->next = current->next;

        else      // we *are* deleting start node
            start = current->next;

        // now the node is unlinked. remove it.
        free(current->value);
        free(current);
    }
    return start;
}
于 2013-01-27T23:54:27.513 回答
1

这是一个可以正常工作的替代测试代码,请尝试一下,看看它是否有帮助。此外,您可以添加

typedef struct node_t {
    struct node_t* next;
    char* value;
} node;

这似乎更容易理解,但这并不是因为 typedef 的性质令人困惑。我强烈建议你看一下https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle 这是linux内核的编码风格,很短很简单,不是特别规律,但是值得注意的是……

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node_t {
    struct node_t* next;
    char* value;
};

struct node_t* create(const char* istr) 
{
    struct node_t* ptr = (struct node_t*)malloc(sizeof(struct node_t));
    char* tmp = (char*)malloc(sizeof(char) * (strlen(istr) + 1));

    strcpy(tmp, istr);
    ptr->next = 0;
    ptr->value = tmp;
    return ptr;
}

struct node_t* remove(struct node_t* start, const char* value)
{
    struct node_t* current = start;
    struct node_t* prev = start;

    while (current != 0) {
        if (!strcmp(value, current->value)) {
            if (current == start) {
                struct node_t* retval = current->next;
                free(current->value);
                free(current);
                return retval;
            } else {
                /* nothing happens */
                return 0;
            }
        }
    }
}

int main(const int argc, const char** argv)
{
    struct node_t* pt = create("1");
    printf("%s\n", pt->value);
    pt->next = create("2");
    printf("%s\n", pt->next->value);
    remove(pt, "1");    

    return 0;
}
于 2013-01-27T23:55:05.043 回答