当前位置: 首页 > news >正文

cs106x-lecture14(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture14

(以下皆使用SPL实现,非STL库,后续课程结束会使用STL实现)

1、min

Write a function named min that accepts a pointer to a ListNode representing the front of a linked list. Your function should return the minimum value in the linked list of integers. If the list is empty, you should throw a string exception.

Constraints: Do not construct any new ListNode objects in solving this problem (though you may create as many ListNode* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify the linked list's state; the state of the list should remain constant with respect to your function.

Assume that you are using the ListNode structure as defined below:

解答:

int min(ListNode* front) {
    int m;
    if (front == nullptr) {
        throw std::string("");
    } else {
        m = front->data;
        ListNode* current = front;
        while (current->next != nullptr) {
            current = current->next;
            if (m > current->data) {
                m = current->data;
            }
        }
    }
    return m;
}

 

2、countDuplicates

Write a function named countDuplicates that accepts a pointer to a ListNode representing the front of a linked list. Your function should return the number of duplicates in a sorted list. Your code should assume that the list's elements will be in sorted order, so that all duplicates will be grouped together. For example, if a variable named front points to the front of the following sequence of values, the call of countDuplicates(front) should return 7 because there are 2 duplicates of 1, 1 duplicate of 3, 1 duplicate of 15, 2 duplicates of 23 and 1 duplicate of 40:

{1, 1, 1, 3, 3, 6, 9, 15, 15, 23, 23, 23, 40, 40}

Constraints: Do not construct any new ListNode objects in solving this problem (though you may create as many ListNode* pointer variables as you like). Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify the linked list's state; the state of the list should remain constant with respect to your function. You should declare the function to indicate this to the caller.

Assume that you are using the ListNode structure as defined below:

解答:

int countDuplicates(ListNode* front) {
    int count = 0;
    if (front == nullptr) {
        return count;
    } else {
        ListNode* current = front;
        while (current->next != nullptr) {
            if (current->data == current->next->data) {
                count++;
            }
            current = current->next;
        }
    }
    return count;
}

 

相关文章:

  • 【Java进阶学习 第五篇】JDK8、9中的接口新特性
  • ARM Cortex-M3 技术解析:核寄存器R1-R15介绍及使用
  • 第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
  • kafka+spring cloud stream 发送接收消息
  • 华为OD机试真题-相对开音节-OD统一考试(E卷)
  • Meterpreter之getsystem命令提权原理详解
  • Zotero 快速参考文献导出(特定期刊引用)
  • 区块链相关方法-波士顿矩阵 (BCG Matrix)
  • Codes 开源免费研发项目管理平台 2025年第一个大版本3.0.0 版本发布及创新的轻IPD实现
  • 在LangFlow中集成OpenAI Compatible API类型的大语言模型
  • 不同类型的网站选择不同的服务器,那么应该怎么选择服务器呢?
  • STM32-心知天气项目
  • python包重要修改
  • 如何把windows机器作为SSH客户端免密登录
  • Markdown使用方法文字版解读
  • 数据表的存储过程和函数介绍
  • OpenBMC:BmcWeb app.run
  • tortoiseGit的使用和上传拉取
  • 使用docker开发镜像编译
  • pytest下allure
  • 幼儿园教师拍打孩子额头,新疆库尔勒教育局:涉事教师已被辞退
  • 上交所五方面落实募资新规:强化关键少数责任和股东权利保障
  • 普京确定俄乌谈判俄方代表团名单
  • “一百零一个愿望——汉字艺术展”亮相意大利威尼斯
  • 腾讯一季度营收增长13%,马化腾:战略性的AI投入将带来长期回报
  • 在古老的意大利科莫歌剧院,廖昌永唱响16首中国艺术歌曲