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. // Forward declare the LinkedList class for the node to use
  9. class LinkedList;
  10.  
  11. // Example node class for linked lists
  12. class node {
  13. // Parametrized constructor
  14. node(int v, node* n = NULL);
  15. // Destructor
  16. ~node();
  17.  
  18. // Data members. Note that one part is the data held in the node,
  19. // and one part is a pointer (or link) to the next node in the list
  20. int value;
  21. node* next;
  22.  
  23. // We only want our list class to use nodes, so we can make the node
  24. // constructor private, and have the list class as a friend
  25. friend class LinkedList;
  26.  
  27. // We also need the list insertion operator to be a friend of the node
  28. // class so it can retrieve values from nodes in the list.
  29. friend ostream& operator<<(ostream& out, const LinkedList& src);
  30. };
  31.  
  32. // Example singly-circularly linked list class
  33. // values will be added and removed at the "head" of the list,
  34. // making this technically a stack. We'll learn more about those later
  35. class LinkedList {
  36. public:
  37. // Default constructor
  38. LinkedList();
  39. // Desctructor
  40. ~LinkedList();
  41.  
  42. // Example manipulation functions
  43. void addValue(int value);
  44. int removeValue();
  45.  
  46. // You'd usually have several more functions here, but for the purposes
  47. // of this example only the essential few have been shown
  48.  
  49. // Output operator
  50. friend ostream& operator<<(ostream& out, const LinkedList& src);
  51.  
  52. private:
  53. node* head;
  54. };
  55.  
  56. node::node(int v, node* n) {
  57. value = v;
  58. next = n;
  59. }
  60.  
  61. node::~node() {
  62.  
  63. }
  64.  
  65. LinkedList::LinkedList() {
  66. // Start with no nodes
  67. head = NULL;
  68. }
  69.  
  70. // Clear all nodes (delete memory)
  71. LinkedList::~LinkedList() {
  72. // While we still have nodes in the list
  73. while(head) {
  74. // Store a pointer to the next node
  75. node* temp = head->next;
  76. // Delete the current head node
  77. delete head;
  78. // Set the head node to the next node.
  79. // Note that when we get to the end of the list, this will
  80. // make head NULL, stopping this loop.
  81. head = temp;
  82. }
  83. }
  84.  
  85. void LinkedList::addValue(int value) {
  86. // Here we create a new node with the new value and "head" as the next node
  87. // and set head to point to it. This inserts the new node at the start of the list,
  88. // as it will be the new head, and point to the previous head node. Note that if
  89. // head was NULL, the new node will have NULL as the next node, signifying that it's
  90. // the last node in the list.
  91. head = new node(value,head);
  92. }
  93.  
  94. int LinkedList::removeValue() {
  95. int result = 0;
  96. // Check that we have at least one node
  97. if(head) {
  98. // Get result
  99. result = head->value;
  100. // Store a pointer to the next node
  101. node* temp = head->next;
  102. // Delete the head node
  103. delete head;
  104. // Set the head to the next node. If we just removed the last node, head will
  105. // be set to NULL, signifying that the list is empty.
  106. head = temp;
  107. }
  108. return result;
  109. }
  110.  
  111. // Operator to output the list graphically
  112. ostream& operator<<(ostream& out, const LinkedList& src) {
  113. // Declare temp pointer to iterate through the list
  114. node* temp = src.head;
  115. // Iterate through the list
  116. while(temp) {
  117. out << temp->value;
  118. // Write an arrow if there is another node
  119. if(temp->next) {
  120. out << " -> ";
  121. }
  122.  
  123. // Move the next node
  124. temp = temp->next;
  125. }
  126.  
  127. // Return out for chaining
  128. return out;
  129. }
  130.  
  131. int main() {
  132. // Test our list class
  133.  
  134. LinkedList l1;
  135. cout << "List 1: " << l1 << endl;
  136.  
  137. for(int index = 0; index < 10; index++) {
  138. l1.addValue(index);
  139. }
  140.  
  141. cout << "List 1: " << l1 << endl;
  142.  
  143. l1.removeValue();
  144. l1.removeValue();
  145. l1.removeValue();
  146.  
  147. cout << "List 1: " << l1 << endl;
  148.  
  149. system("pause");
  150. return 0;
  151. }
  152.  
Success #stdin #stdout #stderr 0s 3456KB
stdin
Standard input is empty
stdout
List 1: 
List 1: 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
List 1: 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0
stderr
sh: 1: pause: not found