fork download
  1. //If you are not sure what some lines of code do, try looking back at
  2. //previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. // SEE THE LAST EXAMPLE FOR COMMENTS ON THE ACTUAL LIST DATA STRUCTURE,
  10. // COMMENTS HERE ARE ONLY ON TEMPLATE FEATURES
  11.  
  12.  
  13. // Our forward declaration has to be template
  14. template <typename T> class LinkedList;
  15.  
  16. // Here our node is templated so each node can hold an arbitrary data type
  17. template <typename T>
  18. class node {
  19. // Our value paramter is of our template paramter "T"
  20. // This is also implemented here, in the class definition
  21. node(T v, node* n = NULL) {
  22. value = v;
  23. next = n;
  24. }
  25. // Don't need this to do anything
  26. ~node() {}
  27.  
  28. // The value this node holds should, of course, be of generic type "T"
  29. T value;
  30. node* next;
  31.  
  32. // Note that we must specify that LinkedList is templated
  33. friend class LinkedList<T>;
  34.  
  35. // We can't implement a friend here, as it's not a member
  36. // We also have to specify what kind of list we're outputting, so we do LinkedList<type>.
  37. // Hence, the friend must also be templated. Note that the template declaration goes before
  38. // "friend," and that we must use U as our argument, as T is already defined.
  39. template <typename U> friend ostream& operator<<(ostream& out, const LinkedList<U>& src);
  40. };
  41.  
  42. // Here our actual list is templated to accomodate the templated nodes and arbitrary data input/output
  43. template <typename T>
  44. class LinkedList {
  45. public:
  46. LinkedList() {
  47. head = NULL;
  48. }
  49. // We need a copy constructor for later
  50. LinkedList(const LinkedList& src) {
  51. head = NULL;
  52. node<T>* temp = src.head;
  53. while(temp) {
  54. addValue(temp->value);
  55. temp = temp->next;
  56. }
  57. }
  58. // We need an assignment operator for later
  59. LinkedList& operator=(const LinkedList& src) {
  60. while(head) {
  61. node<T>* temp = head->next;
  62. delete head;
  63. head = temp;
  64. }
  65. node<T>* temp = src.head;
  66. while(temp) {
  67. addValue(temp->value);
  68. temp = temp->next;
  69. }
  70. }
  71. ~LinkedList() {
  72. while(head) {
  73. node<T>* temp = head->next;
  74. delete head;
  75. head = temp;
  76. }
  77. }
  78.  
  79. // Here we add a value of generic type T
  80. void addValue(T value) {
  81. head = new node<T>(value,head);
  82. }
  83. // Here we remove a value, returning generic type T
  84. T removeValue() {
  85. T result;
  86. if(head) {
  87. result = head->value;
  88. node<T>* temp = head->next;
  89. delete head;
  90. head = temp;
  91. }
  92. return result;
  93. }
  94.  
  95. // Again, we can't implement a friend here, and we must specify that the LinkedList uses type U
  96. template <typename U> friend ostream& operator<<(ostream& out, const LinkedList<U>& src);
  97.  
  98. private:
  99. // Our node must use generic type T
  100. node<T>* head;
  101. };
  102.  
  103. // Here we actually implement our friend, note the use of another template
  104. template <typename T>
  105. ostream& operator<<(ostream& out, const LinkedList<T>& src) {
  106. node<T>* temp = src.head;
  107. while(temp) {
  108. // Output brackets for list-of-list clarity
  109. // Later, we create a list of lists, so in that case "out << temp->value"
  110. // will actually recursively call this function
  111. out << "[" << temp->value << "]";
  112. if(temp->next) {
  113. out << " -> ";
  114. }
  115.  
  116. temp = temp->next;
  117. }
  118.  
  119. return out;
  120. }
  121.  
  122. int main() {
  123. // Declare list using integers
  124. LinkedList<int> intList;
  125. // Declare list using floats
  126. LinkedList<float> floatList;
  127. // Delcare list using lists that use integers
  128. // prior to c++11, you have to leave a space between two '>' brackets in a
  129. // nested template, otherwise the compiler thinks you're using the ">>" operator
  130. LinkedList<LinkedList<int> > listList;
  131.  
  132. // Output lists
  133. cout << "Int list: " << intList << endl;
  134. cout << "Float list: " << floatList << endl;
  135. cout << "List list: " << listList << endl;
  136.  
  137. // Add values to the int list
  138. for(int index = 0; index < 10; index++) {
  139. intList.addValue(index);
  140. }
  141.  
  142. // Add values to the float list
  143. for(float index = 0.5; index < 10; index++) {
  144. floatList.addValue(index);
  145. }
  146.  
  147. // Add values to list list
  148. for(int index = 0; index < 10; index++) {
  149. LinkedList<int> value;
  150. for(int index = 0; index < 10; index++) {
  151. value.addValue(index);
  152. }
  153. listList.addValue(value);
  154. }
  155.  
  156. // Output lists
  157. cout << "Int list: " << intList << endl;
  158. cout << "Float list: " << floatList << endl;
  159. cout << "List list: " << listList << endl;
  160.  
  161. system("pause");
  162. return 0;
  163. }
  164.  
Success #stdin #stdout #stderr 0s 3416KB
stdin
Standard input is empty
stdout
Int list: 
Float list: 
List list: 
Int list: [9] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0]
Float list: [9.5] -> [8.5] -> [7.5] -> [6.5] -> [5.5] -> [4.5] -> [3.5] -> [2.5] -> [1.5] -> [0.5]
List list: [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]] -> [[0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7] -> [8] -> [9]]
stderr
sh: 1: pause: not found