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

cs106x-lecture13(Autumn 2017)-SPL实现

打卡cs106x(Autumn 2017)-lecture13

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

1、v1v2p1p2 

The following code C++ uses pointers and produces two lines of output. What is the output?

int v1 = 10;
int v2 = 25;
int* p1 = &v1;
int* p2 = &v2;
​
*p1 += *p2;
p2 = p1;
*p2 = *p1 + *p2;
​
cout << v1 << " " << v2 << endl;
cout << *p1 << " " << *p2 << endl;
解答:

70 25

70 70

2、parameterMystery1 

The following code produces 4 lines of output. What is the output? Write each line of output as it would appear on the console.

For the purposes of this problem, assume that the variables in main are stored at the following memory addresses:

  • main's a variable is stored at address 0xaa00
  • main's b variable is stored at address 0xbb00
  • main's c variable is stored at address 0xcc00
  • main's d variable is stored at address 0xdd00

解答:

line 19 -3 1 0xcc00
line 2-7 9 6 0xbb00
line 35 -7 2 0xdd00
line 45 9 -3 -7

 

3、12-123

Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown and/or creating new nodes as needed. Assume that the nodes have already been declared and initialized to match the "before" figure below. There may be more than one way to write the code, but do NOT change any existing node's data field value.

If a variable does not appear in the "after" picture, it doesn't matter what value it has after the changes are made. If a given node object does not appear in the "After" picture, you must free its memory to avoid a memory leak.

Before
list: 1 -> 2 /

After
list: 1 -> 2 -> 3 /

Assume that you are using the following ListNode structure:

struct ListNode {
    int data;        // data stored in this node
    ListNode* next;  // a link to the next node in the list
​
    ListNode(int data = 0, ListNode* next = nullptr) { ... }   // constructor
};
解答:
ListNode* node3 = new ListNode(3);
node3->next = nullptr;
list->next->next = node3;

 

4、12-312

Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown and/or creating new nodes as needed. Assume that the nodes have already been declared and initialized to match the "before" figure below. There may be more than one way to write the code, but do NOT change any existing node's data field value.

If a variable does not appear in the "after" picture, it doesn't matter what value it has after the changes are made. If a given node object does not appear in the "After" picture, you must free its memory to avoid a memory leak.

Before
list: 1 -> 2 /

After
list: 3 -> 1 -> 2 /

Assume that you are using the following ListNode structure:

struct ListNode {
    int data;        // data stored in this node
    ListNode* next;  // a link to the next node in the list
​
    ListNode(int data = 0, ListNode* next = nullptr) { ... }   // constructor
};

解答:

//ListNode* list = new ListNode(1);
//list->next = new ListNode(2);
//list->next->next = nullptr;

ListNode* node3 = new ListNode(3);
node3->next = list;
list = node3;

http://www.dtcms.com/a/29601.html

相关文章:

  • 【Linux网络编程】IP协议格式,解包步骤
  • 模拟实现Java中的计时器
  • C++17中的std::scoped_lock:简化多锁管理的利器
  • android 网络防护 手机网络安全怎么防
  • 【算法】----多重背包问题I,II(动态规划)
  • Redis-线程模型
  • VMware下ubuntu-24.04.1系统的下载与安装(保姆级)
  • 【Spring详解四】自定义标签的解析
  • Zabbix——自定义监控项脚本分享
  • Grafana 快速部署监控视图指南
  • leetcode day19 844+977
  • java项目之风顺农场供销一体系统的设计与实现(源码+文档)
  • Html5学习教程,从入门到精通,HTML5 简介语法知识点及案例代码(1)
  • 如何通过 Homebrew 安装 Qt 并配置环境变量
  • 【Linux网络】认识协议(TCP/UDP)、Mac/IP地址和端口号、网络字节序、socket套接字
  • 一文讲解Redis中的基本数据类型
  • PcVue : 点亮马来西亚砂拉越偏远村庄
  • Linux阿里云服务器安装RocketMQ教程
  • Nginx环境安装
  • 2.17-2.23学习周报
  • 深度解析分布式事务:从经典实现到AI增强的创新之路 [特殊字符]
  • 【并发测试】Redis并发性能测试
  • C/C++面试知识点总结
  • 制造行业CRM选哪家?中大型企业CRM选型方案
  • 云服务器和物理服务器该如何选择
  • Java常见问题(二)
  • Jetpack Architecture系列教程之(三)——ViewModel控制器
  • 框架--Mybatis3
  • Git安装
  • 掌握 Zabbix 监控系统配置:从零到精通