Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct
- {
- const char *first_name, *last_name;
- float height, weight;
- int age;
- } Human;
- struct Node;
- typedef struct Node Node;
- struct Node
- {
- Node* next;
- Human head;
- };
- typedef struct
- {
- Node* root;
- size_t size; // unused
- } LinkedList;
- LinkedList create_list(void) {
- LinkedList list;
- list.root = NULL;
- list.size = 0;
- return list;
- }
- void list_push(LinkedList* list, Human head) {
- if(list->root == NULL) {
- list->root = (Node*) malloc(sizeof(Node));
- list->root->head = head;
- list->root->next = NULL;
- } else {
- Node *node = list->root;
- while(node->next != NULL) {
- node = node->next;
- }
- node->next = (Node*) malloc(sizeof(Node));
- node->next->head = head;
- node->next->next = NULL;
- }
- list->size++;
- }
- Human list_access(LinkedList* list, int index) {
- Node *node = list->root;
- for(int i = 0; i != index; i++, node = node->next);
- return node->head;
- }
- int main()
- {
- LinkedList list = create_list();
- Human bob;
- bob.first_name = "Bob";
- bob.last_name = "Martin";
- bob.height = 1.65f;
- bob.weight = 140.0f;
- bob.age = 74;
- Human betty;
- betty.first_name = "Betty";
- betty.last_name = "Martin";
- betty.height = 1.55f;
- betty.weight = 120.0f;
- betty.age = 42;
- list_push(&list, bob);
- list_push(&list, betty);
- list_push(&list, bob);
- list_push(&list, bob);
- list_push(&list, betty);
- printf("%d\n", list.size);
- for(int i = 0; i < list.size; i++) {
- Human human = list_access(&list, i);
- printf("name:\t%s %s\n", human.first_name, human.last_name);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement