From 6980abc71df8ac764a566865be50cbc0bd9e14c7 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Fri, 5 Dec 2025 17:29:01 +0300 Subject: [PATCH] kr 2 --- kr2/list.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ kr2/list.h | 22 ++++++++++++ kr2/palindrom.c | 25 ++++++++++++++ kr2/shift.c | 29 ++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 kr2/list.c create mode 100644 kr2/list.h create mode 100644 kr2/palindrom.c create mode 100644 kr2/shift.c diff --git a/kr2/list.c b/kr2/list.c new file mode 100644 index 0000000..759e9e4 --- /dev/null +++ b/kr2/list.c @@ -0,0 +1,90 @@ +#include "list.h" + + +void initList(List* list) { + (*list).head = NULL; + (*list).size = 0; +} + +void addValue(List* list, int value) { + Node* head = (*list).head; + + Node* node = malloc(sizeof(Node)); + (*node).value = value; + (*node).next = NULL; + + if (head == NULL) { + (*list).head = node; + (*list).size++; + return; + } + + (*node).next = head; + (*list).head = node; + (*list).size++; +} + +void deleteList(List* list) { + Node* head = (*list).head; + + if (head == NULL) { + return; + } + + + + Node* next = (*head).next; + // 1 2 3 4 5 + while (next != NULL) { + free(head); + head = next; + next = (*next).next; + } + + free(head); + + (*list).size = 0; + (*list).head = NULL; +} + +int isPalindrom(List* list) { + List listReverse; + initList(&listReverse); + + Node* head = (*list).head; + + + int size = (*list).size; + for (int i = 0; i < size; i++) { + int value = (*head).value; + addValue(&listReverse, value); + head = (*head).next; + } + + Node* headStart = (*list).head; + Node* headEnd = listReverse.head; + for (int i = 0; i < size; i++) { + int valueStart = (*headStart).value; + int valueEnd = (*headEnd).value; + if (valueStart != valueEnd) { + deleteList(&listReverse); + return 0; + } + headStart = (*headStart).next; + headEnd = (*headEnd).next; + } + + deleteList(&listReverse); + + return 1; +} + + +void printList(List* list) { + Node* head = (*list).head; + while (head != NULL) { + printf("%d ", (*head).value); + head = (*head).next; + } + printf("\n"); +} diff --git a/kr2/list.h b/kr2/list.h new file mode 100644 index 0000000..e49b504 --- /dev/null +++ b/kr2/list.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + +struct Node { + int value; + struct Node* next; +} typedef Node; + +struct List { + Node* head; + int size; +} typedef List; + +void initList(List* list); +void deleteList(List* list); + +void addValue(List* list, int value); + +int isPalindrom(List* list); + +void printList(List* list); diff --git a/kr2/palindrom.c b/kr2/palindrom.c new file mode 100644 index 0000000..ed36664 --- /dev/null +++ b/kr2/palindrom.c @@ -0,0 +1,25 @@ +#include "list.h" + +void main() { + List list; + initList(&list); + + int n; + printf("Введите количество: "); + scanf("%d", &n); + + for (int i = 0; i < n; i++) { + int a; + scanf("%d", &a); + addValue(&list, a); + } + + + int res = isPalindrom(&list); + if (res == 0) { + printf("Не палиндром\n"); + } + else { + printf("Палиндром\n"); + } +} diff --git a/kr2/shift.c b/kr2/shift.c new file mode 100644 index 0000000..9bbdcb4 --- /dev/null +++ b/kr2/shift.c @@ -0,0 +1,29 @@ +#include + +void main() { + int* arr[32] = {0}; + + + int n; + printf("Введите число: "); + scanf("%d", &n); + + toArray(n, &arr); + + for (int i = 0; i < 32; i++) { + int a = arr[i]; + printf("%d", a); + } + printf("\n"); + +} + +void toArray(int n, int* arr[32]) { + int step = 0; + while (n > 0) { + int res = n % 2; + n = n / 2; + step++; + arr[32-step] = res; + } +}