Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. // THIS IS THE HEADER FILE
  2.  
  3. #pragma once
  4.  
  5. #include <stdexcept>
  6.  
  7. using namespace std;
  8.  
  9. template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
  10.  
  11. template <typename E>
  12. class SNode { // singly linked list node
  13. private:
  14. E elem; // linked list element value
  15. SNode<E> *next; // next item in the list
  16. SNode<E> *prev;
  17. SNode<E> *current;
  18. friend class SLinkedList<E>; // provide SLinkedList access
  19.  
  20.  
  21. //public:
  22. // void addC(SNode<E>* u, const E &b);
  23. };
  24.  
  25. template <typename E>
  26. class SLinkedList { // a singly linked list
  27. public:
  28. SLinkedList(); // empty list constructor
  29. ~SLinkedList(); // destructor
  30. bool empty() const; // is list empty?
  31. E& front(); // return front element
  32. void addFront(const E& e); // add to front of list
  33. void removeFront(); // remove front item list
  34. int size() const; // list size
  35.  
  36. void addC(SNode<E>* u, const E &b);
  37. E& printElements();
  38. //ConsecutiveNumList();
  39. private:
  40. SNode<E>* head; // head of the list
  41. int n; // number of items
  42. };
  43.  
  44. template <typename E>
  45. SLinkedList<E>::SLinkedList() // constructor
  46. {
  47. head = nullptr;
  48. n = 0;
  49. }
  50.  
  51. template <typename E>
  52. bool SLinkedList<E>::empty() const // is list empty?
  53. {
  54. return head == NULL; // can also use return (n == 0);
  55. }
  56.  
  57. template <typename E>
  58. E& SLinkedList<E>::front() // return front element
  59. {
  60. if (empty()) throw length_error("empty list");
  61. return head->elem;
  62. }
  63.  
  64. template <typename E>
  65. SLinkedList<E>::~SLinkedList() // destructor
  66. {
  67. while (!empty()) removeFront();
  68. }
  69.  
  70. template <typename E>
  71. void SLinkedList<E>::addFront(const E& e) { // add to front of list
  72. SNode<E>* v = new SNode<E>; // create new node
  73. SNode<E>*tmp = new SNode<E>;
  74. v->elem = e; // store data
  75. v->next = head; // head now follows v
  76. head = v; // v is now the head
  77. n++;
  78. }
  79.  
  80.  
  81.  
  82. template <typename E>
  83. void SLinkedList<E>::removeFront() { // remove front item
  84. if (empty()) throw length_error("empty list");
  85. SNode<E>* old = head; // save current head
  86. head = old->next; // skip over old head
  87. delete old; // delete the old head
  88. n--;
  89. }
  90.  
  91.  
  92. template <typename E>
  93. int SLinkedList<E>::size() const { // list size
  94. return n;
  95. }
  96.  
  97.  
  98. template <typename E>
  99. void SLinkedList<E>::addC(SNode<E>* u, const E & b) {
  100. SNode<E>*u = new SNode<E>;
  101. SNode<E>*tmp = new SNode<E>;
  102. u->elem = b;
  103. u->next = head;
  104. head = u;
  105. n++;
  106.  
  107. }
  108.  
  109. template <typename E>
  110. E& SLinkedList<E>::printElements()
  111. {
  112. SNode<E> const* current = new SNode<E>;
  113. current = head;
  114. if (empty()) throw length_error("empty list");
  115. return head->elem;
  116. delete(current);
  117.  
  118.  
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement