答疑解惑必采纳 2021-08-18 11:27 采纳率: 91.3%
浏览 23
已结题

c语言,补全一下我的这个代码,在put your code here添加代码,其他的不用改

Compare给出了两个参数head1和head2,它们是指向链表第一个节点的指针。
如果第一个列表比第二个列表短,Compare应该返回-1
如果两个列表长度相同,Compare应该返回0
如果第一个列表比第二个列表长,Compare应该返回1
img
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

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

// compare should return -1 if head1 is a shorter list than head2
// compare should return 1 if head1 is a longer list than head2
// compare should return 0 if head1 is the same length as head2
int compare(struct node *head1, struct node *head2) {

// PUT YOUR CODE HERE (change the next line!)
return 42;

}
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
// create two linked lists from command line arguments
int dash_arg = argc - 1;
while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
dash_arg = dash_arg - 1;
}
struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);

int result = compare(head1, head2);
printf("%d\n", result);

return 0;

}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
struct node *head = NULL;
for (int i = len - 1; i >= 0; i = i - 1) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
}
return head;
}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-08-18 11:37
    关注

    哦,又来问题啦,让我看看先

    int compare(struct node *head1, struct node *head2) {
        while(head1 != NULL && head2 != NULL)
        {
              head1 = head1->next;
              head2 = head2->next;
        }
        if(head1 == NULL && head2 == NULL)
            return 0;
        if(head1 == NULL && head2 != NULL)
            return -1;
        return 1;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行