Created
December 1, 2021 03:30
-
-
Save mkhan45/b2cef15c40b555dd1aafd9f09ceeb51b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define MINERAL_LEN (50) | |
| #include <assert.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| typedef struct rock { | |
| char mineral[MINERAL_LEN]; | |
| float mass; | |
| struct rock* prev_node; | |
| struct rock* next_node; | |
| } rock_t; | |
| rock_t* create(char* mineral, float mass) { | |
| assert(mineral != NULL); | |
| rock_t* created = malloc(sizeof(rock_t)); | |
| assert(created != NULL); | |
| strncpy(created->mineral, mineral, MINERAL_LEN); | |
| created->mineral[MINERAL_LEN] = '\0'; | |
| created->mass = mass; | |
| created->next_node = NULL; | |
| created->prev_node = NULL; | |
| return created; | |
| } | |
| rock_t* swap_rocks(rock_t* fst_node, rock_t* snd_node) { | |
| assert(fst_node); | |
| assert(snd_node); | |
| rock_t* head = fst_node; | |
| while (head->prev_node) | |
| head = head->prev_node; | |
| char mineral_swap[MINERAL_LEN] = ""; | |
| strcpy(mineral_swap, fst_node->mineral); | |
| strcpy(fst_node->mineral, snd_node->mineral); | |
| strcpy(snd_node->mineral, mineral_swap); | |
| float mass_swap = fst_node->mass; | |
| fst_node->mass = snd_node->mass; | |
| snd_node->mass = mass_swap; | |
| return head; | |
| } | |
| rock_t* find_head(rock_t* node) { | |
| while (node->prev_node) | |
| node = node->prev_node; | |
| return node; | |
| } | |
| void print_list(rock_t* head) { | |
| if (head) { | |
| printf("Mineral: %s, Mass: %f\n", head->mineral, head->mass); | |
| print_list(head->next_node); | |
| } | |
| } | |
| rock_t* append(rock_t* head, char* mineral, float mass) { | |
| rock_t* node = create(mineral, mass); | |
| head->next_node = node; | |
| node->prev_node = head; | |
| return node; | |
| } | |
| int main() { | |
| rock_t* node_1 = create("Min 1", 1.0); | |
| rock_t* node_2 = append(node_1, "Min 2", 2.0); | |
| rock_t* node_3 = append(node_2, "Min 3", 3.0); | |
| rock_t* node_4 = append(node_3, "Min 4", 4.0); | |
| rock_t* node_5 = append(node_4, "Min 5", 5.0); | |
| printf("-----------\n"); | |
| print_list(node_1); | |
| printf("-----------\n"); | |
| rock_t* head = swap_rocks(node_2, node_4); | |
| printf("-----------\n"); | |
| print_list(head); | |
| printf("-----------\n"); | |
| head = swap_rocks(node_3, node_5); | |
| printf("-----------\n"); | |
| print_list(head); | |
| printf("-----------\n"); | |
| head = swap_rocks(node_1, node_5); | |
| printf("-----------\n"); | |
| print_list(head); | |
| printf("-----------\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ----------- | |
| Mineral: Min 1, Mass: 1.000000 | |
| Mineral: Min 2, Mass: 2.000000 | |
| Mineral: Min 3, Mass: 3.000000 | |
| Mineral: Min 4, Mass: 4.000000 | |
| Mineral: Min 5, Mass: 5.000000 | |
| ----------- | |
| ----------- | |
| Mineral: Min 1, Mass: 1.000000 | |
| Mineral: Min 4, Mass: 4.000000 | |
| Mineral: Min 3, Mass: 3.000000 | |
| Mineral: Min 2, Mass: 2.000000 | |
| Mineral: Min 5, Mass: 5.000000 | |
| ----------- | |
| ----------- | |
| Mineral: Min 1, Mass: 1.000000 | |
| Mineral: Min 4, Mass: 4.000000 | |
| Mineral: Min 5, Mass: 5.000000 | |
| Mineral: Min 2, Mass: 2.000000 | |
| Mineral: Min 3, Mass: 3.000000 | |
| ----------- | |
| ----------- | |
| Mineral: Min 3, Mass: 3.000000 | |
| Mineral: Min 4, Mass: 4.000000 | |
| Mineral: Min 5, Mass: 5.000000 | |
| Mineral: Min 2, Mass: 2.000000 | |
| Mineral: Min 1, Mass: 1.000000 | |
| ----------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment