Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define MAXLEN 40
- #define MAXNEIGHBOURS 10
- int i;
- struct country {
- char *name;
- int citizens;
- struct country *neighbours[MAXNEIGHBOURS];
- struct country *next;
- };
- void *addCountry(struct country **head, char *name, int *citizens) {
- struct country *curr = (*head);
- // create new node
- struct country *newcountry = (struct country*)malloc(sizeof(struct country));
- newcountry->name = (char *)malloc(strlen(name) + 1);
- strcpy(newcountry->name, name);
- newcountry->citizens = citizens;
- for (i = 0; i < MAXNEIGHBOURS; i++) {
- newcountry->neighbours[i] == NULL;
- }
- if (curr != NULL) {
- do {
- if (strcmp(curr->name, name) == 0) {
- printf("Country already exists\n");
- return;
- }
- curr = curr->next;
- } while (curr != *head);
- }
- curr = *head;
- if (curr == NULL) {
- // if list is still empty, loop back to self
- newcountry->next = newcountry;
- *head = newcountry;
- } else if (strcmp(curr->name, newcountry->name) > 0) {
- // if list needs to be inserted at the last pos
- while (curr->next != *head) {
- // look for last item before headref
- curr = curr->next;
- }
- // insert new country and move pointer to head to the new head
- curr->next = newcountry;
- newcountry->next = *head;
- *head = newcountry;
- } else {
- // for regular inserts
- while (curr->next != *head && (strcmp(curr->next->name, newcountry->name) < 0)) {
- // look for node after which the new node needs to be placed
- curr = curr->next;
- }
- newcountry->next = curr->next;
- curr->next = newcountry;
- }
- return;
- };
- void printList(struct country **head) {
- struct country *temp = (*head);
- float tempcit;
- if (temp!= NULL) {
- do {
- if (temp->citizens > 1000000) {
- tempcit = (float) temp->citizens / 1000000;
- } else {
- tempcit = (float) temp->citizens / 1000;
- }
- printf("name: %s, citizens: %0.1f", temp->name, tempcit);
- for (i = 0; i < MAXNEIGHBOURS; i++) {
- if (temp->neighbours[i] != NULL) {
- printf(" -- neighbour %d: %s", i, temp->neighbours[i]->name);
- }
- }
- printf("\n");
- temp = temp->next;
- } while (temp != *head);
- } else {
- printf("List is empty\n");
- }
- return;
- }
- struct country *findCountry(struct country *search, char *name) {
- struct country *head = search;
- struct country *temp = search;
- if (search == NULL) {
- return NULL;
- }
- do {
- if (strcmp(temp->name, name) == 0) {
- return temp;
- }
- temp = temp->next;
- } while (temp != head);
- return NULL;
- }
- void addNeighbour(struct country *countries, char *n1name, char *n2name) {
- struct country *temp = countries;
- struct country *n1 = findCountry(countries, n1name);
- struct country *n2 = findCountry(countries, n2name);
- // check if both countries exist
- if (n1 == NULL) {
- printf("%s does not exist\n", n1name);
- return;
- } else if (n2 == NULL) {
- printf("%s does not exist\n", n2name);
- return;
- }
- for (i = 0; i < MAXNEIGHBOURS; i++) {
- // check if the neighbour isnt already in list
- if (n1->neighbours[i] == NULL) {
- // when finding an empty spot, add n2 to n1 neighbours and do same other way around
- n1->neighbours[i] = n2;
- for (i = 0; i < MAXNEIGHBOURS; i++) {
- if (n2->neighbours[i] == NULL) {
- n2->neighbours[i] = n1;
- printf("%s\n", n1->neighbours[0]->name);
- return;
- }
- }
- }
- }
- }
- void removeCountry(struct country **head, char *name) {
- struct country *curr = (*remove);
- struct country *prev = (*remove);
- struct country *remove = findCountry((*head), name);
- if (remove == NULL) {
- printf("Country doesn't exist\n");
- return;
- }
- if (curr == NULL) {
- // if list is empty
- printf("list empty\n");
- } else if (strcmp((*head)->name, name) == 0) {
- // if node to be removed is head
- // look for last node
- while (curr->next != (*head)) {
- curr = curr->next;
- }
- (*head) = (*head)->next;
- curr->next = remove->next;
- free(remove->name);
- free(remove);
- } else {
- while (1) {
- }
- }
- }
- int main() {
- char cmd;
- char secondcmd[MAXLEN];
- char thirdcmd[MAXLEN];
- int intcmd = 123;
- float tempcit;
- struct country *countries = NULL;
- struct country *searchCountry = (struct country*)malloc(sizeof(struct country));
- while (1) {
- printf("Command? ");
- scanf(" %c", &cmd);
- switch(cmd) {
- case 'a':
- printf("name and number of citizens? ");
- scanf(" %s %d", secondcmd, &intcmd);
- addCountry(&countries, secondcmd, intcmd);
- break;
- case 'p':
- printList(&countries);
- break;
- case 'q':
- printf("Bye!");
- return;
- break;
- case 'f':
- printf("Name? ");
- scanf(" %s", secondcmd);
- searchCountry = findCountry(countries, secondcmd);
- if (searchCountry != NULL) {
- if (searchCountry->citizens > 1000000) {
- tempcit = (float) searchCountry->citizens / 1000000;
- } else {
- tempcit = (float) searchCountry->citizens / 1000;
- }
- printf("name: %s, citizens: %0.1f\n", searchCountry->name, tempcit);
- } else {
- printf("Country not found\n");
- }
- break;
- case 'n':
- printf("Name of two countries? ");
- scanf(" %s %s", secondcmd, thirdcmd);
- addNeighbour(countries, secondcmd, thirdcmd);
- break;
- default:
- printf("invalid command: %c\n", cmd);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement