Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <Windows.h>
- // Структура узла стека
- struct Node {
- int data;
- struct Node* next;
- };
- // Функция для создания нового узла
- struct Node* newNode(int data) {
- struct Node* node = (struct Node*)malloc(sizeof(struct Node));
- node->data = data;
- node->next = NULL;
- return node;
- }
- // Функция для добавления элемента в стек
- void push(struct Node** root, int data) {
- struct Node* node = newNode(data);
- node->next = *root;
- *root = node;
- printf("%d добавлено в стек.\n", data);
- }
- // Функция для удаления всех четных чисел из стека
- void removeEvenNumbers(struct Node** root) {
- struct Node* current = *root;
- struct Node* prev = NULL;
- while (current != NULL) {
- if (current->data % 2 == 0) {
- if (prev == NULL) {
- *root = current->next;
- free(current);
- current = *root;
- } else {
- prev->next = current->next;
- free(current);
- current = prev->next;
- }
- } else {
- prev = current;
- current = current->next;
- }
- }
- }
- // Функция для вывода содержимого стека
- void printStack(struct Node* root) {
- if (root == NULL) {
- printf("Стек пуст.\n");
- return;
- }
- printf("Содержимое стека: ");
- while (root != NULL) {
- printf("%d ", root->data);
- root = root->next;
- }
- printf("\n");
- }
- int main() {
- SetConsoleOutputCP(CP_UTF8);
- struct Node* stack = NULL;
- push(&stack, 1);
- push(&stack, 2);
- push(&stack, 3);
- push(&stack, 4);
- push(&stack, 5);
- printf("Исходное ");
- printStack(stack);
- removeEvenNumbers(&stack);
- printf("Стек после удаления четных чисел: ");
- printStack(stack);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment