c - 如何按字母顺序在列表中插入结构体

标签 c list struct

嗯,我有一个代码可以按字母顺序在列表中插入一个新结构,但我遇到了一个小问题。我只需让它工作有一个函数,当我调用时,我使 head = Insert(frota* head, char name)。代码如下

typedef struct robot {
   int bateria;
   char nome[3];
   int pos_x;
   int pos_y;
   int target_x;
   int target_y;
   int limpos;
   int percorridos;
   struct robot * next;
}frota;

frota* Insert(frota* head, char name){
frota* temp = (frota*)malloc(sizeof(frota));
if(temp == NULL){
        printf("Unable to allocate memory for new node\n");
        exit(-1);
}
temp->nome[0] = 'R';
temp->nome[1] = name;
int* prevtemp = head;
int* nexttemp = head;
if(head != NULL)
    {
        // Corner Case: First on the list
        if(temp->nome[1] <= prevtemp->nome[1])
        {
            head = temp;
            temp->next = prevtemp;
        }
        else
        {
            // CASE: Somewhere between the first and the list
            while(nexttemp->next != NULL)
            {
                nexttemp = nexttemp->next;
                if(temp->nome[1] >= prevtemp->nome[1] && temp->nome[1] <= nexttemp->nome[1])
                {
                    prevtemp->next = temp;
                    temp->next = nexttemp;
                    break;
                }
                prevtemp = prevtemp->next;
            }

            // Corner Case: end of list
            if(nexttemp->next == NULL)
            {
                nexttemp->next = temp;
            }
        }
    }
    else 
    {
        // Corner Case: We had an empty list
        head = temp;
    }
}

我的问题是如何使代码正常工作并直接更改值头的值,从而使该函数成为一个过程。我尝试过的代码是这样的:

void Insert(frota* head, char name){
frota* temp = (frota*)malloc(sizeof(frota));
if(temp == NULL){
        printf("Unable to allocate memory for new node\n");
        exit(-1);
}
temp->nome[0] = 'R';
temp->nome[1] = name;
int* prevtemp = *head;
int* nexttemp = *head;
if(*head != NULL)
    {
        // Corner Case: First on the list
        if(temp->nome[1] <= prevtemp->nome[1])
        {
            *head = temp;
            temp->next = prevtemp;
        }
        else
        {
            // CASE: Somewhere between the first and the list
            while(nexttemp->next != NULL)
            {
                nexttemp = nexttemp->next;
                if(temp->nome[1] >= prevtemp->nome[1] && temp->nome[1] <= nexttemp->nome[1])
                {
                    prevtemp->next = temp;
                    temp->next = nexttemp;
                    break;
                }
                prevtemp = prevtemp->next;
            }

            // Corner Case: end of list
            if(nexttemp->next == NULL)
            {
                nexttemp->next = temp;
            }
        }
    }
    else 
    {
        // Corner Case: We had an empty list
        *head = temp;
        (*head)->next = NULL;
    }
}

这样我将过程称为 Insert(&head,name)。任何帮助将不胜感激。当我运行代码时,它出现段错误,而不是内存错误 PS:该代码不完全由我负责,在网上找到了一些部分......

感恩

最佳答案

对于初学者:

改变

int* prevtemp;

成为

struct robot * prevtemp;

(与 nexttemp 相同)

<小时/>

全部更改

head

(*head)
<小时/>

全部更改

frota

struct robot

关于c - 如何按字母顺序在列表中插入结构体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288103/

相关文章:

c++ - 在C/C++中将变量名定义为__00000001有什么好处

python-2.7 - 在 Python 2.7 中使用 `scandir`

c++ - 从 0 到 8 之间随机选择一个数字,一个数字一个数字,直到没有数字可以选择,然后重新开始

c# - 在泛型中使用什么类型进行装箱

c - 实现 NTP 服务器端

c - 在 C 中不使用对象进行管理 - 而且,为什么我可以在 C 中的函数中的任何位置声明变量?

c# - 基于其他列合并 C# 列表中一列的多行

function - 结构嵌入、函数输入、多态性

json - 如何使用 goreq 接收复杂的 json?

c - 为什么结构数组允许我索引到边界数组之外