#ifndef SINGLY_LINKED_LIST_H #define SINGLY_LINKED_LIST_H #include #include "queue.h" namespace seq { class SinglyLinkedListNode { public: // return a reference to the stored item int& get_item() { return this->item; } // return pointer to next node, or nullptr if this is the final node SinglyLinkedListNode* get_next() const { return this->next; } // overwrite the item stored in this node void set_item(int in_item) { this->item = in_item; } private: int item = 0; SinglyLinkedListNode* next = nullptr; // attach a node to this node // if there was a node attached to this previously, it is NOT deleted! void set_next(SinglyLinkedListNode* in_next) { this->next = in_next; } friend class SinglyLinkedList; // allow SinglyLinkedList to access private members }; class SinglyLinkedList: public Queue { public: bool empty() const { return (this->head == nullptr); } // test whether the singly linked list is empty size_t size() const; // return the size (number of items in the singly linked list) void enqueue(int value); int dequeue(); void clear() { while(!this->empty()) this->dequeue(); } // remove all the items from the list ~SinglyLinkedList() { this->clear(); } private: SinglyLinkedListNode* head = nullptr; SinglyLinkedListNode* tail = nullptr; }; } #endif