#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int value;
struct node* next;
struct node* prev;
};
struct linked_list
{
size_t size;
struct node* tail;
struct node* head;
};
static struct linked_list* linked_list_new(void)
{
struct linked_list* list = malloc(sizeof * list);
list->size = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
static void linked_list_add(struct linked_list* list,
int value)
{
struct node* new_node = malloc(sizeof * new_node);
struct node* list_tail = list->tail;
new_node->prev = list_tail;
new_node->next = NULL;
new_node->value = value;
if (list_tail) {
list_tail->next = new_node;
}
else {
list->head = new_node;
}
list->tail = new_node;
list->size++;
}
static void linked_list_free(struct linked_list* list)
{
struct node* node = list->head;
if (list->size > 0) {
while (node->next) {
node = node->next;
free(node->prev);
}
free(node);
}
free(list);
}
static int min_element(const struct linked_list* list)
{
int min;
struct node* tmp = list->head;
if (list->size < 0) {
return -1;
}
else {
min = tmp->value;
}
while (tmp->next) {
tmp = tmp->next;
if (tmp->value < min) {
min = tmp->value;
}
}
return min;
}
int main(void)
{
struct linked_list* list = linked_list_new();
struct node* tmp;
size_t count;
srand(time(NULL));
printf("Number of elements: ");
scanf_s("%u", &count);
while (count--) {
linked_list_add(list, rand() % 100 - 50);
}
tmp = list->head;
while (tmp) {
printf("%d ", tmp->value);
tmp = tmp->next;
}
printf("\nMin. element: %d\n", min_element(list));
linked_list_free(list);
return 0;
}
(linked_list*)...
, но то ли умные, то ли занудные дяди говорят что надо писать static_cast<linked_list*>(...)
struct linked_list* list = malloc(sizeof * list); // подчеркивает красным
struct linked_list* list = (linked_list*)malloc(sizeof * list); //не подчеркивает красным