答疑解惑必采纳 2021-08-18 13:38 采纳率: 91.3%
浏览 59
已结题

C语言,补全一下这个代码,其他代码不要改动

从最高点开始,向右从大到小的顺序排列,如下图
img
struct node {
int height;
struct node *next;
};

// ADD ANY FUNCTION DECLARATIONS YOU WISH TO USE HERE

struct node *mountaineer(struct node *head) {
// TODO: COMPLETE THIS FUNCTION

return head;

}
void print_and_free_list(struct node *head) {
if (head == NULL) {
printf("\n");
return;
}
printf("%d, ", head->height);
print_and_free_list(head->next);
free(head);
}

struct node *make_list(int length, char *argv[]) {
struct node *head = malloc(sizeof (struct node));
struct node *n = head;
int i = 0;
while (i < length) {
n->height = strtol(argv[i + 1], NULL, 10);
if (i < length - 1) {
// there are more nodes to make
n->next = malloc(sizeof (struct node));
n = n->next;
} else {
n->next = NULL;
}
i++;
}
return head;
}

int main(int argc, char *argv[]) {
struct node *head = make_list(argc - 1, argv);
struct node *return_path = mountaineer(head);
printf("Given stopping points: ");
print_and_free_list(head);

printf("Return stopping points: ");
print_and_free_list(return_path);
return 0;

}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-08-18 13:42
    关注

    十万个为什么么......
    国外老师这么狠啊
    先要找到最高点,然后向前遍历比他小的,再把下一个作为标杆,向前找比它小的

    struct node *mountaineer(struct node *head) {
           if(head == NULL)
            return NULL;
        struct node *r = NULL;
        struct node *p = head;
        struct node *q = head->next;
        unsigned long sH = p->height;
        unsigned long mH = 0xFFFFFFFF;
        while(q != NULL)
        {
            if(q->height < sH)
            {
                  p->next = q->next;
                  free(q);
                  q = p->next;
            }
            else if(q->height > sH && q->height < mH)
            {
                  mH = q->height;
                  r->next = q;
                  free(p);
                  p = r;
            }
            else
            {
                p->next = q->next;
            }
            r = p;
            p = q;
            q = q->next;
        }
        //
        p = head;
        q = p->next;
        p->next = NULL;
        while(q != NULL)
        {
            head = q;
            head->next = p;
            p = head;
            q = q->next;
        }
        return head;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月26日
  • 已采纳回答 8月18日
  • 创建了问题 8月18日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)