Описать процедуру, которая удаляет из списка L первый отрицательный элемент - C (СИ)
Формулировка задачи:
Описать процедуру, которая удаляет из списка L первый отрицательный элемент, если такой есть (type=int);
(надо через линейные однонаправленные списки)
Решение задачи: «Описать процедуру, которая удаляет из списка L первый отрицательный элемент»
textual
Листинг программы
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct list_t
{
int value;
struct list_t* next;
} TList;
//-----------------------------------------------------------------------------
TList* Push(TList** list, int value)
{
TList* node = (TList*) malloc(sizeof(TList));
node->value = value;
node->next = *list;
*list = node;
return *list;
}
//-----------------------------------------------------------------------------
void Clear(TList** list)
{
TList* node;
while (*list)
{
node = *list;
*list = (*list)->next;
free(node);
}
}
//-----------------------------------------------------------------------------
int IsNegative(int value)
{
return (value < 0);
}
//-----------------------------------------------------------------------------
TList** FindElement(TList** list, int (*func)(int))
{
if (*list && !func((*list)->value))
{
return FindElement(&((*list)->next), func);
}
return list;
}
//-----------------------------------------------------------------------------
TList* DeleteFirstNegative(TList** list)
{
TList** find = FindElement(list, IsNegative);
if (*find)
{
TList* node = *find;
*find = (*find)->next;
free(node);
}
return *list;
}
//-----------------------------------------------------------------------------
TList* GetStack()
{
TList* list = NULL;
int value;
printf("input element (write other char for the end): ");
while (scanf("%d", &value))
{
Push(&list, value);
}
return list;
}
//-----------------------------------------------------------------------------
void Print(const TList* list)
{
for (; list; list = list->next)
{
printf("%d ", list->value);
}
printf("\n");
}
//-----------------------------------------------------------------------------
int main()
{
srand(time(NULL));
TList* list = GetStack();
Print(list);
DeleteFirstNegative(&list);
Print(list);
system("pause");
Clear(&list);
return 0;
}