Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define MAXLEN 40
  6. #define MAXNEIGHBOURS 10
  7.  
  8. int i;
  9.  
  10. struct country {
  11. char *name;
  12. int citizens;
  13. struct country *neighbours[MAXNEIGHBOURS];
  14. struct country *next;
  15. };
  16.  
  17. void *addCountry(struct country **head, char *name, int *citizens) {
  18. struct country *curr = (*head);
  19.  
  20. // create new node
  21. struct country *newcountry = (struct country*)malloc(sizeof(struct country));
  22. newcountry->name = (char *)malloc(strlen(name) + 1);
  23. strcpy(newcountry->name, name);
  24. newcountry->citizens = citizens;
  25. for (i = 0; i < MAXNEIGHBOURS; i++) {
  26. newcountry->neighbours[i] == NULL;
  27. }
  28.  
  29. if (curr != NULL) {
  30. do {
  31. if (strcmp(curr->name, name) == 0) {
  32. printf("Country already exists\n");
  33. return;
  34. }
  35. curr = curr->next;
  36. } while (curr != *head);
  37. }
  38. curr = *head;
  39.  
  40. if (curr == NULL) {
  41. // if list is still empty, loop back to self
  42. newcountry->next = newcountry;
  43. *head = newcountry;
  44. } else if (strcmp(curr->name, newcountry->name) > 0) {
  45. // if list needs to be inserted at the last pos
  46. while (curr->next != *head) {
  47. // look for last item before headref
  48. curr = curr->next;
  49. }
  50. // insert new country and move pointer to head to the new head
  51. curr->next = newcountry;
  52. newcountry->next = *head;
  53. *head = newcountry;
  54. } else {
  55. // for regular inserts
  56. while (curr->next != *head && (strcmp(curr->next->name, newcountry->name) < 0)) {
  57. // look for node after which the new node needs to be placed
  58. curr = curr->next;
  59. }
  60. newcountry->next = curr->next;
  61. curr->next = newcountry;
  62. }
  63. return;
  64. };
  65.  
  66. void printList(struct country **head) {
  67. struct country *temp = (*head);
  68. float tempcit;
  69.  
  70. if (temp!= NULL) {
  71. do {
  72. if (temp->citizens > 1000000) {
  73. tempcit = (float) temp->citizens / 1000000;
  74. } else {
  75. tempcit = (float) temp->citizens / 1000;
  76. }
  77. printf("name: %s, citizens: %0.1f", temp->name, tempcit);
  78.  
  79. for (i = 0; i < MAXNEIGHBOURS; i++) {
  80. if (temp->neighbours[i] != NULL) {
  81. printf(" -- neighbour %d: %s", i, temp->neighbours[i]->name);
  82. }
  83. }
  84.  
  85. printf("\n");
  86. temp = temp->next;
  87. } while (temp != *head);
  88.  
  89. } else {
  90. printf("List is empty\n");
  91. }
  92.  
  93. return;
  94. }
  95.  
  96. struct country *findCountry(struct country *search, char *name) {
  97. struct country *head = search;
  98. struct country *temp = search;
  99.  
  100. if (search == NULL) {
  101. return NULL;
  102. }
  103.  
  104. do {
  105. if (strcmp(temp->name, name) == 0) {
  106. return temp;
  107. }
  108. temp = temp->next;
  109. } while (temp != head);
  110.  
  111. return NULL;
  112. }
  113.  
  114. void addNeighbour(struct country *countries, char *n1name, char *n2name) {
  115. struct country *temp = countries;
  116. struct country *n1 = findCountry(countries, n1name);
  117. struct country *n2 = findCountry(countries, n2name);
  118.  
  119. // check if both countries exist
  120. if (n1 == NULL) {
  121. printf("%s does not exist\n", n1name);
  122. return;
  123. } else if (n2 == NULL) {
  124. printf("%s does not exist\n", n2name);
  125. return;
  126. }
  127.  
  128.  
  129. for (i = 0; i < MAXNEIGHBOURS; i++) {
  130. // check if the neighbour isnt already in list
  131. if (n1->neighbours[i] == NULL) {
  132. // when finding an empty spot, add n2 to n1 neighbours and do same other way around
  133. n1->neighbours[i] = n2;
  134. for (i = 0; i < MAXNEIGHBOURS; i++) {
  135. if (n2->neighbours[i] == NULL) {
  136. n2->neighbours[i] = n1;
  137. printf("%s\n", n1->neighbours[0]->name);
  138. return;
  139. }
  140. }
  141. }
  142. }
  143. }
  144.  
  145. void removeCountry(struct country **head, char *name) {
  146. struct country *curr = (*remove);
  147. struct country *prev = (*remove);
  148. struct country *remove = findCountry((*head), name);
  149.  
  150. if (remove == NULL) {
  151. printf("Country doesn't exist\n");
  152. return;
  153. }
  154.  
  155. if (curr == NULL) {
  156. // if list is empty
  157. printf("list empty\n");
  158. } else if (strcmp((*head)->name, name) == 0) {
  159. // if node to be removed is head
  160. // look for last node
  161. while (curr->next != (*head)) {
  162. curr = curr->next;
  163. }
  164. (*head) = (*head)->next;
  165. curr->next = remove->next;
  166. free(remove->name);
  167. free(remove);
  168. } else {
  169. while (1) {
  170.  
  171. }
  172. }
  173. }
  174.  
  175. int main() {
  176. char cmd;
  177. char secondcmd[MAXLEN];
  178. char thirdcmd[MAXLEN];
  179. int intcmd = 123;
  180. float tempcit;
  181.  
  182. struct country *countries = NULL;
  183. struct country *searchCountry = (struct country*)malloc(sizeof(struct country));
  184.  
  185. while (1) {
  186. printf("Command? ");
  187. scanf(" %c", &cmd);
  188.  
  189. switch(cmd) {
  190. case 'a':
  191. printf("name and number of citizens? ");
  192. scanf(" %s %d", secondcmd, &intcmd);
  193. addCountry(&countries, secondcmd, intcmd);
  194. break;
  195. case 'p':
  196. printList(&countries);
  197. break;
  198. case 'q':
  199. printf("Bye!");
  200. return;
  201. break;
  202. case 'f':
  203. printf("Name? ");
  204. scanf(" %s", secondcmd);
  205. searchCountry = findCountry(countries, secondcmd);
  206. if (searchCountry != NULL) {
  207. if (searchCountry->citizens > 1000000) {
  208. tempcit = (float) searchCountry->citizens / 1000000;
  209. } else {
  210. tempcit = (float) searchCountry->citizens / 1000;
  211. }
  212. printf("name: %s, citizens: %0.1f\n", searchCountry->name, tempcit);
  213. } else {
  214. printf("Country not found\n");
  215. }
  216. break;
  217. case 'n':
  218. printf("Name of two countries? ");
  219. scanf(" %s %s", secondcmd, thirdcmd);
  220. addNeighbour(countries, secondcmd, thirdcmd);
  221. break;
  222. default:
  223. printf("invalid command: %c\n", cmd);
  224. break;
  225. }
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement