Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
2/22/17 BASIC LINKED DATA STRUCTURES Dr. John Georgas, Northern Arizona University Copyright © John Georgas All Rights Reserved Outline 2 ¨ Current ¤ Data structures, in general ¤ Arrays ¤ Linked list ¤ Stack ¤ Queue Data structures 3 ¨ ¨ Data structures define a way of storing and organizing data elements Data structures set rules for: ¤ What kind of data is stored? is data stored? ¤ How can data be accessed? ¤ How can data be inserted? ¤ How can data be removed? ¤ How ¨ Each data structure captures a set of design decisions about these (and other) questions 1 2/22/17 Python lists and arrays 4 ¨ ¨ ¨ Fundamental structure: Array ¤ Stores data that is the same size ¤ Organized contiguously ¤ Accessible in random order ¤ Fixed sized structure, unless resized Python lists are built on top of dynamically sized arrays, as an array of references ¤ References to objects are stored in the array ¤ Objects are stored externally on the heap Complexity implications ¤ Reference of an index location? ¤ Value change? ¤ Addition of a list element? ¤ Removal of a list element? Linked List 5 ¨ Linked list consists of: ¤ Nodes: each containing data and a reference to another node ¤ Structure around nodes: allowing access into sequence of nodes ¨ It’s all about trade-offs: ¤ Insertion and removal is cheap not support random access well (comparatively expensive) ¤ Does Variations, bells and whistles 6 ¨ ¨ ¨ ¨ Singly-linked list ¤ Structure: Nodes have one reference to the next node ¤ Function: Can only traverse one way Doubly-linked list ¤ Structure: Nodes have two references, one to the next node and one to the previous node ¤ Function: Can traverse either way First and last node references Counter ¤ A counter of nodes n ¨ Amortization: Costs of algorithms is distributed among many operations Circular-linked list ¤ Structure: Last node points back to the first ¤ Function: Complete iteration from any start position 2 2/22/17 Operations of interest, in Python 7 ¨ ¨ Linked lists, the OO way ¤ LinkedList n __init_ n add_first, remove_first n __len__ n __str__ ¤ Node n __init__ n __str__ ¤ Iterators n __init_ n next and StopIteration n __iter__ from LinkedList Using a linked list ¤ Creating a new linked list ¤ Adding some elements ¤ Remove some elements ¤ Traverse the linked list with an iterator Stacks 8 ¨ Elements of a stack can only be added to and removed from the top ¤ Bottom ¨ can not be directly modified Operations: ¤ pop – remove element from the top of the stack ¤ push – add element to the top of the stack ¨ Imposes a last-in, first-out ordering (LIFO) ¨ Built on top of a list, with a limited set of operations ¤ Or, first-in, last-out Queues 9 ¨ Elements of a queue: ¤ ¤ ¨ Can only be added to the tail Can only be removed from the head Operations: ¤ ¤ enqueue – add an element to the tail of the queue dequeue – remove an element from the head of the queue ¨ Imposes a last-in, last-out ordering (LILO) ¨ Built on top of a list, with a limited set of operations Variations: ¤ ¨ ¤ Or, first-in, first-out Priority queue allows nodes to be marked with a priority indicator, that overrides traditional ordering 3