Skip to content
Open
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
6 changes: 6 additions & 0 deletions hw7/mergeSort/contacts.txt
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
112 changes: 112 additions & 0 deletions hw7/mergeSort/list.c
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);

This comment was marked as resolved.

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;
}
44 changes: 44 additions & 0 deletions hw7/mergeSort/list.h
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);
50 changes: 50 additions & 0 deletions hw7/mergeSort/main.c
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);
}
94 changes: 94 additions & 0 deletions hw7/mergeSort/mergeSort.c
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);

Choose a reason for hiding this comment

The 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;
}
11 changes: 11 additions & 0 deletions hw7/mergeSort/mergeSort.h
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);
31 changes: 31 additions & 0 deletions hw7/mergeSort/mergeSort.sln
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
Loading