Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // THIS IS THE HEADER FILE
- #pragma once
- #include <stdexcept>
- using namespace std;
- template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
- template <typename E>
- class SNode { // singly linked list node
- private:
- E elem; // linked list element value
- SNode<E> *next; // next item in the list
- SNode<E> *prev;
- SNode<E> *current;
- friend class SLinkedList<E>; // provide SLinkedList access
- //public:
- // void addC(SNode<E>* u, const E &b);
- };
- template <typename E>
- class SLinkedList { // a singly linked list
- public:
- SLinkedList(); // empty list constructor
- ~SLinkedList(); // destructor
- bool empty() const; // is list empty?
- E& front(); // return front element
- void addFront(const E& e); // add to front of list
- void removeFront(); // remove front item list
- int size() const; // list size
- void addC(SNode<E>* u, const E &b);
- E& printElements();
- //ConsecutiveNumList();
- private:
- SNode<E>* head; // head of the list
- int n; // number of items
- };
- template <typename E>
- SLinkedList<E>::SLinkedList() // constructor
- {
- head = nullptr;
- n = 0;
- }
- template <typename E>
- bool SLinkedList<E>::empty() const // is list empty?
- {
- return head == NULL; // can also use return (n == 0);
- }
- template <typename E>
- E& SLinkedList<E>::front() // return front element
- {
- if (empty()) throw length_error("empty list");
- return head->elem;
- }
- template <typename E>
- SLinkedList<E>::~SLinkedList() // destructor
- {
- while (!empty()) removeFront();
- }
- template <typename E>
- void SLinkedList<E>::addFront(const E& e) { // add to front of list
- SNode<E>* v = new SNode<E>; // create new node
- SNode<E>*tmp = new SNode<E>;
- v->elem = e; // store data
- v->next = head; // head now follows v
- head = v; // v is now the head
- n++;
- }
- template <typename E>
- void SLinkedList<E>::removeFront() { // remove front item
- if (empty()) throw length_error("empty list");
- SNode<E>* old = head; // save current head
- head = old->next; // skip over old head
- delete old; // delete the old head
- n--;
- }
- template <typename E>
- int SLinkedList<E>::size() const { // list size
- return n;
- }
- template <typename E>
- void SLinkedList<E>::addC(SNode<E>* u, const E & b) {
- SNode<E>*u = new SNode<E>;
- SNode<E>*tmp = new SNode<E>;
- u->elem = b;
- u->next = head;
- head = u;
- n++;
- }
- template <typename E>
- E& SLinkedList<E>::printElements()
- {
- SNode<E> const* current = new SNode<E>;
- current = head;
- if (empty()) throw length_error("empty list");
- return head->elem;
- delete(current);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement