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

相关文章:

  • 【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学习周报
  • 公司做网络推广怎么做/seo的工具有哪些
  • 免费空间做网站/seo实战密码第三版
  • 哪些网站可以做微商/如何做网站设计
  • 企业管理控制系统/百度seo查询系统
  • 制作表白网站教程/seo推广方法集合
  • python怎么做抢课网站/网络营销计划的七个步骤