Lecture 5/18: Linked Lists 1


May 18, 2020

đź“‚Associated files

Introduction to Linked Lists

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

A long line of people


Slide 2

Announcements


Slide 3

Today's Topics


Slide 4

Using pointers with class and struct objects


Slide 5

Can you architect a queue?

An image that says 'WE CAN DO BETTER'


Slide 6

And Now for Something Completely Different


Slide 7

Linked Lists


Slide 8

Adding a node in the middle of a linked list


Slide 9

Removing a node in the middle of a linked list


Slide 10

Why linked lists?


Slide 11

Linked lists in C++


Slide 12

Always!

Always draw pictures when you are building linked lists! This is critical to getting the hang of it.


Slide 13

Lord of the Linked Lists


Slide 14

Return of the King

An image of Aragorn from Lord of the Rings, Return of the King


Slide 15

Lord of the Linked Lists


Slide 16

How is the Stack implemented with a linked list?

The qt logo


Slide 17

Stack


Slide 18

Stack pop()


Slide 19

Stack Code

#pragma once

class IntStack {
public:
    IntStack(); // constructor
    ~IntStack();

    bool isEmpty();
    
    void push(int value);
    int top();
    int pop();

private:
    struct Node {
      int data;
      Node* next;
    };
    
    Node* head;
};
#include "intStack.h"

IntStack::IntStack()
{
    head = nullptr;
}

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

void IntStack::push(int value)
{
    Node* node = new Node;

    node->data = value;
    node->next = head;

    head = node;
}

int IntStack::pop()
{
    if (isEmpty()) {
        throw "Error! Trying to pop from empty stack!";
    }
   
    int toReturn = head->data;
 
    Node* temp = head;
    head = head->next;

    delete temp;
    return toReturn;
}

Slide 20

A Better Linked List Queue

#pragma once

class IntQueue {
public:
    IntQueue(); // constructor
    ~IntQueue();

    bool isEmpty();

    void enqueue(int value);
    int front();
    int dequeue();

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

    Node* _front;
    Node* _back;
};
#include "intQueue.h"

IntQueue::IntQueue()
{
    _front = nullptr;
    _back = nullptr;
}

IntQueue::~IntQueue()
{
    while (_front != nullptr) {
        Node *temp = _front;
        _front = _front->next;
        delete temp;
    }
}

void IntQueue::enqueue(int value)
{
    Node* node = new Node;

    node->data = value;
    node->next = nullptr;

    if (_back == nullptr) { // enqueue on empty queue
        _front = node;
        _back = node;
    } else {
        _back->next = node;
        _back = node;
    }
}

int IntQueue::dequeue()
{
    if (isEmpty()) {
        throw "Error! Trying to dequeue from empty queue!";
    }

    int toReturn = _front->data;

    Node* temp = _front;
    _front = _front->next;

    if (_front == nullptr) {
        _back = nullptr; // empty queue
    }

    delete temp;
    return toReturn;
}

bool IntQueue::isEmpty()
{
    return _front == nullptr;
}

int IntQueue::front()
{
    return _front->data;
}