Last active
August 29, 2015 14:20
-
-
Save joffilyfe/cb301de11141b645751a to your computer and use it in GitHub Desktop.
Linked list with header (first, last, count)
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
#include <stdio.h> | |
typedef struct node { | |
int val; | |
struct node *next; | |
} node_t; | |
typedef struct head { | |
node_t *first; | |
node_t *last; | |
int count; | |
} head_t; | |
//Funções | |
void start_list(head_t *head_list); | |
int show_list(head_t *head_list); | |
void add_node(head_t *linked_list, int value); | |
void count_elements(head_t *head_list); | |
void remove_last_node(head_t *head_list); | |
int main(void) { | |
//Alocando o cabeçalho | |
head_t *head_list = (head_t*) malloc(sizeof(head_t)); | |
start_list(head_list); | |
printf("Contagem de lista %d\n", head_list->count); | |
show_list(head_list); | |
add_node(head_list, 5); | |
add_node(head_list, 55); | |
add_node(head_list, 1); | |
show_list(head_list); | |
count_elements(head_list); | |
remove_last_node(head_list); | |
show_list(head_list); | |
return 0; | |
} | |
void start_list(head_t *head_list) { | |
head_list->first = NULL; | |
head_list->last = NULL; | |
head_list->count = 0; | |
} | |
int show_list(head_t *head_list) { | |
node_t *current = malloc(sizeof(node_t)); | |
current = head_list->first; | |
int i = 0; | |
if (head_list->first == NULL) { | |
printf("Lista vazia!\n"); | |
return 1; | |
} | |
while(current->next != NULL) { | |
printf("Node[%d] = %d\n", i, current->val); | |
current = current->next; | |
i++; | |
} | |
if (current->next == NULL) { | |
printf("Node[%d] = %d\n", i, current->val); | |
} | |
return 0; | |
} | |
void add_node(head_t *linked_list, int value) { | |
node_t *node = malloc(sizeof(node_t)); | |
node_t *new_node = malloc(sizeof(node_t)); | |
int node_address = 0; | |
if (node == NULL) { | |
printf("Out of memory, exiting.\n"); | |
} | |
//A lista está vazia? Se sim, adicionamos o primeiro nó. | |
if (linked_list->first == NULL) { | |
linked_list->first = node; | |
node->next = NULL; | |
node->val = value; | |
linked_list->count++; | |
linked_list->last = node; | |
} else { | |
//Se a lista não estiver vazia, o nó será o primeiro da lista. | |
node = linked_list->first; | |
while(node->next != NULL) { | |
node = node->next; | |
} | |
node->next = new_node; | |
new_node->next = NULL; | |
new_node->val = value; | |
linked_list->count++; | |
linked_list->last = new_node; | |
} | |
} | |
void count_elements(head_t *head_list) { | |
printf("Elementos na lista = %d\n", head_list->count); | |
} | |
void remove_last_node(head_t *head_list) { | |
node_t *current = malloc(sizeof(node_t)); | |
node_t *last_node = malloc(sizeof(node_t)); | |
current = head_list->first; | |
last_node = head_list->last; | |
while(current->next->next != NULL) { | |
current = current->next; | |
} | |
current->next = NULL; | |
free(last_node); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment