Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 112 Fundamentals of Programming II Lists.

Similar presentations


Presentation on theme: "Computer Science 112 Fundamentals of Programming II Lists."— Presentation transcript:

1 Computer Science 112 Fundamentals of Programming II Lists

2 Lists: Formal Properties A list is a linear collection that supports access, replacement, insertion, and removal at any position Lists are more general-purpose than stacks and queues No standard set of operations, but most lists support many typical ones

3 Categories of List Operations Supporting, such as len, isEmpty, iter, +, == Index-based (use an index position) Content-based (use an object) Position-based (move a cursor)

4 L1 == L2 compares items at consecutive positions L1 + L2 items from L2 follow the items from L1 iter(L) items are served up from the first position through the last one Supporting Operations

5 L.insert(i, item) opens up a slot in the list at index i and inserts item in this slot L[i] returns the item at index i L.pop(i) removes and returns the item at index i (or item at last position if i is omitted) L[i] = item replaces the item at index i with the item Preconditions on [] and pop : 0 <= i < length of the list Index-Based Operations

6 L.append(item) adds item at the list ’ s tail (method add has same effect) item in L returns true if the list contains an object equal to item L.index(item) returns the index of the first instance of item in the list L.remove(item) removes the first instance of item from the list Preconditions on index and remove : item must be in the list Content-Based Operations

7 LI = L.listIterator() returns a new list iterator object on a list LI.first() moves the cursor to the first item LI.hasNext() returns true if there are any items following the current position LI.next() returns the next item and advances the position LI.last() moves the cursor to the last item LI.hasPrevious() returns true if there are any items preceding the current position LI.previous() returns the previous item and moves the position backward Position-Based Operations for Navigation Similar to an extended iterator

8 LI.insert(newItem) inserts newItem at the current position LI.remove() removes the last item returned by next or previous LI.replace(newItem) replaces the last item returned by next or previous Position-Based Operations for Mutation

9 List Applications object heap storage management documents files implementations of other collections

10 List Implementations –list standard Python (array-based) –ArrayList array-based –LinkedList two-way nodes –ListIterator (allows insertions, removals, movement to previous items)

11 Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list for i in range(len(lyst)): print(lyst[i]) Index-based traversal of a LinkedList using [] looks like it would be linear, but it’s actually quadratic! Almost all LinkedList operations are linear, including []

12 Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list for item in lyst: print(item) The simple iterator has the same performance in all list implementations

13 Performance Tradeoffs lyst = LinkedList() # Add some objects to lyst # Traverse the list rator = lyst.listIterator() while rator.hasNext(): print(rator.next()) Or use a list iterator for traversals of a list, in either direction

14 One-Way Linked Structures A one-way linked structure supports movement in one direction only; cannot move backward Access at the head is O(1), but access anywhere else is O(N) Insertion or removal at the head is a special case 5432 head

15 Tweak with a Tail Pointer Access to either end is O(1), but generally is still O(N) Still cannot move backward; insertion or removal at the head or tail is still a special case Removal at the tail is still linear 5432 head tail

16 Two-Way Linked Structures D1D2 A circular, doubly linked structure with a dummy header node permits movement in both directions allows constant-time access to the head or tail eliminates special cases in code when access is at the beginning or the end of the structure D3

17 An Empty Structure When there are no data, there is a single dummy header node Its two links point ahead and back to itself Its data field is None

18 class TwoWayNode(object): def __init__(self, data, previous = None, next = None): self.data = data self.previous = previous self.next = next The Node Class Strictly a utility class No need for accessor or mutator methods

19 Declaring an External Pointer head = TwoWayNode(None) head.previous = head.next = head head class TwoWayNode(object): def __init__(self, data, previous = None, next = None): self.data = data self.previous = previous self.next = next

20 Appending a Node temp = TwoWayNode("A", head.previous, head) head head.previous always points to the last node The last node ’ s next pointer always points back to head “A”“A” temp

21 Appending a Node temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head “A”“A” temp head.previous always points to the last node The last node ’ s next pointer always points back to head

22 Appending a Node temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head.previous = temp head “A”“A” temp head.previous always points to the last node The last node ’ s next pointer always points back to head

23 Analysis temp = TwoWayNode("A", head.previous, head) head.previous.next = temp head.previous = temp head No loop is needed to locate the last node Append is a constant time operation! No if statements needed to check for special cases “A”“A” temp

24 For Monday List Iterators


Download ppt "Computer Science 112 Fundamentals of Programming II Lists."

Similar presentations


Ads by Google