Singly linked lists (part 2)

This lesson contains approximately 8 minutes of video content.

Traversing a linked list

Activity: Singly Linked List - In Order Insertions

Graded Playground Autograder

Activity Prompt:

Implement the follwing member functions of the SinglyLinkedList class.

SinglyLinkedList Member Functions

public
SinglyLinkedList() Default constructor
~SinglyLinkedList() Destructor
void Insert(int value) Insert a Node whose data is value into the linked list in ascending order.

Be sure to update head_ and tail_ accordingly!

#include <iostream> #include "singly_linked_list.hpp" int main() { /* SinglyLinkedList sll; std::cout << sll << std::endl; sll.Insert(11); sll.Insert(7); sll.Insert(8); sll.Insert(8); std::cout << sll << std::endl; */ }
#ifndef SINGLY_LINKED_LIST_HPP #define SINGLY_LINKED_LIST_HPP #include <iostream> #include "node.hpp" class SinglyLinkedList { public: SinglyLinkedList(); ~SinglyLinkedList(); void Insert(int value); friend std::ostream& operator<<(std::ostream& os, const SinglyLinkedList& sll); private: Node* head_; Node* tail_; }; #endif
#include "singly_linked_list.hpp" SinglyLinkedList::SinglyLinkedList() { // TODO } SinglyLinkedList::~SinglyLinkedList() { // TODO } void SinglyLinkedList::Insert(int value) { // TODO }
#ifndef NODE_HPP #define NODE_HPP struct Node { Node(int value): data(value) {} int data = 0; Node* next = nullptr; }; #endif
#include "utilities.hpp" #include <iomanip> #include <sstream> std::ostream& operator<<(std::ostream& os, const SinglyLinkedList& ll) { Node* curr = ll.head_; int field_size = 5; os << '.' << std::setw(field_size) << std::setfill('-') << '.' << std::setfill(' ') << std::endl; os << '|' << Center("Head", field_size - 1) << '|' << std::endl; if (curr == nullptr) os << ' ' << std::setw(field_size) << Center("--", field_size - 1) << ' ' << std::setfill(' ') << std::endl; while (curr != nullptr) { os << '.' << std::setw(field_size) << std::setfill('-') << '.' << std::setfill(' ') << std::endl; os << '|' << Center(std::to_string(curr->data), field_size - 1) << '|' << " " << curr << "\x1b[0m" << std::endl; os << '\'' << std::setw(field_size) << std::setfill('-') << '\'' << std::setfill(' ') << std::endl; curr = curr->next; if (curr != nullptr) os << ' ' << std::setw(field_size) << Center("|", field_size - 1) << ' ' << std::setfill(' ') << std::endl; } os << '|' << Center("Tail", field_size - 1) << '|' << std::endl; os << '\'' << std::setw(field_size) << std::setfill('-') << '\'' << std::setfill(' ') << std::endl; return os; } std::string Center(const std::string& str, unsigned int col_width) { // quick and easy (but error-prone) implementation auto padl = (col_width - str.length()) / 2; auto padr = (col_width - str.length()) - padl; std::string strf = std::string(padl, ' ') + str + std::string(padr, ' '); return strf; }
#ifndef UTILITIES_HPP #define UTILITIES_HPP #include "singly_linked_list.hpp" #include <iostream> #include <string> std::ostream& operator<<(std::ostream& os, const SinglyLinkedList& sll); std::string Center(const std::string& str, unsigned int col_width); #endif