O GoConqr está de visual novo! Conte para nós o que você achou.

Flashcards

Lists

Virginie Gagné Computer Science Inglês 42 flashcards

Lists

Flashcards do conjunto (42)

  1. 1define a listlist implies that there is a positional ordering
  2. 2operations performed on listsget(i) // Returns the i-th element (but doesn’t remove it)
    set(i,e) // Replaces the i-th element with e
    add(i,e) // Inserts element e into the i-th position
    add(e) // Inserts element e (e.g. at the end of the list)
    remove(i) // Removes the i-th element from list
    remove(e) // Removes element e from the list (if it is there)
    clear() // Empties the list.
    isEmpty() // Returns true if empty, false if not empty.
    size() // Returns number of elements in the list
  3. 3what's the extra property of array liststhe elements are squeezed into the lowest indices possible so that there are no holes
  4. 4what's the difference between length and size?size is the number of elements in the array, length (also called capacity) is the number of spots.
    capacity is always >= size
  5. 5get(i) alg arr lget(i)
    if (i >= 0) & (i < size)
    return a[i]
  6. 6set(i) alg arr lset(i,e)
    if (i >= 0) & (i < size)
    a[i] = e
  7. 7add(i, e) alg arr ladd(i, e) alg arr l
  8. 8remove(i) alg arr lremove(i) alg arr l
  9. 9what is fast and what is slow with array lists?Adding to or removing from near the back end of the list is fast since few shift operations are necessary.
    From the front is slow.
  10. 10define the Java ArrayList classimplements the various methods such as we discussed and uses an array as its underlying data structure
  11. 11how do you construct an ArrayList object? what's to watch out for?ArrayList shapes = new ArrayList();
    the generic type needs to be a reference type; it cannot be a primitive type.
    (e.g. use the wrapper class Integer)
  12. 12How do you access elements in the array list?NOT using a[i] cuz we don't know the underlying array.
    USING get or set or other methods!
  13. 13Time complexity for array lists?add and remove from the front is O(n) slow
    get and set in constant time
  14. 14what does an ArrayList object look like?what does an ArrayList object look like?
  15. 15define a linked list define a list nodeeach element in the list is referenced by a list node
    A linked list node is an object that contains:
    - a reference to an element of a list
    - a reference to the next node in the linked list.
    class SNode{
    T element;
    SNode next;
    }
  16. 16define head and taillinked list has variables head and tail which reference the first and last node, respectively
    If there is only one node in the list, then head and tail point to the same node.
  17. 17basic skeleton of a SLinkedList classbasic skeleton of a SLinkedList class
  18. 18suppose we have a linked list with size = 4. How many objects do we have in total? how many for an array list?We have the four SNode objects. We have the four elements (objects of type Shape, say) that are referenced by these nodes. We also have the SLinkedList object which has the head and tail references. So this is 9 objects in total.
    (for an arraylist, 6)
  19. 19important difference between array lists and linked listslinked list nodes can be anywhere in memory (no notion of running out of room)(but no constant time access to elements), whereas the underlying array in array list is a block of consecutive locations in memory
  20. 20s l l addFirst(e) alg and time complexityconstant amount of times l l addFirst(e) alg and time complexity
  21. 21s l l removeFirst() algs l l removeFirst() alg
  22. 22s l l addLast(e) algs l l addLast(e) alg
  23. 23s l l removeLast() alg and time complexityrequires about size steps.s l l removeLast() alg and time complexity
  24. 24computational complexity of arraylist and linked list methodscomputational complexity of arraylist and linked list methods
  25. 25define nodes of doubly linked listsEach node of a doubly linked list has two links rather than one, namely references to the previous node in the list and to the next node in the list. These reference variables are typically called prev and next
  26. 26skeleton for DLinkedList classskeleton for DLinkedList class
  27. 27advantage of DLinked lists over SLinked listsdoubly linked lists allows us to quickly access elements near the back of the list
  28. 28removeLast() alg doubly l lremoveLast() alg doubly l l
  29. 29what to watch out for in d l l and solutionthe edge cases are the first and last elements
    head.prev and tail.next will be null
    To avoid such errors, it is common to define doubly linked lists by using a “dummy” head
    node and a “dummy” tail node, instead of head and tail reference variables.
  30. 30skeleton for DLinkedList class with dummy nodesskeleton for DLinkedList class with dummy nodes
  31. 31how many objects for a d l l of 4 elementshow many objects for a d l l of 4 elements
  32. 32d l l getNode(i) alg for get( i ){ node = getNode(i) return node.element }d l l getNode(i) alg for get( i ){ node = getNode(i) return node.element }
  33. 33faster version of getNode(i) d l lfaster version of getNode(i) d l l
  34. 34remove(i) alg d l lremove(i) alg d l l
  35. 35time complexity remove(i) d l ltakes size/2 operations in the worst case so O(N)
  36. 36define the Java LinkedList classLinkedList class which is implemented as a doubly linked list. Like the ArrayList class, it uses a generic type which is write as T below.
    LinkedList list = new LinkedList();
  37. 37time complexity for methods on the 3 kinds of liststime complexity for methods on the 3 kinds of lists
  38. 38implementation of enhanced for loop for a linked listimplementation of enhanced for loop for a linked list
  39. 39what's the space complexity of different kinds of lists?Array lists use the least amount of total space since they require just one reference for each element of the list.
    Singly linked lists take twice as much space as array lists, since for each element in a singly linked lists, we require a reference to the element and a reference to the next node in the list.
    Doubly linked lists require three times as much space as array lists, because they require a reference to each element and also references to the previous and next node.
    BUT they all require O(N) space.
  40. 40define a shallow copy of a linked listdefine a shallow copy of a linked list
  41. 41define a deep copy of a linked listdefine a deep copy of a linked list
  42. 42how do you make a shallow copy of a linked list?Java LinkedList class has a clone method

Informações

Descrição
Computer Science FlashCards sobre Lists, criado por Virginie Gagné em 18-04-2022.
Criado por
Virginie Gagné
Visualizações
1
Avaliação

Fale Conosco

Sobre o que é sua mensagem?

O que você acha do novo visual?

Você gostou?