Skip to content

Kr2number1IsSymmetricalList - #16

Open
Palezehvat wants to merge 2 commits into
mainfrom
KR2Number1FirstTry
Open

Palezehvat wants to merge 2 commits into
mainfrom
KR2Number1FirstTry

Conversation

@Palezehvat

Copy link
Copy Markdown
Owner

No description provided.

Comment on lines +24 to +25
temp->next = temp;
temp->prev = temp;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

В этой задаче циклический список несколько излишний :)

Comment on lines +31 to +33
while (walker->next != list->head) {
walker = walker->next;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

И получится head->prev, только за линейное время. Тем более что у Вас ещё есть tail

list->tail = temp;
walker->next = temp;
temp->prev = walker;
temp->next = list->head;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

list->head->prev = temp ещё

}
list->tail = walker->prev;
free(walker);
walker = NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Это бесполезно, потому что строчкой ниже мы уйдём на следующую итерацию, где walker будет уже другим. Можно хоть 239 тут в него присвоить :)

Comment on lines +51 to +59
while (list->head != list->tail) {
Node* walker = list->head;
while (walker != list->tail) {
walker = walker->next;
}
list->tail = walker->prev;
free(walker);
walker = NULL;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Удаление списка за O(n^2) вообще без какой-то видимой причины :) Прямо обязательно удалять из хвоста, при этом несмотря на цикличность и двусвязность списка попадать в хвост полным проходом по списку — это неправильно. Можно было просто while (list->head != list->tail) { Node *temp = list->head; list->head = list->head->next; free(temp); }

List* createList(void);

//Check is symmetrical list
int isSymmetricalList(List* list); No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Он должен был быть bool-ом

}
Node* walkerHead = list->head;
Node* walkerTail = list->tail;
while (walkerTail != list->head && walkerHead != list->tail) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А мне кажется, что они могли бы двигаться, пока не встретились. А так они список по сути дважды проходят.

#include <stdbool.h>
#include "list.h"

int workWithFile(char fileName[]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
int workWithFile(char fileName[]) {
int workWithFile(const char *fileName) {

List* list = createList();
while (fscanf(file, "%d", &number) > 0) {
if (insert(list, number) == -1) {
return -2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Так уже выделенная под список память не удалится

bool test() {
int result = workWithFile("test.txt");
return result == 1;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Надо было ещё на несимметричность хоть один тест :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants