#include <stdio.h>
#include <malloc.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Linked_List {
Node* root;
} Linked_List;
Node* create_node(int number) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = number;
node->next = NULL;
return node;
}
Linked_List* create_list() {
Linked_List* list = (Linked_List*)malloc(sizeof(Linked_List));
list->root = NULL;
}
void add_first(Linked_List* list, Node* node) {
if(list->root == NULL) {
list->root = node;
} else {
Node* temp = list->root;
list->root = node;
list->root->next = temp;
}
}
void add_last(Linked_List* list, Node* node) {
if(list->root == NULL) {
list->root = node;
} else {
Node* iterator = list->root;
while(iterator->next != NULL) {
iterator = iterator->next;
}
iterator->next = node;
}
}
void print_list(Linked_List* list) {
Node* node = list->root;
while(node != NULL) {
printf("Number: %i\n", node->data);
node = node->next;
}
printf("\n");
}
Node* remove_and_retrieve_first(Linked_List *list) {
if(list->root == NULL) {
printf("Tried to return value from empty list, returned NULL");
return NULL;
} else {
Node* temp = list->root;
list->root = list->root->next;
return temp;
}
}
Node* remove_and_retrieve_last(Linked_List* list) {
if(list->root == NULL) {
printf("Tried to return value from empty list, returned NULL");
return NULL;
} else {
Node* node = list->root;
while(node->next->next != NULL) {
node = node->next;
}
Node* temp = node;
node->next = NULL;
return temp;
}
}
int main() {
Linked_List* list = create_list();
for(int i = 0; i <= 10000; i += 5)
add_last(list, create_node(i));
print_list(list);
return 0;
}
Comments