INF205/lab_3/sequences-int/singly-linked-list.h
2024-03-10 13:16:20 +01:00

52 lines
1.4 KiB
C++

#ifndef SINGLY_LINKED_LIST_H
#define SINGLY_LINKED_LIST_H
#include <cassert>
#include "sequence.h"
#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 element);
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