CPSC 221

Linked Lists

Today

Announcements

  • PA1 is due Monday Oct 5 — linked lists all the way down
  • Examlet 2 is this week — book your seat on PrairieTest
  • PEX2 is posted, and now covers pointers


Where we’re going

  • A new memory model
  • insertAtFront
  • printReverse, printReverseOdds
  • reverse

A new memory model:

template <class T>
struct Node {
    T data;
    Node * next;
    Node(T nD, Node * nN = nullptr) : data(nD), next(nN) { }
};

What is the result of each statement?

int main() {
    Node<int> nn(5);
    Node<int> * pnn;
}

Write code that would result in each of these memory configurations, in order:

8 6 8

Example 1: insertAtFront

insertAtFront<farmAnimal>(head, cow);
head dog cat pig
void insertAtFront(Node * ________ curr, LIT e) {

}

Running time?

Example 2: printReverse

8 4 2 6 3
void printReverse(Node * curr) {

}


Running time?   (our first recurrence)

Example 3: printReverseOdds

8 4 2 6 3 1
void printReverseOdds(Node * curr) {
    // no items in list


    // one item in list


    // >1 item in list

}

Running time?

Example 4: reverse

8 4 2 6 3
Node * reverse(Node * curr) {
    if (curr != nullptr && _________________) {
        Node * revRest = ______________________



    }
    return ______________;
}

Running time?