1.5

14 阅读1分钟

1. Exam Points

  • Traverse one list or two lists
  • Manipulate list elements using various procedures, including INSERT, APPEND, REMOVE, LENGTH, etc.

2. Knowledge Points

(1) Lists

  • A list is an ordered sequence of elements.
  • A value in the list is called an element.
  • An index is a common method for referencing the elements in a list or string using natural numbers (1,2,3,…).
  • The starting index of a list is 1. (!!!)

(2) Manipulate Lists

  • A. Create a list

    • image.png
    • Remember to use ”” to enclose string values.
    • Assign one list to another means copying the values of one list to the other.
  • B. Access an element by index: listName[index]

    • aList[2] = 10
    • bList[1] = "Annabelle"
    • DISPLAY(cList[3])
  • C. Assign a value of an element of a list to a variable

    • x ← aList [3]
  • D. Assign a value to an element of a list

    • aList [4] ← x
  • E. Insert an element at a given index: INSERT(listName, index, value)

    • INSERT (aList, 3, 100)
  • F. Add an element to the end of a list: APPEND(listName, value)

    • APPEND (aList, 66)
  • **G. Remove elements: REMOVE(listName, index) **

    • REMOVE (aList, 3)
  • **H. Determine the length of a list: LENGTH(listName) **

    • LENGTH (aList)
  • I. Traverse a list

    • Syntax:
      image.png
    • The variable item is the current element traversed.
    • Example 1: Compute the sum of all the elements in a list
      image.png
    • Example 2: Find the maximum element
      image.png
  • Linear search or sequential search algorithms check each element of a list, in order, until the desired value is found or all elements in the list have been checked.

(3) For FRQs

  • Benefits of using lists:
    • Elements in a list can be manipulated in a unified approach, that means you can apply the same operation on the elements by accessing them using indexes.
    • With a list, you do not need to store elements with different variables, which reduces complexity.

3. Exercises