The List Data Model
DAT911 - Foundations of Computer Science
UiS
Darío Garigliotti
February 17, 2016
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
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
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
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?
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?
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
Implementation by Linked Lists
• Lookup operation
• Delete operation
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?
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
Implementation by Linked Lists
• What if it is a
doubly linked list?
• E.g. delete
operation
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
Implementation by Arrays
• Lookup operation
• Version with sentinel
(element is "added"
at the end)
Implementation by Arrays
• Assuming sorted, binsearch performs better ;)
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
• Implementation with arrays
Stacks
• Implementation w/ linked lists
• 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
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
Thanks!
Questions?

The List Data Model

  • 1.
    The List DataModel DAT911 - Foundations of Computer Science UiS Darío Garigliotti February 17, 2016
  • 2.
    The List DataModel 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 • emptylist • 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 LinkedLists • 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 LinkedLists • Lookup operation • Delete operation
  • 9.
    Implementation by LinkedLists • Insert operation • all of them are O(n) in both avg and worst cases • how different is it if allowing duplicates?
  • 10.
    Implementation by LinkedLists • 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
  • 11.
    Implementation by LinkedLists • What if it is a doubly linked list? • E.g. delete operation
  • 12.
    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
  • 13.
    Implementation by Arrays •Lookup operation • Version with sentinel (element is "added" at the end)
  • 14.
    Implementation by Arrays •Assuming sorted, binsearch performs better ;)
  • 15.
    Stacks • A restrictedlist, with operations only in one of the ends (the top) • Operations (and conventions about returned values): • push • pop • isEmpty • isFull • clear
  • 16.
    • Implementation witharrays Stacks • Implementation w/ linked lists
  • 17.
    • A restrictedlist, 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
  • 18.
    Some conclusions • Somedata 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
  • 19.