Záleži od požiadaviek, každá má svoje výhody a nevýhody
INFO: Použi počítač...
int pole[`10] = {1,2,3,4,5,6,7,8,9,10};
int stvrty = pole[`3];
for (int i = 0; i < 10; i++){
printf("%d\n",pole[`i]);
}
int* pole = malloc(sizeof(int) * 10);
for (int i = 0; i < 10; i++){
pole[i] = i;
}
int stvrty = pole[3];
free(pole);
Lineárna zložitosť je dosť zlá
for (c = n - 1; c >= position - 1; c--)
array[`c+1] = array[`c];
array[`position-1] = value;
https://www.programmingsimplified.com/c/source-code/c-program-insert-element-in-array
Jazyk C, ani štandardná knižnica nepoznajú spojkový zoznam.
- Musíme si ho naprogramovať
struct node {
int value;
struct node* next;
};
struct node* create_node(int value){
struct node* linked_list = malloc(sizeof(struct node));
linked_list->value = 1;
linked_list->next = NULL;
return linked_list;
}
(struct node*)malloc(sizeof(struct node));
sizeof(struct node*) +------------+
| 'a' | NULL |
+------------+
#aabb
+-------------+ +-------------+ +------------+
| 'a' | #aa01 | --> | 'a' | #aabb | --> | 'a' | NULL |
+-------------+ +-------------+ +------------+
#0a01 #aa01 #aabb
struct node* linked_list = create_node(1);
struct node* next_node = create_node(2);
linked_list->next = next_node;
free(next_node);
free(linked_list)
struct node* add(struct node* list,int value) {
struct node* item = create_node(value);
item->next = list;
return item;
}
struct node* add_end(struct node* list,int value) {
struct node* item = create_node(value);
if (list == NULL){
return item;
}
struct node* first = list;
while (list->next != NULL){
list = list->next;
}
list->next = item;
return first;
}
for(struct node* this = linked_list;this != NULL;this = this->next){
if (this->value == 'a'){
printf("Nasiel som a");
}
}
void delete(struct node* list){
if (list!=NULL){
delete(list->next);
free(list);
}
}
https://stackoverflow.com/questions/3770457/what-is-memory-fragmentation
Spojkový zoznam nie je dobrý ak:
Spojkový zoznam je dobrý ak: