CP164 Study Guide - Final Guide: Array Data Structure, Nth Metal, Data Element

100 views2 pages
14 Jun 2018
School
Course
Professor
The Array-Based List Implementation
The general notes about an array-based implementations for Stacks and Queues also apply
to a List.
The front of the List, insomuch as it has one (which applies for the purposes of searching) is
the left-most inhabited element in the List. The size of the List at any given time is simply
the number of array elements that contain values, or right-most element index + 1. No
array elements can be left empty. A List is empty if the array element with index 0 is empty.
Implementing the Array-Based List
The following code shows how to implement part of the array-based List in Python. The
rest is left to you as an exercise for labs and assignments.
Initializing an Array-Based List
See the initializations for a Stack or Queue.
Getting the Length of an Array-Based List
... is identical to the Queue __len__ method.
Implementing the in Operator
The in operator tells us whether a value is contained in a List or not, and is used as:
if key_movie in movie_list:
The special Python name of the method for this operator is __contains__:
def __contains__(self, key):
"""
---------------------------------------------------------
Determines if the list contains key.
Use: b = key in l
-------------------------------------------------------
Preconditions:
key - a partial data element (?)
Postconditions:
returns
True if the list contains key, False otherwise. (boolean)
-------------------------------------------------------
"""
# search self._values for key
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The general notes about an array-based implementations for stacks and queues also apply to a list. The front of the list, insomuch as it has one (which applies for the purposes of searching) is the left-most inhabited element in the list. The size of the list at any given time is simply the number of array elements that contain values, or right-most element index + 1. A list is empty if the array element with index 0 is empty. The following code shows how to implement part of the array-based list in python. The rest is left to you as an exercise for labs and assignments. See the initializations for a stack or queue. Is identical to the queue __len__ method. The in operator tells us whether a value is contained in a list or not, and is used as: if key_movie in movie_list: The special python name of the method for this operator is __contains__: def __contains__(self, key):

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents

Related Questions