-
Notifications
You must be signed in to change notification settings - Fork 0
kr 2 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
kr 2 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #include "list.h" | ||
|
|
||
|
|
||
| void initList(List* list) { | ||
| (*list).head = NULL; | ||
| (*list).size = 0; | ||
| } | ||
|
|
||
| void addValue(List* list, int value) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Элемент добавляется в начало списка, да? Надо бы как-то это указать. Естественно, самый предпочтительный вариант -- в имени функции: Кстати, комментарии недо не забывать писать. Формально на контрольной можно снижать баллы за отстутсвие комментариев. |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот тут оно и стреляет. Если не вчитываться в код |
||
| 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"); | ||
| } | ||
| 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); |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется, Ваш алгоритм работает за |
||
| } | ||
|
|
||
|
|
||
| int res = isPalindrom(&list); | ||
| if (res == 0) { | ||
| printf("Не палиндром\n"); | ||
| } | ||
| else { | ||
| printf("Палиндром\n"); | ||
| } | ||
| } | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вы мне говорили, что научились пользоваться оператором
->. А здесь снова это. Вы что ли изучали C в ночь после контрольной?There was a problem hiding this comment.
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)В этом случае через
->понятнее и проще->быстрее пишется, может так скоро начну писать и для одного разыменования