Diferență între revizuiri ale paginii „List.c”
De la WikiLabs
Jump to navigationJump to search (Pagină nouă: <syntaxhighlight lang="c"> →* * This is the header file for a list implementation: #ifndef __LIST_H__ #define __LIST_H__ #define UNLIMITED_SIZE -1 struct simply_linked_node...) |
|||
Linia 1: | Linia 1: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
+ | #include "list.h" | ||
+ | #include <stdlib.h> | ||
− | |||
− | |||
− | |||
− | + | struct simply_linked_node * create_simply_linked_node(int data){ | |
− | + | struct simply_linked_node * new_node = | |
+ | (struct simply_linked_node*)malloc(sizeof(struct simply_linked_list)); | ||
+ | |||
+ | new_node->data = data; | ||
+ | new_node->next = NULL; | ||
+ | |||
+ | return new_node; | ||
+ | } | ||
− | + | struct simply_linked_list * create_simply_linked_list(int max_size){ | |
+ | struct simply_linked_list * new_list = | ||
+ | (struct simply_linked_list*)malloc(sizeof(struct simply_linked_list)); | ||
+ | |||
+ | new_list->first = NULL; | ||
+ | new_list->last = NULL; | ||
+ | new_list->size = 0; | ||
+ | new_list->max_size = max_size; | ||
+ | |||
+ | return new_list; | ||
+ | } | ||
+ | |||
+ | int is_full(struct simply_linked_list * list){ | ||
+ | return list->max_size != UNLIMITED_SIZE && list->size == list->max_size; | ||
+ | } | ||
− | struct simply_linked_node{ | + | void add_node_to_end_of_list(struct simply_linked_node * node, struct simply_linked_list * list){ |
− | + | if(is_full(list)){ | |
− | + | return; | |
− | }; | + | } |
− | + | ||
− | struct simply_linked_node * | + | node->next = NULL; |
− | + | if(!list->size){ | |
− | struct simply_linked_list{ | + | list->first = node; |
− | + | list->last = node; | |
− | + | }else{ | |
− | + | list->last->next = node; | |
− | + | list->last = node; | |
− | } | + | } |
− | + | ||
− | struct simply_linked_list * | + | list->size++; |
− | + | } | |
− | + | ||
− | + | struct simply_linked_node * get_node(struct simply_linked_list * list, unsigned position){ | |
− | + | struct simply_linked_node * node = list->first; | |
− | + | while(node != NULL && position){ | |
− | + | node = node->next; | |
+ | position--; | ||
+ | } | ||
+ | return node; | ||
+ | } | ||
+ | |||
+ | void add_node_to_list(struct simply_linked_node * new_node, struct simply_linked_list * list, unsigned position){ | ||
+ | if(is_full(list)){ | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | if(!position){ | ||
+ | new_node->next = list->first; | ||
+ | list->first = new_node; | ||
+ | if(!list->size){ | ||
+ | list->last = list->first; | ||
+ | } | ||
+ | }else{ | ||
+ | struct simply_linked_node * node = get_node(list, position - 1); | ||
+ | new_node->next = node->next; | ||
+ | node->next = new_node; | ||
+ | } | ||
+ | |||
+ | list->size++; | ||
+ | } | ||
− | + | void delete_node(struct simply_linked_list * list, unsigned position){ | |
+ | struct simply_linked_node * old_node; | ||
− | + | if(!list->size){ | |
− | + | return; | |
− | + | } | |
+ | |||
+ | |||
+ | if(position == 0){ | ||
+ | if(list->size == 1){ | ||
+ | free(list->first); | ||
+ | list->first = NULL; | ||
+ | list->last = NULL; | ||
+ | list->size = 0; | ||
+ | }else{ | ||
+ | old_node = list->first; | ||
+ | list->first = old_node->next; | ||
+ | free(old_node); | ||
+ | list->size--; | ||
+ | } | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | |||
+ | struct simply_linked_node * node = get_node(list, position - 1); | ||
+ | |||
+ | if(node == NULL){ | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | old_node = node->next; | ||
+ | node->next = old_node->next; | ||
+ | |||
+ | if(old_node == list->last){ | ||
+ | list->last = node; | ||
+ | } | ||
+ | list->size--; | ||
+ | |||
+ | free(old_node); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Versiunea curentă din 3 aprilie 2014 09:02
#include "list.h"
#include <stdlib.h>
struct simply_linked_node * create_simply_linked_node(int data){
struct simply_linked_node * new_node =
(struct simply_linked_node*)malloc(sizeof(struct simply_linked_list));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
struct simply_linked_list * create_simply_linked_list(int max_size){
struct simply_linked_list * new_list =
(struct simply_linked_list*)malloc(sizeof(struct simply_linked_list));
new_list->first = NULL;
new_list->last = NULL;
new_list->size = 0;
new_list->max_size = max_size;
return new_list;
}
int is_full(struct simply_linked_list * list){
return list->max_size != UNLIMITED_SIZE && list->size == list->max_size;
}
void add_node_to_end_of_list(struct simply_linked_node * node, struct simply_linked_list * list){
if(is_full(list)){
return;
}
node->next = NULL;
if(!list->size){
list->first = node;
list->last = node;
}else{
list->last->next = node;
list->last = node;
}
list->size++;
}
struct simply_linked_node * get_node(struct simply_linked_list * list, unsigned position){
struct simply_linked_node * node = list->first;
while(node != NULL && position){
node = node->next;
position--;
}
return node;
}
void add_node_to_list(struct simply_linked_node * new_node, struct simply_linked_list * list, unsigned position){
if(is_full(list)){
return;
}
if(!position){
new_node->next = list->first;
list->first = new_node;
if(!list->size){
list->last = list->first;
}
}else{
struct simply_linked_node * node = get_node(list, position - 1);
new_node->next = node->next;
node->next = new_node;
}
list->size++;
}
void delete_node(struct simply_linked_list * list, unsigned position){
struct simply_linked_node * old_node;
if(!list->size){
return;
}
if(position == 0){
if(list->size == 1){
free(list->first);
list->first = NULL;
list->last = NULL;
list->size = 0;
}else{
old_node = list->first;
list->first = old_node->next;
free(old_node);
list->size--;
}
return;
}
struct simply_linked_node * node = get_node(list, position - 1);
if(node == NULL){
return;
}
old_node = node->next;
node->next = old_node->next;
if(old_node == list->last){
list->last = node;
}
list->size--;
free(old_node);
}