Guest User

Untitled

a guest
Mar 6th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #include "list.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. Rec* first = NULL;
  6. Rec* last = NULL;
  7. Rec* current;
  8.  
  9. bool dupeID(Rec r)
  10. {
  11. current = first;
  12. while (current != NULL)
  13. {
  14. if (strcmp(r.id, current->id) == 0)
  15. {
  16. return true;
  17. }
  18. current = current->next;
  19. }
  20. return false;
  21. }
  22.  
  23. bool checkExists(char a[15])// checks to see if the id exists
  24. {
  25. current = first;
  26. while (current != NULL)
  27. {
  28. if (strcmp(a, current->id) == 0)
  29. {
  30. return false;
  31. }
  32. current = current->next;
  33. }
  34. return true;
  35. }
  36.  
  37.  
  38. int AddItem(Rec r)
  39. {
  40. Rec* current = new Rec; // creates new Rec
  41. Rec* firstCopy = first;
  42. bool dupe = dupeID(r);
  43. if (dupe) // checks to see if the id is a duplicate
  44. {
  45. cout << "You've entered a duplicate ID. Please re-enter the record with a valid ID." << endl;
  46. return 0;
  47. }
  48.  
  49. if (first == NULL) {
  50. current->id = r.id;
  51. strcpy_s(current->firstname,15, r.firstname);
  52. strcpy_s(current->lastname,15, r.lastname);
  53. current->next = NULL;
  54. current->previous = NULL;
  55. first = current;
  56. last = current;
  57. return 1;
  58. }
  59. else {
  60.  
  61. if (((string)last->lastname).compare(r.lastname) < 0) {
  62. current->id = r.id;
  63. strcpy_s(current->firstname, 15, r.firstname);
  64. strcpy_s(current->lastname, 15, r.lastname);
  65. current->previous = last;
  66. current->next = NULL;
  67. last->next = current;
  68. last = current;
  69. return 1;
  70. }
  71. else if (((string)first->lastname).compare(r.firstname) > 0) {
  72. current->id = r.id;
  73. strcpy_s(current->firstname, 15, r.firstname);
  74. strcpy_s(current->lastname, 15, r.lastname);
  75. current->previous = NULL;
  76. current->next = first;
  77. first->previous = current;
  78. first = current;
  79. return 1;
  80. }
  81. else {
  82. bool check = true;
  83. while (check == true) {
  84. if ((strcmp((char*)first->next, r.lastname) > 0)) {
  85. strcpy_s(current->lastname, 15, r.lastname);
  86. current->id = r.id;
  87. current->previous = firstCopy;
  88. current->next = firstCopy->next;
  89. firstCopy->next = current;
  90. check = false;
  91. return 1;
  92. }
  93. firstCopy = firstCopy->next;
  94. }
  95. }
  96. }
  97. return 0;
  98.  
  99. }
  100. int DeleteItem(char* delid) { // deletes the item
  101.  
  102. bool idExists = checkExists(delid);
  103. if (idExists)
  104. {
  105. cout << "The ID you entered does not exist in the list. Please try again with a valid ID." << endl;
  106. return 0;
  107. }
  108. Rec* RecToDelete = first;
  109. if (strcmp(RecToDelete->id, delid) == 0) // if the Rec is located in the front delete like this
  110. {
  111. if (RecToDelete->next == NULL && RecToDelete->previous == NULL) // if theres only 1 Rec in the list
  112. {
  113. first = NULL; // this needs to be Null to keep a refrence to the front
  114. return 0;
  115. }
  116. delete RecToDelete->id; // deletes the id first
  117. RecToDelete->next->previous = NULL; // set addresses to null... cant delete and address
  118. first = RecToDelete->next;
  119. delete RecToDelete; // deletes everything
  120. return 1;
  121. }
  122. else
  123. {
  124. Rec* RecAfterRecToDelete = RecToDelete->next; // gets the next record
  125. if (RecToDelete == NULL || RecAfterRecToDelete == NULL) // tests to see if either are NULL
  126. {
  127. return 0;
  128. }
  129. while (strcmp(RecAfterRecToDelete->id, delid) > 0 || strcmp(RecAfterRecToDelete->id, delid) < 0) // loops to search for the delid
  130. {
  131. RecToDelete = RecToDelete->next; // moves to the next Rec
  132. RecAfterRecToDelete = RecAfterRecToDelete->next; // moves to the next Rec
  133. if (RecToDelete == NULL || RecAfterRecToDelete == NULL) // tests to see if either are NULL
  134. {
  135. return 0;
  136. }
  137. }
  138. RecToDelete->next = RecAfterRecToDelete->next; // moves to the next spot
  139. if (RecAfterRecToDelete->next != NULL) // if id wasnt found
  140. {
  141. RecAfterRecToDelete->next->previous = RecToDelete;
  142. }
  143. }
  144.  
  145.  
  146. }
  147. void PrintList(int order) {
  148. Rec* ptr;
  149. if (order == 0) {
  150. ptr = first;
  151. if (ptr == NULL) {
  152. cout << "list is empty" << endl;
  153. }
  154. while (ptr != NULL) {
  155. cout << ptr->id << " ";
  156. cout << ptr->firstname << " ";
  157. cout << ptr->lastname << " ";
  158. cout << ptr->previous << " " << endl; // address of previous item
  159. cout << ptr << " "; // address of item
  160. cout << ptr->next << " " << endl; // address of next item
  161. cout.flush();
  162. ptr = ptr->next;
  163. }
  164. }
  165. else if (order == 1) {
  166. {
  167.  
  168. ptr = first;
  169. while (ptr != NULL) // this gets the last Rec and traverses backwards
  170. {
  171. last = ptr;
  172. ptr = ptr->next;
  173. }
  174. ptr = last;
  175. while (ptr != NULL) {
  176. cout << ptr->id << " ";
  177. cout << ptr->firstname << " ";
  178. cout << ptr->lastname << " ";
  179. cout << ptr->previous << " " << endl; // address of previous item
  180. cout << ptr << " "; // address of item
  181. cout << ptr->next << " " << endl; // address of next item
  182. if (ptr->previous == NULL)
  183. {
  184. break;
  185. }
  186. ptr = ptr->previous;
  187. }
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment