Lecture 16: More Linked lists

Mar 13, 2024  β”‚  Last updated Mar 12, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

  • Intro to linked list structures
  • Nodes and node pointers
  • Building lists
  • Traversing lists
  • Lists vs Arrays

Textbook Sections 13.1

// Start the list
Node *head = new Node;
head->data = 1;

// Add another element
head->next = new Node;
head->next->data = 2;
head->next->next = NULL;

Today’s topics

Textbook Chapter 13

Traversing a list

In C++ syntax:

Node *current = head;
while (current) { // or while (current != NULL)
    // do something with current->data
    current = current->next;
}

Passing linked lists to functions

Linked list + function example

Write a function to calculate and return the length of a linked list.

Inputs: Head pointer (by value or by reference?)

Outputs: Length of list (what datatype?)

Caution: passing by reference vs value

Say we defined an insertion function as:

void insert(Node *head, int value); // pass head by value

When testing with the value 5, this will work!

Pass-by-reference is only needed when head changes, but to keep your function general, you should assume that it might change

Finding the proper position - v1

Finding the proper position - v2

Special cases

prevcurrentSolution
NULLNULLInsert as first node in empty list
NULLnot NULLInsert as first node in non-empty list
not NULLNULLInsert as last node in non-empty list
not NULLnot NULLInsert in middle of list

For every linked list operation, think about the special cases!

emoji Linked List check-in 1/2

What am I forgetting in the following code? Assume that a list of Nodes already exists with a pointer to head defined.

  1. head = temp;
  2. delete temp;
  3. temp = NULL;
  4. Both 1 and 2
  5. Both 1 and 3
Node *temp = new Node;
temp->data = 0;
temp->next = head;

emoji Linked List check-in 2/2

What is the following code doing? Again, assume head is defined.

  1. Inserting at the start of the list
  2. Inserting at the end of the list
  3. Inserting in the middle of the list
  4. Finding a node
  5. Deleting a node
Node *temp = new Node;
temp->data = 5;
temp->next = head->next;
head->next = temp;

Deleting a node

Deleting a node - code example

Node *prev = NULL;
Node *current = head;
// Find the node to delete
while (current && current->data != value) {
    prev = current;
    current = current->next;
}

prev->next = current->next; // Disconnect the node
delete current; // Free the memory

What special cases do we need to consider?

Special cases for deletion

prevcurrentSolution
NULLNULLEmpty list, nothing to delete
NULLnot NULLDelete the first node, update head
not NULLNULLEnd of list, nothing to delete
not NULLnot NULLDelete within the list (could be last)

Linked list variations

Doubly linked lists

Circularly linked lists

Lists of lists

Cross-linked lists

Expanding on the previous example, consider the situation where:

struct Student {
    string name;
    int id;
    Course *courses;
};
struct Course {
    string name;
    int number;
    Student *students;
    Instructor *instructor;
};
struct Instructor {
    string name;
    Course *courses;
};

COMP 2631 is all about various information structure

Object oriented programming preview

Classes

Coming up next

Textbook Chapter 10.2-10.3



Previous: Lecture 16: Linked lists
Next: Lecture 18: Intro to Classes