Lecture 5/20: Linked Lists 2


May 20, 2020

đź“‚Associated files

More on Linked Lists

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

An image of a people with their wrists linked together (linked wrists)


Slide 2

Announcements


Slide 3

Today's Topics


Slide 4

Stack


Slide 5

Stack pop()


Slide 6

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 7

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;
}

Slide 8

A LinkedIntList class

// Represents a linked list of integers.
#pragma once
#include<ostream>

using namespace std;

struct ListNode {
    int data;
    ListNode* next;
    ListNode(int value) { // constructor
        data = value;
        next = nullptr;
};

class LinkedIntList { 
public:
    LinkedIntList(); 
    ~LinkedIntList();
    void addBack(int value); 
    void addFront(int value); 
    void clear();
    int get(int index);
    void insert(int index, int value); 
    bool isEmpty();
    void remove(int index);
    void set(int index, int value); 
    int size();
private:
    ListNode* _front; // null if empty
};

Slide 9

Adding to the front of a linked list


Slide 10

Adding to the back of a linked list


Slide 11

Removing at an index