Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SLINKEDLIST_H
- #define SLINKEDLIST_H
- #include <iostream>
- #include "SNode.h"
- #include <stdexcept>
- #include "SLinkedIterator.h"
- #include "SLinkedIteratorConst.h"
- template <typename Type>
- using iterator = SLinkedIterator<Type>;
- template <typename Type>
- using const_iterator = SLinkedIteratorConst<Type>;
- template <typename Type>
- using Node = SNode<Type>;
- template <typename Type>
- class SLinkedList {
- private :
- Node<Type> *head;
- Node<Type> *tail;
- size_t count;
- public :
- SLinkedList();
- SLinkedList(const SLinkedList& list);
- SLinkedList(SLinkedList&& list);
- ~SLinkedList();
- SLinkedList& operator=(const SLinkedList& list);
- SLinkedList& operator=(SLinkedList&& list);
- void push_back(const Type& value);
- void push_back(Type&& value);
- void push_front(const Type& value);
- void push_front(Type&& value);
- void pop_back();
- void pop_front();
- Type& front();
- const Type& front() const;
- Type& back();
- const Type& back() const;
- size_t size() const;
- bool empty() const;
- void print() const;
- void clear();
- iterator<Type> begin();
- iterator<Type> end();
- const_iterator<Type> begin() const;
- const_iterator<Type> end() const;
- iterator<Type> insert(iterator<Type> position, const Type& value);
- iterator<Type> insert(iterator<Type> position, Type&& value);
- };
- template<typename Type>
- iterator<Type> SLinkedList<Type>::begin() { return iterator<Type>(head);}
- template<typename Type>
- iterator<Type> SLinkedList<Type>::end() { return iterator<Type>(tail->getNext());}
- template<typename Type>
- const_iterator<Type> SLinkedList<Type>::begin() const { return const_iterator<Type>(head);}
- template<typename Type>
- const_iterator<Type> SLinkedList<Type>::end() const { return const_iterator<Type>(tail->getNext());}
- template<typename Type>
- SLinkedList<Type>::SLinkedList() : head(nullptr), tail(nullptr), count(0) {}
- template<typename Type>
- iterator<Type> SLinkedList<Type>::insert(iterator<Type> position, const Type& value) {
- Node<Type> *newNode = new Node<Type>(value,position.current->getNext());
- Node<Type> *temp = head;
- while(temp != position.current) {
- temp = temp->getNext();
- }
- temp->setNext(newNode);
- }
- template<typename Type>
- iterator<Type> SLinkedList<Type>::insert(iterator<Type> position, Type&& value) {
- Node<Type> *newNode = new Node<Type>(value,position.current->getNext());
- Node<Type> *temp = head;
- while(temp != position.current) {
- temp = temp->getNext();
- }
- temp->setNext(newNode);
- }
- template<typename Type>
- SLinkedList<Type>::SLinkedList(const SLinkedList& list ) {
- count = list.count;
- head = new Node<Type>(list.head->getInfo(), nullptr);
- tail = new Node<Type>
- Node<Type> *temp = list;
- while(temp) {
- push_back()
- }
- }
- template<typename Type>
- SLinkedList<Type>::SLinkedList(SLinkedList&& list) {
- }
- template<typename Type>
- SLinkedList<Type>::~SLinkedList(){
- clear();
- }
- template<typename Type>
- SLinkedList<Type>& SLinkedList<Type>::operator=(const SLinkedList& list) {
- return *this;
- }
- template<typename Type>
- SLinkedList<Type>& SLinkedList<Type>::operator=(SLinkedList&& list) {
- return *this;
- }
- template<typename Type>
- void SLinkedList<Type>::push_back(const Type& value) {
- Node<Type> *newNode = new Node<Type>(value, nullptr);
- if(!head) {
- head = newNode;
- tail = newNode;
- }
- else {
- Node<Type> *temp = head;
- while(temp->getNext()) {
- temp = temp->getNext();
- }
- temp->setNext(newNode);
- tail = newNode;
- }
- ++count;
- }
- template<typename Type>
- void SLinkedList<Type>::push_back(Type&& value) {
- Node<Type> *newNode = new Node<Type>(value, nullptr);
- if(!head) {
- head = newNode;
- tail = newNode;
- }
- else {
- Node<Type> *temp = head;
- while(temp->getNext()) {
- temp = temp->getNext();
- }
- temp->setNext(newNode);
- tail = newNode;
- }
- ++count;
- }
- template<typename Type>
- void SLinkedList<Type>::push_front(const Type& value) {
- Node<Type> *newNode = new Node<Type>(value, nullptr);
- if(!head) {
- head = newNode;
- tail = newNode;
- }
- else{
- newNode->setNext(head);
- head = newNode;
- }
- ++count;
- }
- template<typename Type>
- void SLinkedList<Type>::push_front(Type&& value) {
- Node<Type> *newNode = new Node<Type>(value, nullptr);
- if(!head) {
- head = newNode;
- tail = newNode;
- }
- else{
- newNode->setNext(head);
- head = newNode;
- }
- ++count;
- }
- template<typename Type>
- void SLinkedList<Type>::pop_back() {
- Node<Type> *newNode = head;
- while(newNode->getNext()->getNext())
- newNode = newNode->getNext();
- tail = newNode;
- tail->setNext(nullptr);
- newNode = newNode->getNext();
- delete newNode;
- --count;
- }
- template<typename Type>
- void SLinkedList<Type>::pop_front() {
- Node<Type> *newNode = head;
- head = head->getNext();
- delete newNode;
- }
- template<typename Type>
- Type& SLinkedList<Type>::front() {
- return head->getInfo();
- }
- template<typename Type>
- const Type& SLinkedList<Type>::front() const {
- return head->getInfo();
- }
- template<typename Type>
- Type& SLinkedList<Type>::back() {
- return tail->getInfo();
- }
- template<typename Type>
- const Type& SLinkedList<Type>::back() const {
- return tail->getInfo();
- }
- template<typename Type>
- size_t SLinkedList<Type>::size() const {
- return count;
- }
- template<typename Type>
- bool SLinkedList<Type>::empty() const {
- return (head == nullptr);
- }
- template<typename Type>
- void SLinkedList<Type>::print() const {
- Node<Type> *temp = head;
- while(temp) {
- std::cout << temp->getInfo() << std::endl;
- temp = temp->getNext();
- }
- }
- template<typename Type>
- void SLinkedList<Type>::clear() {
- Node<Type> *temp = head;
- while(head->getNext()) {
- pop_front();
- }
- count = 0;
- }
- #endif // SLINKEDLIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement