Advertisement
prprice16

Striniglist

Nov 23rd, 2021
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. //node class
  7. class StringNode
  8. {
  9. public:
  10. string data;
  11. StringNode* next;
  12.  
  13. StringNode() { data = ""; next = nullptr; }
  14. StringNode(string d) { data = d; next = nullptr; }
  15.  
  16. };
  17.  
  18. class StringList
  19. {
  20. protected:
  21. StringNode* head;
  22.  
  23. public:
  24. //default constructor
  25. StringList();
  26. ~StringList();
  27. void append(string); //put at end of list
  28. void insert(string); //insert in order
  29. void display();
  30. bool deleteval(string);
  31.  
  32. //copy constructor
  33. void copyList(const StringList&);
  34. StringList(const StringList&);
  35.  
  36. };
  37.  
  38. /*
  39. //default constructor
  40. StringList::StringList()
  41. {
  42. head = nullptr; //empty list
  43. }
  44.  
  45. //destructor
  46. StringList::~StringList()
  47. {
  48. StringNode* nodeptr; //walks the list
  49. StringNode* nextnode; //keeps next node
  50. //start at beginning of list
  51. nodeptr = head;
  52. //loop while nodes to delete
  53. while (nodeptr != nullptr)
  54. {
  55. //deleting node at nodeptr
  56. //cout << "Deleting " << nodeptr->data << endl;
  57. //but must save address of next node first
  58. nextnode = nodeptr->next;
  59. //now delete nodeptr
  60. delete nodeptr;
  61. //move to next node
  62. nodeptr = nextnode;
  63. }
  64. }
  65. */
  66.  
  67. /*
  68. void StringList::insert(string val)
  69. {
  70. //insert in order
  71. //declare some pointers
  72. StringNode* nodeptr;
  73. //create new node
  74. StringNode* newnode = new StringNode(val);
  75. StringNode* previous = nullptr;
  76.  
  77. //if empty list, make this first node
  78. if (head == nullptr)
  79. {
  80. head = newnode;
  81. }
  82. else
  83. {
  84. //start at first node
  85. nodeptr = head;
  86. //skip all nodes less than val
  87. while (nodeptr != nullptr && nodeptr->data < val)
  88. {
  89. //move to next
  90. previous = nodeptr;
  91. nodeptr = nodeptr->next;
  92. }
  93. //is it the new first node?
  94. if (previous == nullptr)
  95. {
  96. head = newnode;
  97. newnode->next = nodeptr;
  98. }
  99. else
  100. {
  101. //insert between previous and nodeptr
  102. previous->next = newnode;
  103. newnode->next = nodeptr;
  104. }
  105. }
  106. }
  107. */
  108.  
  109. /*
  110. void StringList::copyList(const StringList& other)
  111. {
  112. //make a copy of the list in other, node by node
  113. StringNode* nodeptr;
  114. StringNode* newnode;
  115. StringNode* previous = nullptr;
  116. //if empty list there are no nodes to be created
  117. if (other.head == nullptr)
  118. {
  119. head = nullptr;
  120. return;
  121. }
  122. //if here then there is at least one node to copy
  123. //start at first node
  124. nodeptr = other.head;
  125. //copy first node, allocate a node
  126. head = new StringNode(nodeptr->data);
  127. previous = head;
  128. //move to next node
  129. nodeptr = nodeptr->next;
  130. //copy remaining list
  131. while (nodeptr != nullptr)
  132. {
  133. //copy this node
  134. newnode = new StringNode(nodeptr->data);
  135. //conncect to previous node
  136. previous->next = newnode;
  137. //move to next node
  138. previous = newnode;
  139. nodeptr = nodeptr->next;
  140. }
  141. }
  142.  
  143. StringList::StringList(const StringList& orig)
  144. {
  145. copyList(orig);
  146. }
  147. */
  148.  
  149.  
  150. /*
  151. void StringList::append(string val)
  152. {
  153. //cout << "append" << endl;
  154. //add a node to the end of the list with val as data
  155. //declare some pointers
  156. StringNode* nodeptr;
  157. //create new node
  158. StringNode* newnode = new StringNode(val);
  159. //2 cases
  160. //empty list, this is now the first node
  161. if (head == nullptr)
  162. {
  163. //make this the first node
  164. head = newnode;
  165. }
  166. else //non empty list, need to find the end
  167. {
  168. //start at beginning
  169. nodeptr = head;
  170. //loop until you find nullptr
  171. while (nodeptr->next != nullptr)
  172. {
  173. //move to next node
  174. nodeptr = nodeptr->next;
  175. }
  176. //when loop ends nodeptr is on the last value in list
  177. //newnode goes after this one
  178. nodeptr->next = newnode;
  179. }
  180. }
  181. */
  182.  
  183. /*
  184. void StringList::display()
  185. {
  186. //print data at each node
  187. StringNode* nodeptr;
  188. //start at beginning
  189. nodeptr = head;
  190. while (nodeptr) //nodeptr != nullptr
  191. {
  192. cout << nodeptr->data << endl;
  193. //move to next
  194. nodeptr = nodeptr->next;
  195. }
  196. }
  197. */
  198.  
  199. /*
  200. bool StringList::deleteval(string val)
  201. {
  202. //delete first node with val in it
  203. //return true if delete happens, false if not found
  204. //declare some pointers
  205. StringNode* nodeptr; //walker
  206. StringNode* previous = nullptr;
  207.  
  208. //if list is empty do nothing
  209. if (head == nullptr)
  210. return false;
  211.  
  212. //is it the first node?
  213. if (head->data == val)
  214. {
  215. //delete first node
  216. //this means second node becomes head, save this value
  217. nodeptr = head->next;
  218. //delete first node
  219. delete head;
  220. //reset beginning of list
  221. head = nodeptr;
  222. return true;
  223. }
  224. //search list for val
  225. nodeptr = head;
  226. //look for val or end of list
  227. while (nodeptr != nullptr && nodeptr->data != val)
  228. {
  229. //keep looking
  230. //update previous
  231. previous = nodeptr;
  232. //move to next
  233. nodeptr = nodeptr->next;
  234. }
  235. //when the loop ends either it was found or reached end of list
  236. if (nodeptr == nullptr)
  237. return false; //not found
  238. //nodeptr has the value to delete
  239. //link around nodeptr
  240. previous->next = nodeptr->next;
  241. //delete node
  242. delete nodeptr;
  243. return true;
  244.  
  245. }
  246. */
  247.  
  248.  
  249.  
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement