答疑解惑必采纳 2021-08-18 12:14 采纳率: 91.3%
浏览 54
已结题

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

fix_counting函数接受一个参数,head指向一个链表。
链表将包含连续递增的整数。换句话说,链表将从一个数字开始并向上计数。
除了可能少了一个整数。也可能没有丢失的整数。
如果缺少整数,fix_counting应该修改列表来添加它。
然后,新的列表应该包含连续的整数,不遗漏任何整数。换句话说,它应该向上计数而不遗漏一个数字。
Fix_counting应该返回一个指向新列表的指针。
img
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

// fix_counting is given a pointer to a linked list containing consecutive integers
// except there may be one missing integer
// fix_counting should return a pointer to a list
// with the missing integer added (if there was a missing integer)
struct node *fix_counting(struct node *head) {

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

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

int main(int argc, char *argv[]) {

// create linked list from command line arguments
struct node *head = NULL;
if (argc > 1) {
    // list has elements
    head = strings_to_list(argc - 1, &argv[1]);
}

struct node *new_head = fix_counting(head);
print_list(new_head);

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;
int i = len - 1;
while (i >= 0) {
struct node *n = malloc(sizeof (struct node));
assert(n != NULL);
n->next = head;
n->data = atoi(strings[i]);
head = n;
i -= 1;
}
return head;
}

// DO NOT CHANGE THIS FUNCTION
// print linked list
void print_list(struct node *head) {
printf("[");
struct node *n = head;
while (n != NULL) {
// If you're getting an error here,
// you have returned an invalid list
printf("%d", n->data);
if (n->next != NULL) {
printf(", ");
}
n = n->next;
}
printf("]\n");
}

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2021-08-18 13:16
    关注
    struct node *fix_counting(struct node *head) {
      if(head == NULL)
            return NULL;
     struct node *p = head;
     struct node * q = p->next;
     while(q != NULL)
    {
          if(q->data != p->data +1)
          {
                struct node *r = (struct node *)malloc(sizeof(struct node));
                r->data = p->data + 1;
                r->next = q;
                p->next = r;
          }
          p = q;
          q = q->next;
    } 
    return head;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度