-
Notifications
You must be signed in to change notification settings - Fork 0
Kr2number1IsSymmetricalList #16
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: main
Are you sure you want to change the base?
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,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.0.31903.59 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "KR2Number1FirstTry", "KR2Number1FirstTry\KR2Number1FirstTry.vcxproj", "{1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|x64 = Debug|x64 | ||
| Debug|x86 = Debug|x86 | ||
| Release|x64 = Release|x64 | ||
| Release|x86 = Release|x86 | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Debug|x64.Build.0 = Debug|x64 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Debug|x86.Build.0 = Debug|Win32 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Release|x64.ActiveCfg = Release|x64 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Release|x64.Build.0 = Release|x64 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Release|x86.ActiveCfg = Release|Win32 | ||
| {1B9DD3DF-DBE1-4D21-B663-3B38FFCE4061}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {522076BB-2443-4BBD-8928-D20A33E57B17} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <ItemGroup Label="ProjectConfigurations"> | ||
| <ProjectConfiguration Include="Debug|Win32"> | ||
| <Configuration>Debug</Configuration> | ||
| <Platform>Win32</Platform> | ||
| </ProjectConfiguration> | ||
| <ProjectConfiguration Include="Release|Win32"> | ||
| <Configuration>Release</Configuration> | ||
| <Platform>Win32</Platform> | ||
| </ProjectConfiguration> | ||
| <ProjectConfiguration Include="Debug|x64"> | ||
| <Configuration>Debug</Configuration> | ||
| <Platform>x64</Platform> | ||
| </ProjectConfiguration> | ||
| <ProjectConfiguration Include="Release|x64"> | ||
| <Configuration>Release</Configuration> | ||
| <Platform>x64</Platform> | ||
| </ProjectConfiguration> | ||
| </ItemGroup> | ||
| <PropertyGroup Label="Globals"> | ||
| <VCProjectVersion>16.0</VCProjectVersion> | ||
| <Keyword>Win32Proj</Keyword> | ||
| <ProjectGuid>{1b9dd3df-dbe1-4d21-b663-3b38ffce4061}</ProjectGuid> | ||
| <RootNamespace>KR2Number1FirstTry</RootNamespace> | ||
| <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> | ||
| </PropertyGroup> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>true</UseDebugLibraries> | ||
| <PlatformToolset>v143</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v143</PlatformToolset> | ||
| <WholeProgramOptimization>true</WholeProgramOptimization> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>true</UseDebugLibraries> | ||
| <PlatformToolset>v143</PlatformToolset> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
| <ConfigurationType>Application</ConfigurationType> | ||
| <UseDebugLibraries>false</UseDebugLibraries> | ||
| <PlatformToolset>v143</PlatformToolset> | ||
| <WholeProgramOptimization>true</WholeProgramOptimization> | ||
| <CharacterSet>Unicode</CharacterSet> | ||
| </PropertyGroup> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
| <ImportGroup Label="ExtensionSettings"> | ||
| </ImportGroup> | ||
| <ImportGroup Label="Shared"> | ||
| </ImportGroup> | ||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
| </ImportGroup> | ||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
| </ImportGroup> | ||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
| </ImportGroup> | ||
| <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
| <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
| </ImportGroup> | ||
| <PropertyGroup Label="UserMacros" /> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
| <LinkIncremental>true</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
| <LinkIncremental>false</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
| <LinkIncremental>true</LinkIncremental> | ||
| </PropertyGroup> | ||
| <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
| <LinkIncremental>false</LinkIncremental> | ||
| </PropertyGroup> | ||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
| <ClCompile> | ||
| <WarningLevel>Level3</WarningLevel> | ||
| <SDLCheck>true</SDLCheck> | ||
| <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
| <ConformanceMode>true</ConformanceMode> | ||
| </ClCompile> | ||
| <Link> | ||
| <SubSystem>Console</SubSystem> | ||
| <GenerateDebugInformation>true</GenerateDebugInformation> | ||
| </Link> | ||
| </ItemDefinitionGroup> | ||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
| <ClCompile> | ||
| <WarningLevel>Level3</WarningLevel> | ||
| <FunctionLevelLinking>true</FunctionLevelLinking> | ||
| <IntrinsicFunctions>true</IntrinsicFunctions> | ||
| <SDLCheck>true</SDLCheck> | ||
| <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
| <ConformanceMode>true</ConformanceMode> | ||
| </ClCompile> | ||
| <Link> | ||
| <SubSystem>Console</SubSystem> | ||
| <EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
| <OptimizeReferences>true</OptimizeReferences> | ||
| <GenerateDebugInformation>true</GenerateDebugInformation> | ||
| </Link> | ||
| </ItemDefinitionGroup> | ||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
| <ClCompile> | ||
| <WarningLevel>Level3</WarningLevel> | ||
| <SDLCheck>false</SDLCheck> | ||
| <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
| <ConformanceMode>true</ConformanceMode> | ||
| </ClCompile> | ||
| <Link> | ||
| <SubSystem>Console</SubSystem> | ||
| <GenerateDebugInformation>true</GenerateDebugInformation> | ||
| </Link> | ||
| </ItemDefinitionGroup> | ||
| <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
| <ClCompile> | ||
| <WarningLevel>Level3</WarningLevel> | ||
| <FunctionLevelLinking>true</FunctionLevelLinking> | ||
| <IntrinsicFunctions>true</IntrinsicFunctions> | ||
| <SDLCheck>true</SDLCheck> | ||
| <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
| <ConformanceMode>true</ConformanceMode> | ||
| </ClCompile> | ||
| <Link> | ||
| <SubSystem>Console</SubSystem> | ||
| <EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
| <OptimizeReferences>true</OptimizeReferences> | ||
| <GenerateDebugInformation>true</GenerateDebugInformation> | ||
| </Link> | ||
| </ItemDefinitionGroup> | ||
| <ItemGroup> | ||
| <ClInclude Include="list.h" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClCompile Include="list.c" /> | ||
| <ClCompile Include="main.c" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Text Include="test.txt" /> | ||
| </ItemGroup> | ||
| <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
| <ImportGroup Label="ExtensionTargets"> | ||
| </ImportGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <ItemGroup> | ||
| <Filter Include="Исходные файлы"> | ||
| <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
| <Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
| </Filter> | ||
| <Filter Include="Файлы заголовков"> | ||
| <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
| <Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> | ||
| </Filter> | ||
| <Filter Include="Файлы ресурсов"> | ||
| <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
| <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
| </Filter> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClInclude Include="list.h"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClInclude> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ClCompile Include="list.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| <ClCompile Include="main.c"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </ClCompile> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Text Include="test.txt"> | ||
| <Filter>Исходные файлы</Filter> | ||
| </Text> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||||
| #include "list.h" | ||||||||
| #include <stdbool.h> | ||||||||
| #include <string.h> | ||||||||
|
|
||||||||
| typedef struct Node { | ||||||||
| int value; | ||||||||
| struct Node* next; | ||||||||
| struct Node* prev; | ||||||||
| } Node; | ||||||||
|
|
||||||||
| struct List { | ||||||||
| struct Node* head; | ||||||||
| struct Node* tail; | ||||||||
| }; | ||||||||
|
|
||||||||
| int insert(List* list, int value) { | ||||||||
| if (list->head == NULL) { | ||||||||
| Node* temp = calloc(1, sizeof(Node)); | ||||||||
| if (temp == NULL) { | ||||||||
| return -1; | ||||||||
| } | ||||||||
| list->head = temp; | ||||||||
| list->tail = temp; | ||||||||
| temp->next = temp; | ||||||||
| temp->prev = temp; | ||||||||
| temp->value = value; | ||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| Node* walker = list->head; | ||||||||
| while (walker->next != list->head) { | ||||||||
| walker = walker->next; | ||||||||
| } | ||||||||
|
Comment on lines
+31
to
+33
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->prev, только за линейное время. Тем более что у Вас ещё есть tail |
||||||||
| Node* temp = calloc(1, sizeof(Node)); | ||||||||
| if (temp == NULL) { | ||||||||
| return -1; | ||||||||
| } | ||||||||
| temp->value = value; | ||||||||
| list->tail = temp; | ||||||||
| walker->next = temp; | ||||||||
| temp->prev = walker; | ||||||||
| temp->next = list->head; | ||||||||
|
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. list->head->prev = temp ещё |
||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| void clearList(List* list) { | ||||||||
| if (list->head == NULL) { | ||||||||
| free(list); | ||||||||
| return; | ||||||||
| } | ||||||||
| while (list->head != list->tail) { | ||||||||
| Node* walker = list->head; | ||||||||
| while (walker != list->tail) { | ||||||||
| walker = walker->next; | ||||||||
| } | ||||||||
| list->tail = walker->prev; | ||||||||
| free(walker); | ||||||||
| walker = NULL; | ||||||||
|
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. Это бесполезно, потому что строчкой ниже мы уйдём на следующую итерацию, где walker будет уже другим. Можно хоть 239 тут в него присвоить :) |
||||||||
| } | ||||||||
|
Comment on lines
+51
to
+59
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. Удаление списка за O(n^2) вообще без какой-то видимой причины :) Прямо обязательно удалять из хвоста, при этом несмотря на цикличность и двусвязность списка попадать в хвост полным проходом по списку — это неправильно. Можно было просто while (list->head != list->tail) { Node *temp = list->head; list->head = list->head->next; free(temp); } |
||||||||
| free(list->head); | ||||||||
| free(list); | ||||||||
| } | ||||||||
|
|
||||||||
| List* createList(void) { | ||||||||
| List* list = calloc(1, sizeof(List)); | ||||||||
| return list; | ||||||||
|
Comment on lines
+65
to
+66
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.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| int isSymmetricalList(List* list) { | ||||||||
| if (list == NULL || list->head == list->tail) { | ||||||||
| return 1; | ||||||||
| } | ||||||||
| Node* walkerHead = list->head; | ||||||||
| Node* walkerTail = list->tail; | ||||||||
| while (walkerTail != list->head && walkerHead != list->tail) { | ||||||||
|
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. А мне кажется, что они могли бы двигаться, пока не встретились. А так они список по сути дважды проходят. |
||||||||
| if (walkerTail->value != walkerHead->value) { | ||||||||
| return 0; | ||||||||
| } | ||||||||
| walkerHead = walkerHead->next; | ||||||||
| walkerTail = walkerTail->prev; | ||||||||
| } | ||||||||
| return 1; | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #pragma once | ||
| #include <stdbool.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct List List; | ||
|
|
||
| //Insert to list | ||
| int insert(List* list, int value); | ||
|
|
||
| //Clear list | ||
| void clearList(List* list); | ||
|
|
||
| //Create new list | ||
| List* createList(void); | ||
|
|
||
| //Check is symmetrical list | ||
| int isSymmetricalList(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. Он должен был быть bool-ом |
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||
| #include <stdio.h> | ||||||
| #include <stdlib.h> | ||||||
| #include <stdbool.h> | ||||||
| #include "list.h" | ||||||
|
|
||||||
| int workWithFile(char fileName[]) { | ||||||
|
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.
Suggested change
|
||||||
| FILE* file = fopen(fileName, "r"); | ||||||
| if (file == NULL) { | ||||||
| printf("File not found!"); | ||||||
| return -2; | ||||||
| } | ||||||
| int number = 0; | ||||||
| List* list = createList(); | ||||||
| while (fscanf(file, "%d", &number) > 0) { | ||||||
| if (insert(list, number) == -1) { | ||||||
| return -2; | ||||||
|
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 result = isSymmetricalList(list); | ||||||
| clearList(list); | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| bool test() { | ||||||
| int result = workWithFile("test.txt"); | ||||||
| return result == 1; | ||||||
| } | ||||||
|
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 main() { | ||||||
| if (test()) { | ||||||
| printf("Tests correct\n"); | ||||||
| } else { | ||||||
| printf("Tests incorrect\n"); | ||||||
| return -1; | ||||||
| } | ||||||
| char fileName[101] = { '\0' }; | ||||||
| printf("Input fileName, maximum of 100 characters\n"); | ||||||
| int checkScanf = scanf("%s", fileName); | ||||||
| while (checkScanf != 1) { | ||||||
| while (getchar() != '\n') { | ||||||
| } | ||||||
| printf("Error...\n"); | ||||||
| checkScanf = scanf("%s", &fileName); | ||||||
| } | ||||||
| int result = workWithFile(fileName); | ||||||
| if (result == -2) { | ||||||
| printf("Error...\n"); | ||||||
| return -1; | ||||||
| } else if (result == 1) { | ||||||
| printf("List symmetrical\n"); | ||||||
| } else { | ||||||
| printf("List not symmetrical\n"); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 10 12 10 |
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.
В этой задаче циклический список несколько излишний :)