当前位置: 首页 > 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;
}

 

http://www.lryc.cn/news/541109.html

相关文章:

  • 基于STM32的智能家居语音系统(单片机毕设)
  • ASP.NET Core 简单文件上传
  • 2502C++,C++继承的多态性
  • 【机器学习】13.十大算法之一K均值算法(K-means)聚类详细讲解
  • Spring扩展点之Mybatis整合模拟
  • .NET MVC实现电影票管理
  • 自媒体账号管理工具:创作罐头使用指南
  • 基于数据可视化+SpringBoot+安卓端的数字化OA公司管理平台设计和实现
  • VSCode离线安装插件
  • 基于Hadoop的汽车大数据分析系统设计与实现【爬虫、数据预处理、MapReduce、echarts、Flask】
  • SHELL32!Shell_MergeMenus函数分析
  • 华为云deepseek大模型平台:deepseek满血版
  • AutoGen 技术博客系列 八:深入剖析 Swarm—— 智能体协作的新范式
  • 从零开始开发纯血鸿蒙应用之网页浏览
  • 【大模型LLM】DeepSeek LLM Scaling Open-Source Language Models with Longtermism
  • 分布式事务-本地消息表学习与落地方案
  • Debezium系列之:记录一次源头数据库刷数据,造成数据丢失的原因
  • PHP约课健身管理系统小程序源码
  • Java之泛型
  • 图论 之 最小生成树
  • STM32-有关内存堆栈、map文件
  • Linux系统中常见的词GNU是什么意思?
  • 【个人开源】——从零开始在高通手机上部署sd(二)
  • 【MCU驱动开发概述】
  • PC端Linux之虚拟CAN
  • C++:std::thread、条件变量与信号量
  • POI pptx转图片
  • Java File 类
  • 工业通信协议 EtherNet/IP 全面解析
  • 使用docker配置PostgreSQL