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:

解答:

cpp 复制代码
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:

解答:

cpp 复制代码
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;
}
相关推荐
hui-梦苑4 分钟前
[Math]复合函数极限存在定理:单调连续函数反函数连续性定理推论
学习·数学·定理·函数极限·海涅定理
欧特克_Glodon6 分钟前
OpenCV计算机视觉开发入门与实践<三十五>:开发视频播放器
c++·opencv·计算机视觉·音视频
在线OJ的阿川16 分钟前
基于智慧一考通Web平台的测试报告
功能测试·学习·测试报告
YHHLAI22 分钟前
Danci —— 用 AI 驱动开发一个全栈英语单词学习平台
人工智能·学习
LuminousCPP41 分钟前
数据结构 - 排序(二):快速排序从错误初版到优化版|双指针划分 + 三数取中 + 小区间插入优化
c语言·数据结构·笔记·算法·排序算法
smartpi_ai42 分钟前
自学习“放着不动“会自动退出吗?超时退出缺席的交互兜底、退出条件挂串口的状态联动设计
学习·microsoft·交互
苦猿的大模型日记44 分钟前
Day58|从0学习 Claude Code(八):对话越聊越长,我给它装了个自动碎纸机
学习
山甫aa1 小时前
【从零开始的 Web 后端学习】令牌技术一篇搞定(JWT 登录认证保姆级)
后端·学习·spring·web·jwt
hansang_IR1 小时前
【题解】P9753 [CSP-S 2023] 消消乐
数据结构·c++·算法
竞赛考级题库1 小时前
202606 青少年等级考试C/C++真题一级 建议答题时长:60min
java·c语言·c++