linear structures
play

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

2/4/2011 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 3 Every element except first


  1. 2/4/2011 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 3  Every element except first has a unique predecessor CPTR 318 1 2 CPTR 314 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 0 i − 1 i i + 1 n − 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. 2/4/2011 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 ) Operations Operations Operation Array Linked List Operation Array Linked List Searching Searching O( n ) O( n ) O( n ) O( n ) Ordered Search Ordered Search O(log n ) O( n ) O(log n ) O( n ) Insertion Insertion O( n ) O(1) 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 ) 2

  3. 2/4/2011 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 Encapsulation — Good!  FIFO  Data structure encapsulation is also known as data hiding  Operations  Client need not worry about the internal details  enqueue  Client is concerned with what , not how  dequeue  The author of the encapsulating class is free to  front change the internals of the encapsulated class without breaking client code  Applications  Wonderful for software maintenance  Breadth-first search issues: fixes, optimizations, etc.  Scheduling  Simulations 16 Encapsulation — Bad! Encapsulation Dilemma  The client is insulated from accessing  How can we provide access to individual the individual elements of the data elements of an encapsulated data set without structure exposing the implementation of the data  The client is at the mercy of the access operations provided by the structure? encapsulating class  This problem has been encountered many  What if I want to print only the items in a times in the past list of integers that are even?  Fortunately a standard solution has been devised 17 18 3

  4. 2/4/2011 Design Patterns Iterator  Iterator is one example of a design pattern  An iterator is an object that provides access  A design pattern in software is a design that solves to individual elements of a collection in a a programming problem standard way without exposing the  The design has application beyond the implementation details of that collection particular problem at hand  Concept originally proposed by  The collection can be real or virtual Christopher Alexander for architectural  Traversing a list vs. generating a random number design  GoF book  The iterator concept is one example of a design pattern 19 21 Structure of a Design Pattern Iterator Design Pattern  Name: Iterator  Name  Context for its use: An encapsulated collection of data  Context for its use elements  Problem description  Problem description: Need to provide client-controlled access to individual elements of the collection without  Solution exposing the underlying data structure of the collection holding those elements  Solution: Provide a separate object that  has data structure-specific knowledge of the collection, but that  presents to the client generic operations that access elements in an implementation-independent manner 22 23 The Standard Template Library. Basic Data Structures  The STL provides a collection of data  Data Structure is a representation of data and structures and provides some generic the operations allowed on that data. algorithms, such as sorting.  Basic Data Structures  As its name suggests, the STL makes heavy  List use of templates.  Stack  All compiler that implement the standard has  Queue the library available.  Sets  Maps  Priority Queues 4

  5. 2/4/2011 STL basic components STL Containers  Containers  A collection of objects (elements)  Iterators  Must support:  bool empty()  Algorithms  iterator begin()  iterator end()  int size() STL Iterators Iterators maintain a notion of a current position in the container and  provides basic operations such as the ability to advance to the next position and access the item in the current position Must support:   itr++  itr-- (optional)  *itr  ==  != Each container could define several iterators   const_iterator must be used to traverse const containers 5

Recommend


More recommend