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;
}
相关推荐
Brookty13 分钟前
【Java学习】Lambda表达式
java·学习
Clockwiseee14 分钟前
SSTI记录
运维·服务器·redis·学习
Huazzi.15 分钟前
使用SSH协议克隆详细步骤
linux·运维·学习·ssh·编程
C_Liu_16 分钟前
C语言:深入理解指针(3)
c语言·数据结构·算法
huangyuchi.19 分钟前
【C++】智能指针
开发语言·jvm·c++·笔记·c++11·智能指针·shared_ptr
南玖yy36 分钟前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计
s_little_monster1 小时前
【Linux】socket网络编程之TCP
linux·运维·网络·笔记·学习·tcp/ip·学习方法
李匠20241 小时前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang
柴薪之王、睥睨众生1 小时前
(自用)Java学习-5.12(Redis,B2C电商)
java·开发语言·学习
xindafu1 小时前
代码随想录算法训练营第三十八天|动态规划part6(完全背包2)
算法·动态规划