fork download
  1. //
  2. // main.cpp
  3. // CS32_Hwk2
  4. //
  5. // Created by Sowmya Sridharan on 7/21/17.
  6. // Copyright © 2017 project. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <iostream>
  11. #include <string>
  12. using namespace std;
  13.  
  14. typedef string ItemType;
  15.  
  16. struct Node {
  17. ItemType value;
  18. Node *next;
  19. };
  20.  
  21. class LinkedList {
  22.  
  23. private:
  24. Node *head;
  25.  
  26. public:
  27.  
  28. // default constructor
  29. LinkedList() : head(nullptr) { }
  30.  
  31. // copy constructor
  32. LinkedList(const LinkedList& rhs);
  33.  
  34. // Destroys all the dynamically allocated memory
  35. // in the list.
  36. ~LinkedList();
  37.  
  38. // assignment operator
  39. const LinkedList& operator=(const LinkedList& rhs);
  40.  
  41. // Inserts val at the front of the list
  42. void insertToFront(const ItemType &val);
  43.  
  44. // Prints the LinkedList
  45. void printList() const;
  46.  
  47. // Sets item to the value at position i in this
  48. // LinkedList and return true, returns false if
  49. // there is no element i
  50. bool get(int i, ItemType& item) const;
  51.  
  52. // Reverses the LinkedList
  53. void reverseList();
  54.  
  55. // Prints the LinkedList in reverse order
  56. void printReverse() const;
  57.  
  58. // Appends the values of other onto the end of this
  59. // LinkedList.
  60. void append(const LinkedList &other);
  61.  
  62. // Exchange the contents of this LinkedList with the other one.
  63. void swap(LinkedList &other);
  64.  
  65. // Returns the number of items in the Linked List.
  66. int size() const;
  67. };
  68.  
  69. LinkedList::~LinkedList(){
  70. Node* p;
  71. while (head!= nullptr) {
  72. p = head;
  73. head = head->next;
  74. delete p;
  75. }
  76. }
  77.  
  78.  
  79. void LinkedList::insertToFront(const ItemType &val){
  80. Node* temp = new Node;
  81. temp->value = val;
  82. temp->next = head;
  83. head = temp;
  84. }
  85. void LinkedList::printList() const {
  86. Node* temp = head;
  87. while (temp!=nullptr) {
  88. cout << temp->value << " ";
  89. temp = temp->next;
  90. }
  91. cout << endl;
  92. }
  93.  
  94. // Sets item to the value at position i in this
  95. // LinkedList and return true, returns false if
  96. // there is no element i
  97. bool LinkedList:: get(int i, ItemType& item) const{
  98. Node* temp = head;
  99. for (int j =0; j < i; j++) {
  100. if (temp->next == nullptr) return false;
  101. temp=temp->next;
  102. }
  103. item = temp->value;
  104. return true;
  105. }
  106.  
  107. // Reverses the LinkedList
  108. void LinkedList::reverseList(){
  109. Node* nxt = nullptr;
  110. Node* current = head;
  111. Node* prev = nullptr;
  112. while (current!=nullptr){
  113. nxt = current->next;
  114. current->next = prev;
  115. prev = current;
  116. current = nxt;
  117. }
  118. head = prev;
  119. }
  120.  
  121. // Prints the LinkedList in reverse order
  122. void LinkedList:: printReverse() const {
  123. Node* headtwo;
  124. Node* tempptr = head;
  125. while (tempptr != nullptr) {
  126. Node* temp = new Node;
  127. headtwo = temp;
  128. temp->value = tempptr->value;
  129. tempptr = tempptr->next;
  130. //temp->next = headtwo;
  131. //headtwo = temp;
  132. headtwo = temp -> next;
  133. }
  134. Node* temp2 = headtwo;
  135. while (temp2!=nullptr) {
  136. cout << temp2->value << " ";
  137. temp2 = temp2->next;
  138. }
  139. cout << endl;
  140. }
  141. // Appends the values of other onto the end of this
  142. // LinkedList.
  143. void LinkedList:: append(const LinkedList &other) {
  144. Node* temp = head;
  145. while (temp->next != nullptr) temp = temp->next;
  146. temp->next = other.head;
  147. }
  148. // Exchange the contents of this LinkedList with the other one.
  149. void LinkedList:: swap(LinkedList &other) {
  150. Node* temp = new Node;
  151. Node* temp2 = new Node;
  152. temp2 = other.head;
  153. Node* placeholder = head;
  154. while (placeholder != nullptr) {
  155. temp->value = temp2->value;
  156. temp2->value = placeholder->value;
  157. placeholder->value = temp->value;
  158. placeholder = placeholder->next;
  159. }
  160. }
  161.  
  162. // Returns the number of items in the Linked List.
  163. int LinkedList::size() const
  164. {
  165. Node* temp = head;
  166. int count = 0;
  167. while (temp != nullptr) {
  168. count++;
  169. temp = temp->next;
  170. }
  171. return count;
  172. }
  173.  
  174. int main () {
  175. LinkedList ls;
  176. ls.insertToFront("Jack");
  177. ls.insertToFront("Germaine");
  178. ls.insertToFront("Agatha");
  179. ls.insertToFront("Agnes");
  180.  
  181. for (int k = 0; k < ls.size(); k++)
  182. {
  183. string x;
  184. ls.get(k, x);
  185. cout << x << endl;
  186. }
  187. LinkedList ls2;
  188. ls2.insertToFront("George");
  189. ls2.insertToFront("Loise");
  190. ls2.insertToFront("Lionel");
  191. ls2.insertToFront("Helen");
  192. ls2.printList();
  193. ls2.printReverse();
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Agnes
Agatha
Germaine
Jack
Helen Lionel Loise George