-
Notifications
You must be signed in to change notification settings - Fork 0
mergeSort #28
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
Open
Andrw-404
wants to merge
7
commits into
main
Choose a base branch
from
hw7-mergeSort
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
mergeSort #28
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd241fe
add: list with everything
Andrw-404 508aa32
removing unnecessary libraries
Andrw-404 874b791
Implementing merge sort
Andrw-404 d5a268a
feat: fixes that caused the program to crash
Andrw-404 752a87b
fix: restoring functionality after breaking into modules
Andrw-404 bd47419
refactor: adding suggested changes
Andrw-404 6498ae8
refactor: removing intermediate lists
Andrw-404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Baaaasd 5555 | ||
| Aaaa 4444 | ||
| Bbbb 3333 | ||
| Cccc 2222 | ||
| Dddd 1111 | ||
| ffff 3242 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #define _CRT_SECURE_NO_WARNINGS | ||
|
|
||
| #include "list.h" | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <stdlib.h> | ||
| #include <stdbool.h> | ||
|
|
||
| typedef struct ListElement { | ||
| char name[40]; | ||
| char phone[20]; | ||
| struct ListElement* next; | ||
| } ListElement; | ||
|
|
||
| typedef struct List { | ||
| ListElement* head; | ||
| } List; | ||
|
|
||
| List* createList() { | ||
| List* list = malloc(sizeof(List)); | ||
| if (list == NULL) { | ||
| return NULL; | ||
| } | ||
| list->head = calloc(1, sizeof(ListElement)); | ||
| if (list->head == NULL) { | ||
| free(list); | ||
| return NULL; | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| void addContact(List* list, const char* name, const char* phone) { | ||
| if (list == NULL) { | ||
| return; | ||
| } | ||
| ListElement* element = malloc(sizeof(ListElement)); | ||
| if (element == NULL) { | ||
| return; | ||
| } | ||
| strcpy(element->name, name); | ||
| strcpy(element->phone, phone); | ||
| element->next = list->head->next; | ||
| list->head->next = element; | ||
| } | ||
|
|
||
| bool isEmpty(List* list) { | ||
| return list == NULL || list->head->next == NULL; | ||
| } | ||
|
|
||
| void printList(List* list) { | ||
| if (isEmpty(list)) { | ||
| printf("List is empty\n"); | ||
| return; | ||
| } | ||
| Position current = list->head->next; | ||
| while (current != NULL) { | ||
| printf("%s %s\n", current->name, current->phone); | ||
| current = current->next; | ||
| } | ||
| } | ||
|
|
||
| void removeList(List** list) { | ||
| if (list == NULL || *list == NULL) { | ||
| return; | ||
| } | ||
|
|
||
| ListElement* current = (*list)->head; | ||
| while (current != NULL) { | ||
| ListElement* next = current->next; | ||
| free(current); | ||
| current = next; | ||
| } | ||
|
|
||
| free(*list); | ||
| *list = NULL; | ||
| } | ||
|
|
||
| const char* getName(ListElement* element) { | ||
| return element ? element->name : NULL; | ||
| } | ||
|
|
||
| const char* getPhone(ListElement* element) { | ||
| return element ? element->phone : NULL; | ||
| } | ||
|
|
||
| ListElement* getFirst(List* list) { | ||
| return list ? list->head : NULL; | ||
| } | ||
|
|
||
| ListElement* getNext(Position position) { | ||
| return position ? position->next : NULL; | ||
| } | ||
|
|
||
| void setNext(ListElement* element, ListElement* next) { | ||
| if (element != NULL) { | ||
| element->next = next; | ||
| } | ||
| } | ||
|
|
||
| ListElement* createListElement(const char* name, const char* phone) { | ||
| ListElement* element = malloc(sizeof(ListElement)); | ||
| if (element == NULL) { | ||
| return NULL; | ||
| } | ||
| strncpy(element->name, name, sizeof(element->name) - 1); | ||
| element->name[sizeof(element->name) - 1] = '\0'; | ||
| strncpy(element->phone, phone, sizeof(element->phone) - 1); | ||
| element->phone[sizeof(element->phone) - 1] = '\0'; | ||
| element->next = NULL; | ||
| return element; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| typedef int Value; | ||
|
|
||
| typedef struct List List; | ||
|
|
||
| typedef struct ListElement ListElement; | ||
|
|
||
| typedef struct ListElement* Position; | ||
|
|
||
| // function for creating a list | ||
| List* createList(void); | ||
|
|
||
| // function for adding an element | ||
| void addContact(List* list, const char* name, const char* phone); | ||
|
|
||
| // checks if the list is empty | ||
| bool isEmpty(List* list); | ||
|
|
||
| // prints all elements of the list | ||
| void printList(List* list); | ||
|
|
||
| // removes the entire list and frees memory | ||
| void removeList(List** list); | ||
|
|
||
| // function for getting the name of the transmitted contact | ||
| const char* getName(ListElement* element); | ||
|
|
||
| // function for getting the phone number of a transmitted contact | ||
| const char* getPhone(ListElement* element); | ||
|
|
||
| // function for getting the first element in the list | ||
| ListElement* getFirst(List* list); | ||
|
|
||
| // function to get the next element in the list | ||
| ListElement* getNext(Position position); | ||
|
|
||
| // function for changing the pointer to the next element | ||
| void setNext(ListElement* element, ListElement* next); | ||
|
|
||
| // accepts the name and number and returns the contact with these details | ||
| ListElement* createListElement(const char* name, const char* phone); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #define _CRT_SECURE_NO_WARNINGS | ||
|
|
||
| #include "list.h" | ||
| #include "reader.h" | ||
| #include "mergeSort.h" | ||
| #include "test.h" | ||
| #include "testsForList.h" | ||
|
|
||
| #include <stdio.h> | ||
| #include <locale.h> | ||
|
|
||
| int main(void) { | ||
| setlocale(LC_ALL, "Rus"); | ||
|
|
||
| if (!testCreateList() || !testIsEmpty() || !testAddContact()) { | ||
| printf("!!!the list did not pass the tests!!!\n\n\n"); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (!testMergeSortByName() || !testMergeSortByPhone()) { | ||
| printf("!!!the program failed the tests!!!\n\n\n"); | ||
| return NULL; | ||
| } | ||
|
|
||
| List* list = createList(); | ||
| readFromFile("contacts.txt", list); | ||
| printList(list); | ||
| int choice = 0; | ||
| printf("\n����������� ��: �����(1) ��� ��������(2)\n"); | ||
| scanf("%d", &choice); | ||
|
|
||
| SortType sortType = byName; | ||
|
|
||
| if (choice == 1) { | ||
| sortType = byName; | ||
| } | ||
| else if (choice == 2) { | ||
| sortType = byPhone; | ||
| } | ||
| else { | ||
| printf("������ �����\n"); | ||
| removeList(&list); | ||
| return 1; | ||
| } | ||
|
|
||
| list = mergeSort(list, sortType); | ||
| printf("\n\n��������������� ������: \n"); | ||
| printList(list); | ||
| removeList(&list); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #include "list.h" | ||
| #include "mergeSort.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| void split(List* source, List** front, List** back) { | ||
| *front = createList(); | ||
| *back = createList(); | ||
|
|
||
| ListElement* pos = getNext(getFirst(source)); | ||
|
|
||
| int length = 0; | ||
| while (pos != NULL) { | ||
| ++length; | ||
| pos = getNext(pos); | ||
| } | ||
|
|
||
| pos = getNext(getFirst(source)); | ||
|
|
||
| for (int i = 0; i < length / 2; ++i) { | ||
| addContact(*front, getName(pos), getPhone(pos)); | ||
| pos = getNext(pos); | ||
| } | ||
|
|
||
| while (pos != NULL) { | ||
| addContact(*back, getName(pos), getPhone(pos)); | ||
| pos = getNext(pos); | ||
| } | ||
| } | ||
|
|
||
| List* merge(List* first, List* second, SortType sortType) { | ||
| List* mergedList = createList(); | ||
| ListElement* current = getFirst(mergedList); | ||
|
|
||
| ListElement* firstElement = getNext(getFirst(first)); | ||
| ListElement* secondElement = getNext(getFirst(second)); | ||
|
|
||
|
|
||
| while (firstElement != NULL && secondElement != NULL) { | ||
| int compare = (sortType == byName) ? | ||
| strcmp(getName(firstElement), getName(secondElement)) : | ||
| strcmp(getPhone(firstElement), getPhone(secondElement)); | ||
|
|
||
| ListElement* nextElement = NULL; | ||
| if (compare <= 0) { | ||
| nextElement = createListElement(getName(firstElement), getPhone(firstElement)); | ||
| firstElement = getNext(firstElement); | ||
| } | ||
| else { | ||
| nextElement = createListElement(getName(secondElement), getPhone(secondElement)); | ||
| secondElement = getNext(secondElement); | ||
| } | ||
|
|
||
| setNext(current, nextElement); | ||
| current = nextElement; | ||
| } | ||
|
|
||
| while (firstElement != NULL) { | ||
| ListElement* nextElement = createListElement(getName(firstElement), getPhone(firstElement)); | ||
| setNext(current, nextElement); | ||
| current = nextElement; | ||
| firstElement = getNext(firstElement); | ||
| } | ||
|
|
||
| while (secondElement != NULL) { | ||
| ListElement* nextElement = createListElement(getName(secondElement), getPhone(secondElement)); | ||
| setNext(current, nextElement); | ||
| current = nextElement; | ||
| secondElement = getNext(secondElement); | ||
| } | ||
|
|
||
| setNext(current, NULL); | ||
| return mergedList; | ||
| } | ||
|
|
||
|
|
||
| List* mergeSort(List* list, SortType sortType) { | ||
| if (isEmpty(list) || getNext(getNext(getFirst(list))) == NULL) { | ||
| return list; | ||
| } | ||
|
|
||
| List* first = NULL; | ||
| List* second = NULL; | ||
| split(list, &first, &second); | ||
|
|
||
|
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 не нужен, его можно было бы тут удалить (и даже нужно, потому что иначе его так никто и не удалит) |
||
|
|
||
| first = mergeSort(first, sortType); | ||
| second = mergeSort(second, sortType); | ||
|
|
||
| List* mergedList = merge(first, second, sortType); | ||
| removeList(&first); | ||
| removeList(&second); | ||
| return mergedList; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include "list.h" | ||
|
|
||
| typedef enum { | ||
| byName, | ||
| byPhone | ||
| } SortType; | ||
|
|
||
| // sorts a list using a merge method | ||
| List* mergeSort(List* list, SortType sortType); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.11.35222.181 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mergeSort", "mergeSort.vcxproj", "{D62015DB-8D03-447E-BF4B-75B1A718FAF7}" | ||
| 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 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Debug|x64.ActiveCfg = Debug|x64 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Debug|x64.Build.0 = Debug|x64 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Debug|x86.ActiveCfg = Debug|Win32 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Debug|x86.Build.0 = Debug|Win32 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Release|x64.ActiveCfg = Release|x64 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Release|x64.Build.0 = Release|x64 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Release|x86.ActiveCfg = Release|Win32 | ||
| {D62015DB-8D03-447E-BF4B-75B1A718FAF7}.Release|x86.Build.0 = Release|Win32 | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {74CF9B38-6392-4E3F-93E9-39BE1F65456A} | ||
| EndGlobalSection | ||
| EndGlobal |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.