list - CS313E: Elements of Software Design Another ADT:...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS313E: Elements of Software Design Another ADT: Linked Lists Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: November 23, 2011 at 06:34 CS313E Slideset 7: 1 Linked Lists The List ADT A list is a finite sequence of elements: A 1 , A 2 , ... A n Is this structure homogeneous or heterogeneous ? What operations are naturally performed on a list? How does (or should) this affect the implementation? Lists are an abstract data type , with a pretty rich interface. CS313E Slideset 7: 2 Linked Lists Lists in Python The analogue of an array in Python is a list . A list is heterogenous and dynamic . What does that mean? The size is not specified at creation and can grow and shrink as needed. Python provides built-in functions to manipulate a list and its contents. There are several ways in which to create a list: enumerate all the elements create an empty list and then append or insert items into the list. CS313E Slideset 7: 3 Linked Lists Lists in Python (2) To obtain the length of a list you can use the len() function. a = [1, 2, 3] length = len (a) # length = 3 The items in a list are indexed starting at 0 and ending at index length - 1 . You can also slice a list by specifying the starting index and the ending index and Python will return to you a sub-list from the starting index and upto but not including the end index. a = [1, 9, 2, 8, 3, 7, 4, 6, 5] b = a[2:5] # b = [2, 8, 3] CS313E Slideset 7: 4 Linked Lists List Methods Use L [int expr] to access the element at a given position. Use L [start : end] to slice for a sublist. Here are some other useful methods: L + L list concatenation L.append(x) add element to the end of L L.extend(lst) add elements of lst to the end of L L.insert(i, x) insert x at i if in L, else at end L.pop() remove and return the last element L.pop(i) remove and return the element at i CS313E Slideset 7: 5 Linked Lists List Methods x in L boolean membership test L.index(x) index of x in L, or error if doesn’t appear L.count(x) count occurrences of x in L L.remove(x) remove first occurrence of x from L L.reverse() reverse L in place L.sort() sort L in place CS313E Slideset 7: 6 Linked Lists List Operations Let’s think of lists more abstractly than just as a Python data type. What is the abstract interface: List() create a new List get(index) fetch the item at index add (item) add an item to the list remove (item) remove an item from the list search (item) see if the item is in the list isEmpty () is the list empty? len () how many items are in the list A tradeoff is necessary between being minimalist and being useful. In many languages, lists are homogenous, but not in Python. Why is that? CS313E Slideset 7: 7 Linked Lists Which Operations Matter?...
View Full Document

This note was uploaded on 12/01/2011 for the course CS 329E taught by Professor Mitra during the Spring '11 term at University of Texas at Austin.

Page1 / 40

list - CS313E: Elements of Software Design Another ADT:...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online