#include <stdio.h>
#include <stdlib.h>
// Структура узла
typedef struct Node {
int data;
struct Node* next;
} Node;
// --- Создание нового узла ---
Node* createNode(int value) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = value;
p->next = NULL;
return p;
}
// --- Добавление в голову ---
void pushFront(Node** head, int value) {
Node* p = createNode(value);
p->next = *head;
*head = p;
}
// --- Удаление головы ---
void popFront(Node** head) {
if (*head == NULL) return;
Node* tmp = *head;
*head = (*head)->next;
free(tmp);
}
// --- Поиск по значению (возвращает указатель на узел или NULL) ---
Node* findNode(Node* head, int value) {
Node* cur = head;
while (cur != NULL) {
if (cur->data == value)
return cur;
cur = cur->next;
}
return NULL;
}
// --- Печать списка (для наглядности) ---
void printList(Node* head) {
for (Node* cur = head; cur != NULL; cur = cur->next)
printf("%d ", cur->data);
printf("\n");
}
// --- Освобождение всей памяти ---
void freeList(Node** head) {
Node* cur = *head;
while (cur != NULL) {
Node* next = cur->next;
free(cur);
cur = next;
}
*head = NULL;
}
// --- Пример использования ---
int main() {
Node* head = NULL; // создание пустого списка
pushFront(&head, 10);
pushFront(&head, 20);
pushFront(&head, 30);
printList(head); // 30 20 10
popFront(&head);
printList(head); // 20 10
Node* found = findNode(head, 20);
if (found)
printf("Найдено: %d\n", found->data);
freeList(&head);
return 0;
}