fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <stdexcept>
  4. #include <initializer_list> // C++11 and later only
  5.  
  6. template <typename E>
  7. class SLinkedList {
  8. public:
  9. SLinkedList();
  10. SLinkedList(const E *vals, int num_vals);
  11. SLinkedList(std::initializer_list<E> vals); // C++11 and later only
  12.  
  13. ~SLinkedList();
  14.  
  15. bool empty() const;
  16. int size() const;
  17.  
  18. E& front();
  19.  
  20. void addFront(const E &e);
  21. void removeFront();
  22.  
  23. void printList() const;
  24.  
  25. private:
  26. class SNode
  27. {
  28. public:
  29. E elem;
  30. SNode *next;
  31. SNode(const E &e, SNode *n = NULL);
  32. };
  33.  
  34. SNode* head;
  35. int n; // number of items
  36. };
  37.  
  38. template <typename E>
  39. SLinkedList<E>::SNode::SNode(const E &e, SLinkedList<E>::SNode *n)
  40. : elem(e), next(n) { }
  41.  
  42. template <typename E>
  43. SLinkedList<E>::SLinkedList()
  44. : head(NULL), n(0) { }
  45.  
  46. template <typename E>
  47. SLinkedList<E>::SLinkedList(const E *vals, int num_vals)
  48. : head(NULL), n(0)
  49. {
  50. for (int i = num_vals-1; i >= 0; --i)
  51. addFront(vals[i]);
  52.  
  53. /* alternatively:
  54.   SNode **ptr = &head;
  55.   for (int i = 0; i < num_vals; ++i)
  56.   {
  57.   *ptr = new SNode(vals[i]);
  58.   ++n;
  59.   ptr = &((*ptr)->next);
  60.   }
  61.   */
  62. }
  63.  
  64. template <typename E>
  65. SLinkedList<E>::SLinkedList(std::initializer_list<E> vals)
  66. : head(NULL), n(0)
  67. {
  68. const E *begin = vals.begin(), *iter = vals.end();
  69. while (iter != begin)
  70. addFront(*(--iter));
  71.  
  72. /* alternatively:
  73.   const E *iter = vals.begin(), *end = vals.end();
  74.   SNode **ptr = &head;
  75.   while (iter != end)
  76.   {
  77.   *ptr = new SNode(*iter);
  78.   ++n;
  79.   ptr = &((*ptr)->next);
  80.   }
  81.   */
  82. }
  83.  
  84. template <typename E>
  85. SLinkedList<E>::~SLinkedList()
  86. {
  87. while (head)
  88. removeFront();
  89. }
  90.  
  91. template <typename E>
  92. bool SLinkedList<E>::empty() const
  93. {
  94. return (!head);
  95. }
  96.  
  97. template <typename E>
  98. int SLinkedList<E>::size() const
  99. {
  100. return n;
  101. }
  102.  
  103. template <typename E>
  104. E& SLinkedList<E>::front()
  105. {
  106. if (!head) throw std::length_error("empty list");
  107. return head->elem;
  108. }
  109.  
  110. template<typename E>
  111. void SLinkedList<E>::printList() const
  112. {
  113. SNode *p = head;
  114. while (p)
  115. {
  116. std::cout << p->elem << " ";
  117. p = p->next;
  118. }
  119. std::cout << std::endl;
  120. }
  121.  
  122. template <typename E>
  123. void SLinkedList<E>::addFront(const E& e)
  124. {
  125. head = new SNode(e, head);
  126. ++n;
  127. }
  128.  
  129. template <typename E>
  130. void SLinkedList<E>::removeFront()
  131. {
  132. SNode* old = head;
  133. if (!old) throw std::length_error("empty list");
  134. head = old->next;
  135. --n;
  136. delete old;
  137. }
  138.  
  139. int main() {
  140. int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  141. SLinkedList<int> list1(a, 10);
  142. list1.printList();
  143.  
  144. SLinkedList<int> list2({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
  145. list2.printList();
  146.  
  147. return 0;
  148. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 5 6 7 8 9