软件设计与数据结构

Chapter 9 Lists and Generics

| 关于   «  5. 编程练习 8   ::   目录   ::   2. 泛型进阶  »

1. 线性表

1.1. 概述与学习目标

完成本模块后,学生将能够:

  • 区分线性表与其他 ADT(栈、队列、包)的特性和用途

  • 使用基于数组或基于链式结构的方法在 java 中实现线性表

  • 考虑各种设计方法及其相应的效率

  • 追踪并调试线性表的实现

1.1.1. 建议阅读:

第 12-14 章:线性表;使用数组实现的线性表;链接数据的线性表实现 ,选自 Data Structures and Abstractions with Java, 4th edition by Frank M. Carrano and Timothy Henry

1.2. 线性表简介

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

ListIntro.pdf

线性表接口

下载视频中的 java 文件(见下面)并在 Eclipse 中自行运行和探索。你可以下载本例的独立 *.java 文件。要运行独立的 *.java 文件,你需要
  1. 创建一个新的 Eclipse 项目,然后

  2. 在项目内创建一个名为"example"的包(类顶部的包名必须与文件在 Eclipse 项目中放置位置的包名一致),最后

  3. 下载并将独立的 *.java 文件导入到所创建的包中。

ListInterface.java (right click-> save link as...)
package list;

/**
* An interface for the ADT list. Entries in a list have positions that begin
* with 0
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @author maellis1
* @version July 2024
*/
public interface ListInterface<T> {
   /**
   * Adds a new entry to the end of this list. Entries currently in the list
   * are unaffected. The list's size is increased by 1.
   *
   * @param newEntry
   *            The object to be added as a new entry.
   */
   public void add(T newEntry);

   /**
   * Adds a new entry at a specified position within this list. Entries
   * originally at and above the specified position are at the next higher
   * position within the list. The list's size is increased by 1.
   *
   * @param newPosition
   *            An integer that specifies the desired position of the new
   *            entry.
   * @param newEntry
   *            The object to be added as a new entry.
   * @throws IndexOutOfBoundsException
   *             if either newPosition less than 0 or newPosition greater than
   *             the size of the list.
   */
   public void add(int newPosition, T newEntry);

   /**
   * Removes the entry at a given position from this list. Entries originally
   * at positions higher than the given position are at the next lower
   * position within the list, and the list's size is decreased by 1.
   *
   * @param givenPosition
   *            An integer that indicates the position of the entry to be
   *            removed.
   * @return A reference to the removed entry.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T remove(int givenPosition);

   /** Removes all entries from this list. */
   public void clear();

   /**
   * Replaces the entry at a given position in this list.
   *
   * @param givenPosition
   *            An integer that indicates the position of the entry to be
   *            replaced.
   * @param newEntry
   *            The object that will replace the entry at the position
   *            givenPosition.
   * @return The original entry that was replaced.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T replace(int givenPosition, T newEntry);

   /**
   * Retrieves the entry at a given position in this list.
   *
   * @param givenPosition
   *            An integer that indicates the position of the desired entry.
   * @return A reference to the indicated entry.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T getEntry(int givenPositi son);

   /**
   * Retrieves all entries that are in this list in the order in which they
   * occur in the list.
   *
   * @return A newly allocated array of all the entries in the list. If the
   *         list is empty, the returned array is empty.
   */
   public Object[] toArray();

   /**
   * Sees whether this list contains a given entry.
   *
   * @param anEntry
   *            The object that is the desired entry.
   * @return True if the list contains anEntry, or false if not.
   */
   public boolean contains(T anEntry);

   /**
   * Gets the length of this list.
   *
   * @return The integer number of entries currently in the list.
   */
   public int getLength();

   /**
   * Sees whether this list is empty.
   *
   * @return True if the list is empty, or false if not.
   */
   public boolean isEmpty();
} // end ListInterface

1.3. 检查点 1

1.4. 交互式:LinkedList 的 Add() 实现

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

LinkedListAdd.pdf

1.5. 检查点 2

1.6. 交互式:使用调试器追踪 Add()

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

TraceAddDebugger.pdf

1.7. 交互式:LinkedList 的 Remove()

跟着做、练习与探索

下载视频中的对应项目并在 Eclipse 中自行运行和探索。上面的示例项目需要 CS2-Support 项目。它也会用在你平时的课程项目中。要下载 CS2-Support,你必须先完成第一次实验的配置步骤。然后就可以通过 Eclipse 使用蓝色向下箭头图标,或使用项目菜单并选择"Download Assignment..."来下载它。

exLinkedList.zip
LinkedListRemove.pdf

1.8. 检查点 3

1.9. 编程实践:线性表 1

1.10. 交互式:LinkedList 的细节与选项

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

LinkedListMoreDetails.pdf

1.11. 检查点 4

1.12. 交互式:线性表的数组实现

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

ArrayListImplementation.pdf

1.13. 检查点 5

1.14. 交互式:线性表实现的效率

跟着做并参与

下载视频对应的幻灯片。观看视频时在幻灯片上做笔记,自己练习画图!

ListEfficiency.pdf

1.15. 检查点 6

1.16. 编程实践:线性表 2

   «  5. 编程练习 8   ::   目录   ::   2. 泛型进阶  »

关闭窗口