From 7fbf4ea0b5679b83e637006b48ca1a287ebf3302 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Sat, 12 Nov 2022 18:37:07 +0200 Subject: add non const front & back --- Seminar05/LinkedList.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Seminar05/LinkedList.hpp b/Seminar05/LinkedList.hpp index 1281a43..58f8794 100644 --- a/Seminar05/LinkedList.hpp +++ b/Seminar05/LinkedList.hpp @@ -129,6 +129,9 @@ public: const T& front() const; const T& back() const; + T& front(); + T& back(); + bool empty() const; size_t size() const; @@ -291,6 +294,14 @@ const T& LinkedList::front() const { return head->data; } +template +T& LinkedList::front() { + if(empty()) + throw std::runtime_error("Empty list!"); + + return head->data; +} + template const T& LinkedList::back() const { if(empty()) @@ -299,6 +310,14 @@ const T& LinkedList::back() const { return tail->data; } +template +T& LinkedList::back() { + if(empty()) + throw std::runtime_error("Empty list!"); + + return tail->data; +} + template LinkedList::~LinkedList() { free(); -- cgit v1.3.1-23-gd82d