Advertisement
Guest User

Feick Program 5 code

a guest
Dec 7th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct ClassInfo
  6. {
  7. string name;
  8. string building;
  9. string code;
  10. string days;
  11. string times;
  12. };
  13.  
  14. struct NodeType;
  15. typedef NodeType* NodePtr;
  16.  
  17. struct NodeType
  18. {
  19. ClassInfo classes;
  20. NodePtr next;
  21. };
  22.  
  23. // Adds data to an existing node
  24. // Pre: Valid NodePtr w/ allocated memory
  25. // Post: NodePtr contains valid info or set to nullptr if no info
  26. void FillInfo(NodePtr&);
  27.  
  28. // Allocates memory and initializes nodePtr->next to NULL
  29. // Pre: None
  30. // Post: Allocated nodePtr with ->next set to NULL
  31. NodePtr GetNode();
  32.  
  33. // Inserts node into list in sorted location
  34. // Pre: Valid pointer for list and prefilled NodePtr
  35. // Post: Sorted list
  36. void InsertNode(NodePtr&, NodePtr);
  37.  
  38. // Returns a pointer to the node with matching last name.
  39. // Pre: Valid NodePtr and string.
  40. // Post: Returns pointer to matching node or nullptr if not found.
  41. NodePtr SearchNode(NodePtr head, string name);
  42.  
  43. // Prints out list
  44. // Pre: Valid nodeptr
  45. void PrintList(NodePtr);
  46.  
  47. // Save data to file
  48. // Pre: Valid nodeptr
  49. void SaveFile(NodePtr);
  50.  
  51. // Support function to capitalize a name
  52. // Pre: s must point to a valid string
  53. // Post: s will be modified to capitalize the first letter
  54. void NormalizeString(string& s);
  55.  
  56. // Deletes the node containing the passed string.
  57. // Pre: NodePtr points to valid linked list and string points to a valid string
  58. // Post: Node containing the passed string has been removed from the list.
  59. NodePtr DeleteNode(NodePtr&, string);
  60.  
  61. int main()
  62. {
  63. NodePtr headPtr = NULL;
  64. NodePtr newPtr = NULL;
  65. string name;
  66. bool done = false;
  67.  
  68. do {
  69. newPtr = GetNode();
  70.  
  71. FillInfo(newPtr);
  72.  
  73. if (newPtr != nullptr){
  74. InsertNode(headPtr, newPtr);
  75.  
  76. done = false;
  77. }
  78. else
  79. done = true;
  80.  
  81. } while (!done);
  82.  
  83. PrintList(headPtr);
  84.  
  85. do {
  86. cout << "Enter a name to search (Enter to finish): ";
  87. getline(cin, name);
  88.  
  89. if (name != "") {
  90. NormalizeString(name);
  91. newPtr = SearchNode(headPtr, name);
  92.  
  93. if (newPtr != nullptr)
  94. cout << newPtr->classes.name << " found!" << endl;
  95. else
  96. cout << name << " not found!" << endl;
  97. }
  98.  
  99. } while (name != "");
  100.  
  101. do {
  102. cout << "Enter a name to delete (Enter to finish): ";
  103. getline(cin, name);
  104.  
  105. if (name != "") {
  106. NormalizeString(name);
  107. newPtr = DeleteNode(headPtr, name);
  108.  
  109. if (newPtr != nullptr){
  110. cout << name << " deleted!" << endl;
  111. delete newPtr;
  112. }
  113. else
  114. cout << name << " not found!" << endl;
  115. }
  116.  
  117. PrintList(headPtr);
  118.  
  119. } while (name != "");
  120.  
  121. return 0;
  122. }
  123.  
  124. NodePtr GetNode()
  125. {
  126. NodePtr newPtr = new NodeType;
  127. newPtr->next = nullptr;
  128.  
  129. return newPtr;
  130. }
  131.  
  132. void FillInfo(NodePtr& np)
  133. {
  134. np->classes.code = "";
  135.  
  136. cout << "Enter class name (Enter to finish): ";
  137. getline(cin, np->classes.name);
  138. NormalizeString(np->classes.name);
  139.  
  140. if (np->classes.name == ""){
  141. delete np;
  142. np = nullptr;
  143. }
  144. }
  145.  
  146. void InsertNode(NodePtr& head, NodePtr np)
  147. {
  148. NodePtr currPtr = head;
  149. NodePtr prevPtr = nullptr;
  150.  
  151. if (currPtr == nullptr){ // Empty list
  152. head = np;
  153. head->next = nullptr;
  154. return;
  155. } else {
  156. while ((currPtr != nullptr) &&
  157. (np->classes.name > currPtr->classes.name)){
  158. prevPtr = currPtr;
  159. currPtr = currPtr->next;
  160. }
  161. }
  162.  
  163. if (prevPtr == nullptr){ // First node
  164. np->next = head;
  165. head = np;
  166. } else if (currPtr == nullptr){ // Last node
  167. prevPtr->next = np;
  168. np->next = nullptr;
  169. } else { // Middle node
  170. np->next = currPtr;
  171. prevPtr->next = np;
  172. }
  173.  
  174. return;
  175.  
  176. }
  177.  
  178. NodePtr DeleteNode(NodePtr& headPtr, string name)
  179. {
  180. NodePtr np = headPtr;
  181. NodePtr prevPtr = nullptr;
  182.  
  183. while ((np != nullptr) && (np->classes.name != name)){
  184. prevPtr = np;
  185. np = np->next;
  186. }
  187.  
  188. if (np == nullptr) {
  189. cout << "Node not found!" << endl;
  190. return nullptr;
  191. } else if (prevPtr == nullptr){ // First node
  192. headPtr = headPtr->next;
  193. } else if (np->next == nullptr){ // Last node
  194. prevPtr->next = nullptr;
  195. } else if (np != nullptr) { // Middle node
  196. prevPtr->next = np->next;
  197. }
  198.  
  199. return np;
  200. }
  201.  
  202. void PrintList(NodePtr np)
  203. {
  204. while (np != NULL)
  205. {
  206. cout << np->classes.name << ", " << np->classes.code << endl;
  207. np = np->next;
  208. }
  209. }
  210.  
  211. NodePtr SearchNode(NodePtr listPtr, string name)
  212. {
  213. NodePtr currPtr = listPtr;
  214.  
  215. while ((currPtr != nullptr) &&
  216. (currPtr->classes.name != name)){
  217. // cout << currPtr->customer.lastName << endl;
  218. currPtr = currPtr->next;
  219. }
  220.  
  221. return currPtr;
  222. }
  223.  
  224. void NormalizeString(string& s)
  225. {
  226. s[0] = toupper(s[0]);
  227. for (int i = 1; i < s.length(); i++){
  228. s[i] = tolower(s[i]);
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement