linear structures
play

Linear Structures Every non-empty linear structure has A unique - PDF document

4/18/2013 Linear Structure Linear Structures Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Chapter 4 Every element except first


  1. 4/18/2013 Linear Structure Linear Structures  Every non-empty linear structure has  A unique element called first  A unique element called last  Every element except last has a unique successor Chapter 4  Every element except first has a unique predecessor CPTR 318 1 2 Implementations Array details  Array  For an array A of size n , where n > 0 :  Contiguous memory  First element: A [0]  Space needed only for data  Last element: A [ n – 1]  Insertions and deletions may require data movement  Predecessor of A [ i ] , for all i > 0 : A [ i – 1]  Linked list  Successor of A [ i ] , for all i < n – 1 : A [ i + 1]  Non-contiguous memory  Data and links both require space n  Insertions and deletions require no data movement A i − 1 n − 1 0 i i + 1 List details Operations template <typename T> template <typename T> struct Node class List { { Operation Array Linked List T data; Node<T> *head, *tail; Node *prev, *next; // Methods omitted … Searching O( n ) O( n ) }; }; Ordered Search O(log n ) O( n )  For a list L , where L .head is not null, and p is a Node<T> pointer to an element in the list: Insertion O( n ) O(1)  First element: L .head Deletion O( n ) O(1)  Last element: L .tail Sorting O( n log n ) O( n log n )  Predecessor of p , for all p ≠ L .head : p ->prev Random Access O(1) O( n )  Successor of p , for all p ≠ L .tail : p ->next 1

  2. 4/18/2013 Operations Operations Operation Array Linked List Operation Array Linked List Searching O( n ) O( n ) Searching O( n ) O( n ) Ordered Search O(log n ) O( n ) Ordered Search O(log n ) O( n ) Insertion O( n ) O(1) Insertion O( n ) O(1) Deletion O( n ) O(1) Deletion O( n ) O(1) Sorting O( n log n ) O( n log n ) Sorting O( n log n ) O( n log n ) Random Access O(1) O( n ) Random Access O(1) O( n ) Operations Operations Operation Array Linked List Operation Array Linked List Searching O( n ) O( n ) Searching O( n ) O( n ) Ordered Search O(log n ) O( n ) Ordered Search O(log n ) O( n ) Insertion O( n ) O(1) Insertion O( n ) O(1) Deletion O( n ) O(1) Deletion O( n ) O(1) Sorting O( n log n ) O( n log n ) Sorting O( n log n ) O( n log n ) Random Access O(1) O( n ) Random Access O(1) O( n ) Operations Operations Operation Array Linked List Operation Array Linked List Searching O( n ) O( n ) Searching O( n ) O( n ) Ordered Search O(log n ) O( n ) Ordered Search O(log n ) O( n ) Insertion O( n ) O(1) Insertion O( n ) O(1) Deletion O( n ) O(1) Deletion O( n ) O(1) Sorting Sorting O( n log n ) O( n log n ) O( n log n ) O( n log n ) Random Access O(1) Random Access O(1) O( n ) O( n ) 2

  3. 4/18/2013 Specialized Linear Structures Stack  Stack  LIFO  LIFO  Operations  Operations: push, pop, top  push  Queue  pop  FIFO  top  Operations: enqueue, dequeue, first  Applications  Deque  Depth-first search  Access at both ends  Recursive evaluation  Operations: insert_front, insert_back, delete_front, delete_back, front, back Queue Circular Queue  FIFO  Operations Page 131  enqueue  dequeue  front  Applications  Breadth-first search  Scheduling  Simulations Encapsulation — Good! Encapsulation — Bad!  Data structure encapsulation is also known as  The client is insulated from accessing data hiding the individual elements of the data structure  Client need not worry about the internal details  The client is at the mercy of the  Client is concerned with what , not how access operations provided by the  The author of the encapsulating class is free to encapsulating class change the internals of the encapsulated class  What if I want to print only the items in a without breaking client code list of integers that are even?  Wonderful for software maintenance issues: fixes, optimizations, etc. 17 18 3

  4. 4/18/2013 Encapsulation Dilemma Iterator  How can we provide access to individual  An iterator is an object that provides access elements of an encapsulated data set without to individual elements of a collection in a exposing the implementation of the data standard way without exposing the structure? implementation details of that collection  This problem has been encountered many  The collection can be real or virtual times in the past  Traversing a list vs. generating a random number  Fortunately a standard solution has been devised 19 20 Design Patterns Structure of a Design Pattern  Iterator is one example of a design pattern  Name  A design pattern in software is a design that solves  Context for its use a programming problem  The design has application beyond the  Problem description particular problem at hand  Solution  Concept originally proposed by Christopher Alexander for architectural design  GoF book  The iterator concept is one example of a design pattern 22 23 Iterator Design Pattern The Standard Template Library.  Name: Iterator  The STL provides a collection of data  Context for its use: An encapsulated collection of data structures and provides some generic elements algorithms, such as sorting.  Problem description: Need to provide client-controlled access to individual elements of the collection without  As its name suggests, the STL makes heavy exposing the underlying data structure of the collection use of templates. holding those elements  Solution: Provide a separate object that  All compiler that implement the standard has  has data structure-specific knowledge of the collection, but that the library available.  presents to the client generic operations that access elements in an implementation-independent manner 24 4

  5. 4/18/2013 Basic Data Structures STL basic components  Data Structure is a representation of data and  Containers the operations allowed on that data.  Iterators  Basic Data Structures  Algorithms  List  Stack  Queue  Sets  Maps  Priority Queues STL Containers STL Iterators  Iterators maintain a notion of a current position in the container and  A collection of objects (elements) provides basic operations such as the ability to advance to the next position and access the item in the current position  Must support: Must support:   bool empty()  itr++  iterator begin()  itr-- (optional)  *itr  iterator end()  ==  int size()  != Each container could define several iterators   const_iterator must be used to traverse const containers C++11 Smart Pointers Singly-linked List // Implements individual nodes within a singly-linked list // Implements singly-linked list objects. template <typename T> template <typename T> struct ListNode class LinkedList { { // The value of interest in a list element // Points to the first element of this list; null if the list is empty T data; shared_ptr<ListNode<T>> head; // A link to the next element in the list; null, if this node is the last element shared_ptr<ListNode<T>> next; // Points to the last element of this list; null if the list is empty shared_ptr<ListNode<T>> tail; // Constructor assumes type T is copyable public: ListNode<T>(const T& elem): data(elem), next(nullptr) {} // Creates a new, empty list object // Constructor assumes type T is copyable LinkedList<T>(): head(nullptr), tail(nullptr) {} ListNode<T>(const T& elem, shared_ptr<ListNode<T>> n): data(elem), next(n) {} // Inserts new element elem onto the back of this list object }; void insert(const T& elem ) { … } }; 5

  6. 4/18/2013 Reference Counting Insert 12 Insert 4 Insert 22 Insert 7 List Object Goes Out of Scope 6

  7. 4/18/2013 List Object Goes Out of Scope List Object Goes Out of Scope List Object Goes Out of Scope List Object Goes Out of Scope List Object Goes Out of Scope List Object Goes Out of Scope 7

  8. 4/18/2013 List Object Goes Out of Scope List Object Goes Out of Scope List Object Goes Out of Scope Doubly-linked Lists 2 2 2 2 List Object Goes Out of Scope Doubly-linked Lists 2 2 2 2 1 2 2 1 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend