1. Exam Points
Traverse one list or two listsManipulate list elementsusing variousprocedures, including INSERT, APPEND, REMOVE, LENGTH, etc.
2. Knowledge Points
(1) Lists
- A
listis anordered sequence of elements. - A value in the list is called an
element. - An
indexis a common method for referencing the elements in a list or string using natural numbers (1,2,3,…). - The
starting indexof a list is1. (!!!)
(2) Manipulate Lists
-
A. Create a list
- Remember to use ”” to enclose
string values. - Assign one list to another means
copyingthe 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:
- The
variable itemis the current element traversed. - Example 1: Compute the sum of all the elements in a list
- Example 2: Find the maximum element
- Syntax:
-
Linear searchor sequential search algorithmscheck each elementof 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:Elementsin a list can bemanipulated 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 needto store elements withdifferent variables, whichreduces complexity.