Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * singly_linked_list.c
- *
- * Created on: Oct 16, 2019
- * Author: Bojan Rikic RA134/2016
- */
- #include "singly_linked_list.h"
- Node* create_node(int_least16_t new_value)
- {
- Node* current_node = malloc(sizeof(Node));
- if (!current_node)
- {
- printf("No memory !\n");
- return (Node*) NULL;
- }
- current_node->value = new_value;
- current_node->next = (Node*) NULL;
- return current_node;
- }
- List* make_list(void)
- {
- List* initalize_singly_linked_list = malloc(sizeof(List));
- if (!initalize_singly_linked_list)
- {
- printf("No memory !\n");
- return (List*) NULL;
- }
- initalize_singly_linked_list->head = (Node*) NULL;
- return initalize_singly_linked_list;
- }
- void push_begin(List* singly_linked_list, int_least16_t n)
- {
- assert(n < CHAR_MAX);
- assert(n > CHAR_MIN);
- assert(n < SHRT_MAX);
- Node* new_node_for_push_to_begin = (Node*) malloc(sizeof(Node));
- new_node_for_push_to_begin->value = n;
- new_node_for_push_to_begin->next = singly_linked_list->head;
- singly_linked_list->head = new_node_for_push_to_begin;
- }
- void insert_in_list(const List* singly_linked_list, const Node *position,int_least16_t n)
- {
- assert(n < CHAR_MAX);
- assert(n > CHAR_MIN);
- assert(n < SHRT_MAX);
- Node* new_node = (Node*) NULL;
- Node* tmp_node = (Node*) NULL;
- if (position == (Node*) NULL)
- {
- printf("No memory !\n");
- return;
- }
- new_node = create_node(n);
- tmp_node = singly_linked_list->head;
- while(tmp_node->next != position->next)
- {
- tmp_node = tmp_node->next;
- }
- new_node->next = tmp_node->next;
- tmp_node->next = new_node;
- }
- void push_end(List* singly_linked_list, int_least16_t n)
- {
- assert(n < CHAR_MAX);
- assert(n > CHAR_MIN);
- assert(n < SHRT_MAX);
- Node* new_node_for_push_to_end = (Node*) NULL;
- if(singly_linked_list->head == (Node*) NULL)
- {
- singly_linked_list->head = create_node(n);
- }
- else
- {
- new_node_for_push_to_end = singly_linked_list->head;
- while (new_node_for_push_to_end->next != (Node*) NULL)
- {
- new_node_for_push_to_end = new_node_for_push_to_end->next;
- }
- new_node_for_push_to_end->next = create_node(n);
- }
- }
- void print_list(const List* singly_linked_list)
- {
- Node* current_node_for_print = singly_linked_list->head;
- if(singly_linked_list->head == (Node*) NULL)
- {
- printf("List is empty!\n");
- return;
- }
- printf("List --> ");
- while(current_node_for_print != NULL)
- {
- printf("%"PRIdLEAST16",", current_node_for_print->value);
- current_node_for_print = current_node_for_print->next;
- }
- printf("\n");
- }
- int_least8_t pop_last(List* singly_linked_list){
- int_least8_t last_element;
- Node* toDelete;
- Node* secondLastNode;
- if(singly_linked_list->head == (Node*) NULL)
- {
- return (int_least8_t) 0;
- }
- else
- {
- toDelete = singly_linked_list->head;
- secondLastNode = singly_linked_list->head;
- while(toDelete->next != (Node*) NULL)
- {
- secondLastNode = toDelete;
- toDelete = toDelete->next;
- }
- if(toDelete == singly_linked_list->head)
- {
- singly_linked_list->head = (Node*) NULL;
- }
- else
- {
- secondLastNode->next = (Node*) NULL;
- }
- last_element = toDelete->value;
- free(toDelete);
- }
- return last_element;
- }
- void clear_list(List* singly_linked_list)
- {
- Node* current_node_for_clear_list;
- while(singly_linked_list->head != (Node*) NULL)
- {
- current_node_for_clear_list = singly_linked_list->head;
- singly_linked_list->head = current_node_for_clear_list->next;
- free(current_node_for_clear_list);
- }
- }
- void split_list(List* singly_linked_list, List* list_with_even_index, List* list_with_odd_index)
- {
- uint_least8_t i = (uint_least8_t) 0U;
- Node* node_odd = singly_linked_list->head;
- Node* temp;
- Node* node_odd_temp = list_with_odd_index->head;
- Node* node_even_temp = list_with_even_index->head;
- while(node_odd != (Node*) NULL)
- {
- temp = create_node(node_odd->value);
- i++;
- if((i % (uint_least8_t) 2U) == (uint_least8_t) 1U)
- {
- node_odd_temp = temp;
- push_end(list_with_odd_index, node_odd_temp->value);
- }
- else
- {
- node_even_temp = temp;
- push_end(list_with_even_index, node_even_temp->value);
- }
- node_odd = node_odd->next;
- }
- clear_list(singly_linked_list);
- node_odd_temp->next = (Node*) NULL;
- node_even_temp->next = (Node*) NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement