C语言 在我的链表中循环时出现问题

anauzrmj  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(130)
typedef struct node{
   unsigned int base_address;       
   unsigned int limit_offset;       
   struct node *next;               
   struct node *previous;           
}node;

这是我用来做节点的代码这是循环

void sort_by_id(node *listHead){
    while(listHead->next != 0){
        node current = *listHead;
        node search = current;
        node lowest = current;
        while(search.next != 0){
            if(search.base_address<lowest.base_address){
                lowest = search;
            }
            search = search->next;
        }
        node temp = lowest;
        search = current;
        if(current.base_address == listHead->base_address){
            lowest.next = search.next;
            lowest.previous = search.previous;
            search.next = temp.next;
            search.previous = temp.previous;
            *listHead = lowest;
        }else{
            lowest.next = search.next;
            lowest.previous = search.previous;
            search.next = temp.next;
            search.previous = temp.previous;
        }
        current= current->next;
    }
}

目前在这种状态下,它给我一个错误,说在行search = search->next;current= current->next; '-〉'是一个无效的参数类型。我是新的c和任何帮助将不胜感激。
我试着将这两行代码分别修改为search = search.next*;current= current.next*;,但是我得到一个错误,告诉我“expected expression before”;所以我不知道该怎么办。

mf98qq94

mf98qq941#

sort_by_id()似乎是选择排序,所以只是重新实现了它(你的代码没有编译,因为你把指针和对象混在一起了)。这里唯一有趣的事情是swap()只处理数据元素,而不处理指针。你可以处理指针而不处理数据元素,但这需要更多的工作(在您正在交换的两个节点中有4个指针,在这两个节点中的每个节点之前/之后的4个节点中有1个指针,总共有8个指针)。
我还写了一个build()print()和一些测试数据来演示它的工作。

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

typedef struct node {
    unsigned base_address;
    unsigned limit_offset;
    struct node *next;
    struct node *previous;
}node;

void swap(node *a, node *b) {
    if(a == b) return;
    unsigned base_address = a->base_address;
    unsigned limit_offset = a->limit_offset;
    a->base_address = b->base_address;
    a->limit_offset = b->limit_offset;
    b->base_address = base_address;
    b->limit_offset = limit_offset;
}

// selelection sort
void sort_by_id(node *listHead){
    for(; listHead && listHead->next; listHead = listHead->next) {
        node *min = listHead;
        for(node *listHead2 = listHead->next; listHead2; listHead2 = listHead2->next)
            if(listHead2->base_address < min->base_address)
                min = listHead2;
        swap(min, listHead);
    }
}

struct node *build(unsigned n, unsigned addrs[n]) {
    node *nodes = malloc(sizeof(node) * n);
    if(!nodes)
        return NULL;
    for(unsigned i = 0; i < n; i++) {
        nodes[i].base_address = addrs[i];
        // TODO: nodes[i].limit_offset = ...
        nodes[i].next = i + 1 < n ? &nodes[i+1] : NULL;
        nodes[i].previous = i ? &nodes[i-1] : NULL;
    }
    return nodes;
}

void print(node *head) {
    for(; head; head = head->next)
        printf("%d%s", head->base_address, head->next ? " " : "\n");
}

int main() {
    node *head = build(5, (unsigned []) {1, 0, 2, 4, 3});
    print(head);
    sort_by_id(head);
    print(head);
}

示例会话:

相关问题