Data Structures Exam 1

0.0(0)
studied byStudied by 0 people
0.0(0)
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/32

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:16 AM on 2/4/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

33 Terms

1
New cards

What does a template do?

A template lets you write generic code that works with many data types without rewriting it for each one. They are resolved at compile time.

2
New cards

Example of writing a function template

template <typename T>

T maxValue(T a, T b) {

    return (a > b) ? a : b;

}

3
New cards

What using a function template looks like

int x = maxValue(3, 5);        // T becomes int
double y = maxValue(2.5, 1.1); // T becomes double

4
New cards

Writing a class template

template <typename T>
class Box {
private:
    T value;
public:
    Box(T v) : value(v) {}
    T getValue() { return value; }
};

5
New cards

What using a class template looks like

Box<int> intBox(10);
Box<string> strBox("hello");

Box<int> and Box<string> are different classes. The compiler generates separate versions for each type.

6
New cards

Why do templates matter in data structures?

Most data structures (lists, stacks, trees, vectors) should work for any type, not just int.

7
New cards

What is a constructor?

A constructor is a special function that:

  • Has the same name as the class

  • Has no return type

  • Runs automatically when an object is created

8
New cards

Example of a constructor

class Node {
public:
    int data;
    Node* next;

    Node(int d) {
        data = d;
        next = nullptr;
    }
};

9
New cards

What are constructors used for?

  • Initializing variables

  • Allocating memory

  • Setting default values

  • Putting the object into a valid state

10
New cards

What is a destructor?

A destructor is a special function that:

  • Has the same name as the class

  • Starts with ~

  • Has no parameters

  • Runs automatically when an object is destroyed

11
New cards

Example of a destructor

class Node {
public:
    ~Node() {
        // clean up resources
    }
};

12
New cards

When are destructors called?

  • Object goes out of scope

  • delete is called on a dynamically allocated object

  • Program ends

13
New cards

Why are destructors important in data structures?

Data structures often use dynamic memory. If you don’t delete it, you get memory leaks.

Example:

~LinkedList() {
    while (head != nullptr) {
        Node* temp = head;
        head = head->next;
        delete temp;
    }
}

14
New cards

What do vectors manage?

  • Size → number of elements actually in the vector

  • Capacity → amount of memory allocated

size <= capacity

15
New cards

What does the size() function do?

  • Returns the number of elements in the vector

  • Does not include unused capacity

16
New cards

What does the capacity() function do?

  • Returns how many elements the vector can hold before reallocation

  • Grows automatically (usually doubles)

17
New cards

What does the resize(n) function do?

  • If n > size, it adds elements (default initialized)

  • If n < size, it removes elements

18
New cards

What does the reserve(n) function do?

  • Ensures capacity is at least n

  • Does not change size

  • Used to avoid repeated reallocations

19
New cards

What does the push_back(n) function do?

  • Adds elements to the end

  • Increases size by 1

  • May increase capacity if full

20
New cards

What does the at(index) function do?

  • Accesses element with bounds checking

  • Throws an exception if index is invalid

  • Safer than operator[]

21
New cards

What does the data() function do?

  • Returns pointer to underlying array

  • Allows direct memory access

  • Dangerous if vector resized afterward

22
New cards

Example of a singly-linked node

struct Node {
    int data;
    Node* next;
};

23
New cards

Example of a doubly-linked node

struct Node {
    int data;
    Node* next;
    Node* prev;
};

24
New cards

Insert at Beginning (push_front)

Singly:

newNode->next = head;
head = newNode;

Doubly:

newNode->next = head;
head->prev = newNode;
head = newNode;

O(1)

25
New cards

Insert at Beginning (push_back)

Singly:

  • Traverse to last node

  • Set last→next = newNode

O(n) unless tail pointer exists)

Doubly: Use tail pointer

tail->next = newNode;
newNode->prev = tail;
tail = newNode;

26
New cards

Insert After a Node

Singly:

newNode->next = curr->next;
curr->next = newNode;

Doubly:

newNode->next = curr->next;
newNode->prev = curr;
curr->next->prev = newNode;
curr->next = newNode;

27
New cards

Remove from Beginning (pop_front)

Singly:

Node* temp = head;
head = head->next;
delete temp;

Doubly:

head = head->next;
head->prev = nullptr;

28
New cards

Remove from End (pop_back)

Singly:

  • Traverse to second-to-last node

  • Set its next to nullptr

O(n)

Doubly:

tail = tail->prev;
tail->next = nullptr;

O(1)

29
New cards

Remove a Node (Doubly Linked List)

curr->prev->next = curr->next;
curr->next->prev = curr->prev;
delete curr;

30
New cards

Find (Singly and Doubly)

Node* curr = head;
while (curr != nullptr) {
    if (curr->data == target) return curr;
    curr = curr->next;
}

O(n)

31
New cards

Insert in Sorted Order

  • Traverse until curr->data > newValue

  • Insert before curr

Singly:

  • Must track prev

Doubly:

  • Easier because of prev pointer

O(n)

32
New cards

Forward Traversal (print)

Node* curr = head;
while (curr != nullptr) {
    cout << curr->data << " ";
    curr = curr->next;
}

33
New cards

Backward Traversal (printReverse)

  • Only possible efficiently in doubly-linked lists

Node* curr = tail;
while (curr != nullptr) {
    cout << curr->data << " ";
    curr = curr->prev;
}

Explore top flashcards