Cards (76)

  • What type of container is a list in STL?
    A sequential container
  • What memory allocation does a list allow?
    Non-contiguous memory allocation
  • What data structure does a list implement?
    Doubly linked list
  • What is each element in a list called?
    Node
  • What is the structure of a node in a doubly linked list?
    • Previous Pointer
    • Data
    • Next Pointer
  • What must be included in the header section to use a list?
    #include <list>
  • How do you declare a list in C++?
    list<dataType> listName;
  • How do you create a list with initial values in C++?
    • list<int> num = {1, 2, 3, 4, 5};
    • list<string> names;
  • What does the .front() function do in a list?
    Returns the first element of a list
  • What does the .back() function do in a list?
    Returns the last element of a list
  • What do iterators .begin() and .end() do in a list?
    Access and traverse the list content
  • What does .begin() point to in a list?
    First element of the list
  • What does .end() point to in a list?
    Pointer after the last element
  • What will cout << num.front(); display if num = {1, 2, 3, 4, 5}?
    1
  • What will cout << num.back(); display if num = {1, 2, 3, 4, 5}?
    5
  • How can you display all elements in a list using a range-based for loop?
    for(int n : num)
  • Why can't a STL list be accessed using index [] or .at() function?
    It does not support random access
  • What is the purpose of the abs() function in the context of a list?
    To get the absolute value of a number
  • What does num.front() = -1; do to the list num = {1, 2, 3, 4, 5}?
    Changes first element to -1
  • What does num.back() = -5; do to the list num = {1, 2, 3, 4, 5}?
    Changes last element to -5
  • How do you add an element to the front of a list?
    Using .push_front()
  • What does num.push_front(1); do?
    Adds 1 to the front of the list
  • How do you add an element to the end of a list?
    Using .push_back()
  • What does num.push_back(5); do?
    Adds 5 to the end of the list
  • What is the purpose of the .insert() function?
    Adds multiple elements at different positions
  • What is the syntax for the .insert() function?
    listName.insert(position, value);
  • What does num.insert(num.begin(), 0); do?
    Adds 0 to the front of the list
  • What does num.insert(num.end(), 6); do?
    Adds 6 to the end of the list
  • What does num.insert(pos, 3); do if pos points to element 4?
    Adds 3 before element 4
  • What does num.insert(pos, {3, 4}); do if pos points to element 6?
    Adds 3 and 4 before element 6
  • What does the .pop_front() function do?
    Removes the first element of a list
  • What does num.pop_front(); do if num = {1, 2, 3, 4, 5}?
    Removes the first element
  • What does the .pop_back() function do?
    Removes the last element of a list
  • What does num.pop_back(); do if num = {1, 2, 3, 4, 5}?
    Removes the last element
  • What is the purpose of the .erase() function?
    Removes a single element or range of elements
  • What is the syntax for the .erase() function?
    listName.erase(position);
  • What does num.erase(num.begin()); do?
    Removes the first element from the list
  • What does num.erase(pos); do if pos points to the last element?
    Removes the last element from the list
  • What does num.erase(num.begin(), num.end()); do?
    Removes all elements from the list
  • What are the key functions for modifying a list in STL?
    • .push_front() - Adds to front
    • .push_back() - Adds to end
    • .insert() - Adds at specific position
    • .pop_front() - Removes from front
    • .pop_back() - Removes from end
    • .erase() - Removes specific elements