Python LinkedLists.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Computer Science 112 Fundamentals of Programming II List Iterators.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Computer Science 112 Fundamentals of Programming II Lists.
Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Tirgul OOP No.3 Iterators. What is an Iterator? An object that provides a way to access elements of an aggregate object sequentially, without exposing.
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
Lecture 05 – Classes.  A class provides the definition for the type of an object  Classes can store information in variables  Classes can provide methods.
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Walk through previous lectures. Tuple tuple_name = (value, value,..., value) A way of packing multiple values into a variable >>> x = 3 >>> y = -5 >>>
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Dr. Philip Cannata 1 Midterm Review. Dr. Philip Cannata 2.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Python iterators and generators. Iterators and generators  Python makes good use of iterators  And has a special kind of generator function that is.
Computer Science 112 Fundamentals of Programming II Binary Search Trees.
Iterators and Generators Thomas Wouters XS4ALL
COMPSCI 105 SS 2015 Principles of Computer Science Linked Lists 1.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
COMPSCI 105 SS 2015 Principles of Computer Science
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Lab-12 Keerthi Nelaturu. BitList Stores bit values in Linked List Has to be stored in right to left order Example :
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Python: Linked Lists and Recursion Damian Gordon.
Computer Science 112 Fundamentals of Programming II Iterators.
Linked Lists and Generics Written by J.J. Shepherd.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Advanced Programming 7/10/20161 Ananda Gunawardena.
Iterators and Generators
Fundamentals of Programming II Linked Lists
Intro2CS Tirgul 11.
Example: Vehicles What attributes do objects of Sedan have?
CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists
Fundamentals of Programming II Binary Search Trees
Fundamentals of Programming II Interfaces and Implementations
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Advanced Linked lists Doubly Linked and Circular Lists
Algorithm for deleting a node from a singly linked list
Computer Science 112 Fundamentals of Programming II
Revised based on textbook author’s notes.
Data Structures ADT List
Lecture 20 Linked Lists Richard Gesick.
Linear Data Structures Chapter 3
Python iterators and generators
Computer Science 210 Computer Organization
ADT list.
Node Applications in Abstract Data Types
Lists in Python Outputting lists.
Data Structures ADT List
Linked Lists.
Revised based on textbook author’s notes.
Python iterators and generators
Presentation transcript:

Python LinkedLists

LinkedList Code from class on Thursday September 19, 2013 element Node size LinkedList next 1 head 1 tail 1 n1 ll1 element = “a” next = None size = 0 head = n2 tail = n1 element = “b” next = n1 n2 class LinkedList() : head = None tail = None size = 0 def cons(self, val) : n = Node(val) self.size+=1 if self.head == None : self.head = n self.tail = n else : n.next = self.head return self def car(self) : return self.head.element def cdr(self) : l = self if self.size > 0 : l.head = l.head.next l.size -= 1 return l def __str__(self): result = "'(" current = self.head for i in range(self.size+1): if current != None: result += str(current.element) if current.next != None : result += ", " # Separate two elements with a comma current = current.next else: result += ")" # Insert the closing ] in the string return result # Return an iterator for a linked list def __iter__(self): # To be discussed in Section 18.3 return LinkedListIterator(self.head) # The Node class class Node: def __init__(self, element): self.element = element self.next = None class LinkedListIterator: # To be discussed in Section 18.3 def __init__(self, head): self.current = head def next(self): if self.current == None: raise StopIteration element = self.current.element self.current = self.current.next return element ll1 = LinkedList() n1 = Node("a") ll1.head = n1 ll1.tail = n1 ll1.size+=1 print "ll1 is: ", ll1 n2 = Node("b") ll1.head = n2 n2.next = n1 Gives: ll1 is: '(a) ll1 is: '(b, a) See the class calendar for a larger font size version of this code.