《计算机科学导论》第十一章课后作业解答(个人版)

复习题

  1. 数据结构的三种类型名称:
    a. 数组
    b. 链表
    c. 树

  2. 数组元素和记录元素的区别是什么?
    数组元素是指在数组中的每个单个数据项,它们的类型通常相同,可以通过数组下标访问。而记录元素是指在记录中的每个字段,每个字段可以具有不同的数据类型,记录通常用于组织多个相关的字段。

  3. 数组元素和链表元素的区别是什么?
    数组元素是在内存中连续存储的数据项,可以通过索引访问。而链表元素是通过指针相互连接的节点,它们在内存中可以是分散的,每个节点包含数据和指向下一个节点的指针。

  4. 为什么用索引而不是下标来标注数组元素?
    索引和下标本质上是同义词,用来表示数组中元素的位置。索引一词更常用于通用的数据结构,而下标一词在编程语言中更常用,特别是在数组访问时,例如array[0]表示数组的第一个元素。

  5. 数组元素在内存中如何存储?
    数组元素在内存中是连续存储的,每个元素占据固定大小的内存空间,元素之间没有间隔。根据元素类型和数据结构,计算机根据数组的起始地址和偏移量来访问数组元素。

  6. 记录中域的定义是什么?
    记录中的域是指记录中的每个字段或属性,每个域包含一个数据项,可以有不同的数据类型。记录是一种复合数据类型,它由多个域组成,用于表示一个实体的多个特征。

  7. 在链表中节点的域是什么?
    在链表中,节点的域是指节点中存储的数据或信息。每个节点通常包含两个域,一个是数据域,用于存储节点的数据;另一个是指针域,用于指向链表中的下一个节点。

  8. 链表中指针的功能是什么?
    链表中的指针用于连接节点,每个节点都包含一个指针,它指向链表中的下一个节点。这样可以通过节点之间的指针关系遍历整个链表。

  9. 如何指向链表中的第一个节点?
    链表的第一个节点可以通过链表的头指针访问。头指针是指向链表中第一个节点的指针,通过头指针可以访问整个链表。

  10. 链表中最后一个节点的指针指向什么?
    链表中最后一个节点的指针通常指向空值(NULL或None),表示链表的结束。这样可以在遍历链表时通过检查节点的指针是否为空来确定是否到达了链表的末尾。

练习题

  1. 测试数组 A 中每个元素是否与数组 B 中对应元素相等的算法:
for i from 0 to 9:
    if A[i] equals B[i]:
        print "Element at index", i, "is equal in both arrays."
    else:
        print "Element at index", i, "is not equal in both arrays."
  1. 倒序排列数组中元素的算法:
initialize start = 0
initialize end = length of array - 1

while start < end:
    swap elements at index start and end
    increment start by 1
    decrement end by 1
  1. 打印二维数组内容的算法:
for i from 0 to R-1:
    for j from 0 to C-1:
        print the element at row i and column j
  1. 在具有 N 个元素的数组上应用顺序查找的算法:
for i from 0 to N-1:
    if array[i] equals target:
        return i (index of target)
        
return -1 (target not found)
  1. 在具有 N 个元素的数组上应用折半查找的算法(假设数组已排序):
initialize start = 0
initialize end = N-1

while start <= end:
    initialize mid as the middle index between start and end
    
    if array[mid] equals target:
        return mid (index of target)
    else if array[mid] < target:
        set start to mid + 1
    else:
        set end to mid - 1

return -1 (target not found)
  1. 在有序的数组中插入一个元素的算法:
index = binary_search(array, new_element)  // Using binary search to find insertion position
if index == -1:
    // Element not found, handle the case appropriately
else:
    shift all elements from index to the right by 1
    insert new_element at index
  1. 在有序的数组中删除一个元素的算法:
index = binary_search(array, element_to_delete)  // Using binary search to find the element
if index == -1:
    // Element not found, handle the case appropriately
else:
    shift all elements from index+1 to the left by 1
  1. 给数组中的每个元素乘以一个常数的算法:
for i from 0 to N-1:
    array[i] = array[i] * constant
  1. 将一分数 Fr1 加到另一分数 Fr2 上的算法:
result_numerator = Fr1.numerator * Fr2.denominator + Fr2.numerator * Fr1.denominator
result_denominator = Fr1.denominator * Fr2.denominator
result = Fraction(result_numerator, result_denominator)
  1. 从一分数 Fr2 中减去另一分数 Fr1 的算法:
result_numerator = Fr2.numerator * Fr1.denominator - Fr1.numerator * Fr2.denominator
result_denominator = Fr1.denominator * Fr2.denominator
result = Fraction(result_numerator, result_denominator)
  1. 一分数 Fr1 乘以另一分数 Fr2 的算法:
result_numerator = Fr1.numerator * Fr2.numerator
result_denominator = Fr1.denominator * Fr2.denominator
result = Fraction(result_numerator, result_denominator)
  1. 用分数 Fr2 除分数 Fr1 的算法:
result_numerator = Fr1.numerator * Fr2.denominator
result_denominator = Fr1.denominator * Fr2.numerator
result = Fraction(result_numerator, result_denominator)
  1. 示意图,显示数据部分是学生记录的链表,记录中有标识、姓名和成绩。
                  +----------+       +----------+       +----------+
head ----------> | Student1 | ----> | Student2 | ----> | Student3 | ----> NULL
                  +----------+       +----------+       +----------+
  1. 链表的删除算法(算法11.4)是如何删除链表中唯一的一个节点的。
if head is NULL:
    // The list is empty
    print "The list is empty."
else:
    delete head
    head = NULL
  1. 链表的插入算法(算法11.3)是如何在空链表中插入一个节点的。
if head is NULL:
    create a new node and set it as head
    set the link of the new node to NULL
else:
    print "The list is not empty. Use the normal insertion algorithm."
  1. 我们如何使用插入算法(算法11.3)从零开始建立一链表。
initialize head as NULL

for i from 1 to N:
    create a new node with data i
    set the link of the new node to head
    set head to the new node
  1. 写一个算法,求出数字链表中数字的平均数。
initialize sum = 0
initialize count = 0

current = head
while current is not NULL:
    add the value of current to sum
    increment count by 1
    move current to the next node

average = sum / count
  1. 如果我们对图11-9的链表应用下列语句,将会发生什么?
scores ←(* scores ).link

这个语句将 scores 指向链表中的下一个节点。

  1. 如果我们对图11-13的链表应用下列语句,将会发生什么?
cur ←(* cur ).link
pre ←(* pre ).link

这个语句将 cur 和 pre 分别指向链表中的下一个节点。

posted @ 2023-07-27 12:45  mu_yu21  阅读(553)  评论(1)    收藏  举报