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;
}
相关推荐
啊?啊?3 分钟前
C/C++练手小项目之倒计时与下载进度条模拟
c语言·开发语言·c++
IT成长日记8 分钟前
【LVS入门宝典】LVS调度算法轮询(RR)深度解析:从原理到实战的公平调度之道
算法·lvs·rr·轮询调度算法
strongwyy16 分钟前
esp32墨水屏学习3
学习
_dindong18 分钟前
Linux系统编程:线程概念
linux·运维·笔记·学习
西阳未落26 分钟前
C++基础(22)——模板的进阶
开发语言·c++
waves浪游26 分钟前
C++模板进阶
开发语言·c++
NAGNIP27 分钟前
一文搞懂量化、剪枝和知识蒸馏都是什么?
算法
点云SLAM41 分钟前
GTSAM 中自定义因子(Custom Factor)的详解和实战示例
算法·机器人·slam·后端优化·gtsam·gtsam自定义因子·因子图
青草地溪水旁1 小时前
设计模式(C++)详解——迭代器模式(1)
c++·设计模式·迭代器模式
青草地溪水旁1 小时前
设计模式(C++)详解——迭代器模式(2)
java·c++·设计模式·迭代器模式