Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The List Data Model

The List Data Model

Date: February 17, 2016
Course: UiS DAT911 - Foundations of Computer Science (fall 2016)

Please cite, link to or credit this presentation when using it or part of it in your work.

#ComputerScience #CS #Algorithms #DataStructures

Darío Garigliotti

February 17, 2016
Tweet

More Decks by Darío Garigliotti

Other Decks in Programming

Transcript

  1. The List Data Model DAT911 - Foundations of Computer Science

    UiS Darío Garigliotti February 17, 2016
  2. The List Data Model Introduction • List: a finite sequence,

    possibly empty, of elements (a1 , a2 , ..., an) • We know linked lists • A data model vs a data structure to implement it
  3. Introduction • Notations with [], () • Character strings: hello!

    • Possible repetitions (i.e. multiple occurrences) of the same element, and order through the sequence • Comparison with other data models • Recursive nature of the model
  4. More terminology • empty list • length of a list

    • head of the list • tail of the list • sublist: given i, j in [1, n], (ai , ai+1 , ..., aj) sublist of (a1 , a2 , ..., an) • subsequence, by eventually removing elements • prefix and suffix of a list
  5. Operations on lists • A basic set of operations like

    we already worked with in previous models: insert, delete, lookup • Particular definitions given the list model • Where to insert? • Which occurrence, if any, to delete?
  6. Operations on lists • Concatenate two lists (a1 , a2

    , ..., an) and (b1 , b2 , ..., bm) is (a1 , a2 , ..., an, b1 , b2 , ..., bm) • Position-related operations: • first and last element (of non-empty lists) • retrieve i-th element • length-related operations: • length of the list • isEmpty?
  7. Implementation by Linked Lists • For each of the implementations

    to explain, we • define a data structure • implement 3 operations: insert, delete, lookup • observe their running times
  8. Implementation by Linked Lists • Insert operation • all of

    them are O(n) in both avg and worst cases • how different is it if allowing duplicates?
  9. Implementation by Linked Lists • Insertion is O(1), but delete

    still O(n) • So look for the convenient balance • How different is it if assuming a sorted list? It's better ;) • E.g. lookup
  10. Implementation by Linked Lists • What if it is a

    doubly linked list? • E.g. delete operation
  11. Implementation by Arrays • A structure with • an array,

    of max length MAX, storing the list in the positions 0, ..., n-1 • the current length of the list (n, n <= MAX) • Comparison vs linked list version
  12. Stacks • A restricted list, with operations only in one

    of the ends (the top) • Operations (and conventions about returned values): • push • pop • isEmpty • isFull • clear
  13. • A restricted list, with operations adding in one end

    (front) and removing from the other (rear) • Operations: • enqueue • dequeue • isEmpty • isFull • clear • E.g implementation with linked lists: Queues
  14. Some conclusions • Some data models are more efficient for

    lookup operations • Lists are good to define functions over sequences by its recursive nature • We can define new abstract data types (stack, queue) by restrictions on the basic data model