Skip to content
Open

kr 2 #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions kr2/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "list.h"


void initList(List* list) {
(*list).head = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вы мне говорили, что научились пользоваться оператором ->. А здесь снова это. Вы что ли изучали C в ночь после контрольной?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, я помню про ->. Мне (*p). больше нравится, так понятнее что я делаю.

Если больше одного раза приходится разыменовывать, например, node->next->next->value или ((*(*(*node).next).next).value)
В этом случае через -> понятнее и проще

-> быстрее пишется, может так скоро начну писать и для одного разыменования

(*list).size = 0;
}

void addValue(List* list, int value) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Элемент добавляется в начало списка, да? Надо бы как-то это указать. Естественно, самый предпочтительный вариант -- в имени функции: addValueToFront. Или хотя бы в комментарии.

Кстати, комментарии недо не забывать писать. Формально на контрольной можно снижать баллы за отстутсвие комментариев.

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;
}



Comment on lines +33 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не забывайте о форматировании.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот тут оно и стреляет. Если не вчитываться в код addValue, кажется, что список просто копируется, и непонятно, где он разворачивается.

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");
}
22 changes: 22 additions & 0 deletions kr2/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>

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);
25 changes: 25 additions & 0 deletions kr2/palindrom.c

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ещё нужны хоть какие-то тесты.

Original file line number Diff line number Diff line change
@@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, Ваш алгоритм работает за O(n). За это я всем добавлял по 5 дополнительных баллов.

}


int res = isPalindrom(&list);
if (res == 0) {
printf("Не палиндром\n");
}
else {
printf("Палиндром\n");
}
}
29 changes: 29 additions & 0 deletions kr2/shift.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

void main() {
int* arr[32] = {0};


int n;
printf("Введите число: ");
scanf("%d", &n);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет, тут надо наоборот. Вводится массив, а вывести надо число.


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;
}
}