Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 14e6c71416 | |||
| 0e7a03e784 | |||
| b638d6c169 | |||
| 62e1b34fd0 | |||
| 10f35b3ac3 | |||
| d9d935c0bf | |||
| c9947a713f | |||
| 10445d4940 | |||
| 5e85fa060b |
55
.gitignore
vendored
55
.gitignore
vendored
|
|
@ -160,3 +160,58 @@ cython_debug/
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Object files
|
||||||
|
*.o
|
||||||
|
*.ko
|
||||||
|
*.obj
|
||||||
|
*.elf
|
||||||
|
|
||||||
|
# Linker output
|
||||||
|
*.ilk
|
||||||
|
*.map
|
||||||
|
*.exp
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Libraries
|
||||||
|
*.lib
|
||||||
|
*.a
|
||||||
|
*.la
|
||||||
|
*.lo
|
||||||
|
|
||||||
|
# Shared objects (inc. Windows DLLs)
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
*.i*86
|
||||||
|
*.x86_64
|
||||||
|
*.hex
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
*.dSYM/
|
||||||
|
*.su
|
||||||
|
*.idb
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Kernel Module Compile Results
|
||||||
|
*.mod*
|
||||||
|
*.cmd
|
||||||
|
.tmp_versions/
|
||||||
|
modules.order
|
||||||
|
Module.symvers
|
||||||
|
Mkfile.old
|
||||||
|
dkms.conf
|
||||||
|
|
||||||
|
# debug information files
|
||||||
|
*.dwo
|
||||||
|
|
|
||||||
1
stepushovgs/.gitignore
vendored
Normal file
1
stepushovgs/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
*.o
|
||||||
2
stepushovgs/data-structures/.gitignore
vendored
Normal file
2
stepushovgs/data-structures/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
.exe
|
||||||
|
.o
|
||||||
208
stepushovgs/data-structures/sorce/bin_search_tree/bst.c
Normal file
208
stepushovgs/data-structures/sorce/bin_search_tree/bst.c
Normal file
|
|
@ -0,0 +1,208 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "bst.h"
|
||||||
|
#include "queue.h"
|
||||||
|
/*
|
||||||
|
3. Двоичное дерево поиска
|
||||||
|
Узел — словарь: `{'val': '123', 'left': None, 'right': None}.`
|
||||||
|
|
||||||
|
Функции:
|
||||||
|
|
||||||
|
def bst_insert(root, name, phone) — рекурсивно или итеративно вставляет, возвращает новый корень (если корень меняется).
|
||||||
|
|
||||||
|
def bst_find(root, name) — поиск.
|
||||||
|
|
||||||
|
def bst_delete(root, name) — удаление, возвращает новый корень.
|
||||||
|
|
||||||
|
def bst_list_all(root) — центрированный обход (рекурсивно собирает записи в отсортированном порядке).
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bst_node* create_bst_node(char name[NAME_LEN], char phone[PHONE_LEN])
|
||||||
|
{
|
||||||
|
bst_node* node = (bst_node*)malloc(sizeof(bst_node));
|
||||||
|
|
||||||
|
strcpy(node->name, name);
|
||||||
|
strcpy(node->phone, phone);
|
||||||
|
|
||||||
|
node->left = NULL;
|
||||||
|
node->right = NULL;
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
bst_node* bst_minimum(bst_node* node)
|
||||||
|
{
|
||||||
|
if (node->left == NULL)
|
||||||
|
return node;
|
||||||
|
return bst_minimum(node->left);
|
||||||
|
}
|
||||||
|
|
||||||
|
bst_node* bst_maximum(bst_node* node)
|
||||||
|
{
|
||||||
|
if (node->right == NULL)
|
||||||
|
return node;
|
||||||
|
return bst_maximum(node->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_bst(bst_node node)
|
||||||
|
{
|
||||||
|
//printf("value: %d\n", node.value);
|
||||||
|
|
||||||
|
printf("name: %s, phone: %s\n", node.name, node.phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bst_inorder_traversal(bst_node* HEAD)
|
||||||
|
{
|
||||||
|
if (HEAD != NULL)
|
||||||
|
{
|
||||||
|
bst_inorder_traversal(HEAD->left);
|
||||||
|
print_bst(*HEAD);
|
||||||
|
bst_inorder_traversal(HEAD->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bst_preorder_traversal(bst_node* HEAD)
|
||||||
|
{
|
||||||
|
if (HEAD != NULL)
|
||||||
|
{
|
||||||
|
print_bst(*HEAD);
|
||||||
|
bst_preorder_traversal(HEAD->left);
|
||||||
|
bst_preorder_traversal(HEAD->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bst_node* bst_search(bst_node* HEAD, char target_name[NAME_LEN])
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Node search(x : Node, k : T):
|
||||||
|
if x == null or k == x.key
|
||||||
|
return x
|
||||||
|
if k < x.key
|
||||||
|
return search(x.left, k)
|
||||||
|
else
|
||||||
|
return search(x.right, k)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((HEAD == NULL) || strcmp(HEAD->name, target_name) == 0)
|
||||||
|
{
|
||||||
|
return HEAD;
|
||||||
|
}
|
||||||
|
if (strcmp(target_name, HEAD->name) < 0)
|
||||||
|
{
|
||||||
|
return bst_search(HEAD->left, target_name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return bst_search(HEAD->right, target_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bst_node* bst_insert(bst_node* HEAD, char name[NAME_LEN], char phone[PHONE_LEN])
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Node insert(x : Node, z : T): // x — корень поддерева, z — вставляемый ключ
|
||||||
|
if x == null
|
||||||
|
return Node(z) // подвесим Node с key = z
|
||||||
|
else if z < x.key
|
||||||
|
x.left = insert(x.left, z)
|
||||||
|
else if z > x.key
|
||||||
|
x.right = insert(x.right, z)
|
||||||
|
return x
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (HEAD == NULL)
|
||||||
|
{
|
||||||
|
return create_bst_node(name, phone);
|
||||||
|
}
|
||||||
|
else if (strcmp(name, HEAD->name) < 0)
|
||||||
|
{
|
||||||
|
HEAD->left = bst_insert(HEAD->left, name, phone);
|
||||||
|
}
|
||||||
|
else if (strcmp(name, HEAD->name) > 0)
|
||||||
|
{
|
||||||
|
HEAD->right = bst_insert(HEAD->right, name, phone);
|
||||||
|
}
|
||||||
|
return HEAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
bst_node* bst_delete(bst_node* root, char target_name[NAME_LEN])
|
||||||
|
{ // корень поддерева, удаляемый ключ
|
||||||
|
if (root == NULL)
|
||||||
|
return root;
|
||||||
|
|
||||||
|
if (strcmp(target_name, root->name) < 0)
|
||||||
|
root->left = bst_delete(root->left, target_name);
|
||||||
|
else if (strcmp(target_name, root->name) > 0)
|
||||||
|
root->right = bst_delete(root->right, target_name);
|
||||||
|
else {
|
||||||
|
if (root->left != NULL && root->right != NULL)
|
||||||
|
{
|
||||||
|
strcpy(root->name, bst_minimum(root->right)->name);
|
||||||
|
strcpy(root->phone, bst_minimum(root->right)->phone);
|
||||||
|
|
||||||
|
root->right = bst_delete(root->right, root->name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bst_node* temp = root;
|
||||||
|
if (root->left != NULL)
|
||||||
|
root = root->left;
|
||||||
|
else
|
||||||
|
root = root->right;
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_bst(bst_node* root)
|
||||||
|
{
|
||||||
|
if (root == NULL)
|
||||||
|
return;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
delete_bst(root->left);
|
||||||
|
delete_bst(root->right);
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printTree(bst_node* node, int depth) {
|
||||||
|
|
||||||
|
if (node == NULL) return;
|
||||||
|
|
||||||
|
printTree(node->right, depth + 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < depth; i++)
|
||||||
|
printf("\t");
|
||||||
|
//printf("%d\n", node->value);
|
||||||
|
print_bst(*node);
|
||||||
|
|
||||||
|
printTree(node->left, depth + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void treeLevelTraversal(bst_node* node) {
|
||||||
|
if (!node) return;
|
||||||
|
|
||||||
|
Queue q;
|
||||||
|
Queue* hq = &q;
|
||||||
|
queueInit(hq);
|
||||||
|
|
||||||
|
queuePush(hq, node);
|
||||||
|
while(!queueEmpty(hq))
|
||||||
|
{
|
||||||
|
bst_node* hn = queuePop(hq);
|
||||||
|
//printf("%d\n", hn->value);
|
||||||
|
print_bst(*hn);
|
||||||
|
|
||||||
|
if(hn->left)
|
||||||
|
queuePush(hq, hn->left);
|
||||||
|
if(hn->right)
|
||||||
|
queuePush(hq, hn->right);
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
||||||
37
stepushovgs/data-structures/sorce/bin_search_tree/bst.h
Normal file
37
stepushovgs/data-structures/sorce/bin_search_tree/bst.h
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#define NAME_LEN 20
|
||||||
|
#define PHONE_LEN 20
|
||||||
|
|
||||||
|
typedef struct bst_node
|
||||||
|
{
|
||||||
|
//int value;
|
||||||
|
|
||||||
|
char name[NAME_LEN];
|
||||||
|
char phone[PHONE_LEN];
|
||||||
|
|
||||||
|
struct bst_node* right;
|
||||||
|
struct bst_node* left;
|
||||||
|
}bst_node;
|
||||||
|
|
||||||
|
bst_node* create_bst_node(char name[NAME_LEN], char phone[PHONE_LEN]);
|
||||||
|
|
||||||
|
bst_node* bst_minimum(bst_node* node);
|
||||||
|
|
||||||
|
bst_node* bst_maximum(bst_node* node);
|
||||||
|
|
||||||
|
void print_bst(bst_node node);
|
||||||
|
|
||||||
|
void bst_inorder_traversal(bst_node* HEAD);
|
||||||
|
|
||||||
|
void bst_preorder_traversal(bst_node* HEAD);
|
||||||
|
|
||||||
|
bst_node* bst_search(bst_node* HEAD, char target_name[NAME_LEN]);
|
||||||
|
|
||||||
|
bst_node* bst_insert(bst_node* HEAD, char name[NAME_LEN], char phone[PHONE_LEN]);
|
||||||
|
|
||||||
|
bst_node* bst_delete(bst_node* root, char target_name[NAME_LEN]);
|
||||||
|
|
||||||
|
void treeLevelTraversal(bst_node* node);
|
||||||
|
|
||||||
|
void printTree(bst_node* node, int depth);
|
||||||
|
|
||||||
|
void delete_bst(bst_node* root);
|
||||||
42
stepushovgs/data-structures/sorce/bin_search_tree/queue.c
Normal file
42
stepushovgs/data-structures/sorce/bin_search_tree/queue.c
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
|
||||||
|
int queueEmpty(Queue* q)
|
||||||
|
{
|
||||||
|
return (q->head == q->tail);
|
||||||
|
}
|
||||||
|
|
||||||
|
int size(Queue* q)
|
||||||
|
{
|
||||||
|
if (q->head > q->tail)
|
||||||
|
return QUEUE_MAX_LENGTH - q->head + q->tail;
|
||||||
|
else
|
||||||
|
return q->tail - q->head;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queuePush(Queue* q, void* ptr)
|
||||||
|
{
|
||||||
|
if (size(q) != QUEUE_MAX_LENGTH)
|
||||||
|
{
|
||||||
|
q->p[q->tail] = ptr;
|
||||||
|
q->tail = (q->tail + 1) % QUEUE_MAX_LENGTH;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void queueInit(Queue* q)
|
||||||
|
{
|
||||||
|
q->head = 0;
|
||||||
|
q->tail = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* queuePop(Queue* q)
|
||||||
|
{
|
||||||
|
if (queueEmpty(q))
|
||||||
|
return NULL;
|
||||||
|
void* x = q->p[q->head];
|
||||||
|
q->head = (q->head + 1) % QUEUE_MAX_LENGTH;
|
||||||
|
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
17
stepushovgs/data-structures/sorce/bin_search_tree/queue.h
Normal file
17
stepushovgs/data-structures/sorce/bin_search_tree/queue.h
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#define QUEUE_MAX_LENGTH 100
|
||||||
|
|
||||||
|
typedef struct Queue {
|
||||||
|
void* p[QUEUE_MAX_LENGTH];
|
||||||
|
unsigned int head;
|
||||||
|
unsigned int tail;
|
||||||
|
} Queue;
|
||||||
|
|
||||||
|
int queueEmpty(Queue* q);
|
||||||
|
|
||||||
|
void queuePush(Queue* q, void* p);
|
||||||
|
|
||||||
|
void* queuePop(Queue* q);
|
||||||
|
|
||||||
|
void queueInit(Queue* q);
|
||||||
|
|
||||||
|
int size(Queue* q);
|
||||||
166
stepushovgs/data-structures/sorce/linked_list/linked_list.c
Normal file
166
stepushovgs/data-structures/sorce/linked_list/linked_list.c
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "linked_list.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
Связный список (LinkedListPhoneBook)
|
||||||
|
|
||||||
|
Узел представляется словарём: `{'name': 'Имя', 'phone': '123', 'next': None}.`
|
||||||
|
|
||||||
|
Функции:
|
||||||
|
|
||||||
|
def ll_insert(head, name, phone) — проходит до конца (или сразу добавляет в конец) и возвращает новую голову (если вставка в начало) или изменяет список по ссылке. Удобнее возвращать новую голову, если вставка может быть в начало.
|
||||||
|
|
||||||
|
def ll_find(head, name) — ищет узел, возвращает телефон или None.
|
||||||
|
|
||||||
|
def ll_delete(head, name) — удаляет узел, возвращает новую голову.
|
||||||
|
|
||||||
|
def ll_list_all(head) — собирает все записи в список и сортирует (сортировка вынесена отдельно).
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int getListNodeLength(Node* HEAD)
|
||||||
|
{
|
||||||
|
int len = 0;
|
||||||
|
|
||||||
|
Node* current = HEAD;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавление в конец
|
||||||
|
Node* insert(Node* head, char name[NAME_BUFF_SIZE], char phone[PHONE_BUFF_SIZE], int show)
|
||||||
|
{
|
||||||
|
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||||
|
|
||||||
|
strcpy_s(newNode->name_, NAME_BUFF_SIZE, name);
|
||||||
|
strcpy_s(newNode->phone_, PHONE_BUFF_SIZE, phone);
|
||||||
|
newNode->next = NULL;
|
||||||
|
|
||||||
|
printf("Data: %s %s\n", name, phone);
|
||||||
|
printf("New Data: %s %s\n", newNode->name_, newNode->phone_);
|
||||||
|
|
||||||
|
if (head == NULL)
|
||||||
|
{
|
||||||
|
printf("\nNew list\n");
|
||||||
|
head = newNode;
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* last = head;
|
||||||
|
int ind = 0;
|
||||||
|
while (last->next != NULL)
|
||||||
|
{
|
||||||
|
if (show == 1)
|
||||||
|
printf("%d \n", ind++);
|
||||||
|
last = last->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
last->next = newNode;
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* find(Node* HEAD, char target_name[NAME_BUFF_SIZE])
|
||||||
|
{
|
||||||
|
Node* current = HEAD;
|
||||||
|
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (strcmp(target_name, current->name_) == 0)
|
||||||
|
{
|
||||||
|
return current->phone_;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вывод всех элементов
|
||||||
|
void printAllNodes(Node* head)
|
||||||
|
{
|
||||||
|
Node* current = head;
|
||||||
|
int ind = 0;
|
||||||
|
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
printf("Ind: %d\nName: %s\nPhone: %s\n", ind++, current->name_, current->phone_);
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* deleteNode(Node* HEAD, char target_name[NAME_BUFF_SIZE])
|
||||||
|
{
|
||||||
|
Node* previous = NULL;
|
||||||
|
Node* current = HEAD;
|
||||||
|
|
||||||
|
if (current != NULL && strcmp(target_name, current->name_) == 0)
|
||||||
|
{
|
||||||
|
HEAD = current->next;
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
return HEAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (current != NULL && strcmp(target_name, current->name_) == 0)
|
||||||
|
{
|
||||||
|
previous = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == NULL) return HEAD;
|
||||||
|
|
||||||
|
previous->next = current->next;
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
return HEAD;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* listAll(Node* HEAD)
|
||||||
|
{
|
||||||
|
if (HEAD == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = getListNodeLength(HEAD);
|
||||||
|
Node* current = HEAD;
|
||||||
|
|
||||||
|
Node* list = (Node*)malloc(len * sizeof(Node));
|
||||||
|
|
||||||
|
int ind = 0;
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
list[ind++] = *current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printNode(Node node)
|
||||||
|
{
|
||||||
|
printf("%s ", node.name_);
|
||||||
|
printf("%s\n", node.phone_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printListNode(Node* list, int length)
|
||||||
|
{
|
||||||
|
printf("\n\n%d\n", length);
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
printNode(list[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
29
stepushovgs/data-structures/sorce/linked_list/linked_list.h
Normal file
29
stepushovgs/data-structures/sorce/linked_list/linked_list.h
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#define NAME_BUFF_SIZE 50
|
||||||
|
#define PHONE_BUFF_SIZE 12+1 // +1 for end symbol
|
||||||
|
|
||||||
|
typedef struct Node
|
||||||
|
{
|
||||||
|
char name_[NAME_BUFF_SIZE];
|
||||||
|
char phone_[PHONE_BUFF_SIZE];
|
||||||
|
struct Node* next;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct LinkedListPhoneNumbers {
|
||||||
|
Node* HEAD;
|
||||||
|
} LinkedListPhoneNumbers;
|
||||||
|
|
||||||
|
Node* insert(Node* head, char name[NAME_BUFF_SIZE], char phone[PHONE_BUFF_SIZE], int show);
|
||||||
|
void printAllNodes(Node* head);
|
||||||
|
void printNode(Node node);
|
||||||
|
|
||||||
|
char* find(Node* HEAD, char target_name[NAME_BUFF_SIZE]);
|
||||||
|
|
||||||
|
|
||||||
|
Node* deleteNode(Node* HEAD, char target_name[NAME_BUFF_SIZE]);
|
||||||
|
|
||||||
|
|
||||||
|
Node* listAll(Node* HEAD);
|
||||||
|
|
||||||
|
void printListNode(Node list[], int length);
|
||||||
|
|
||||||
|
int getListNodeLength(Node* HEAD);
|
||||||
44
stepushovgs/data-structures/sorce/main.c
Normal file
44
stepushovgs/data-structures/sorce/main.c
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "linked_list/linked_list.h"
|
||||||
|
|
||||||
|
#define NAME_BUFF_SIZE 50
|
||||||
|
#define PHONE_BUFF_SIZE 12+1 // +1 for end symbol
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Node* list = NULL;
|
||||||
|
char phone[] = "1234";
|
||||||
|
for (int i = 0; i < 12; i++)
|
||||||
|
{
|
||||||
|
char num[3];
|
||||||
|
sprintf_s(num, 3, "%d", i);
|
||||||
|
|
||||||
|
char name[] = "name ";
|
||||||
|
strcat_s(name, 9, num);
|
||||||
|
printf("%d %s %s\n", i, name, phone);
|
||||||
|
list = insert(list, name, phone, 0);
|
||||||
|
}
|
||||||
|
char test_name[] = "name 20";
|
||||||
|
char test_phone[] = "phone 343";
|
||||||
|
|
||||||
|
list = insert(list, test_name, test_phone, 1);
|
||||||
|
|
||||||
|
printAllNodes(list);
|
||||||
|
|
||||||
|
printf("\n%s\n", find(list, test_name));
|
||||||
|
|
||||||
|
strcpy_s(test_name, NAME_BUFF_SIZE, "name 10");
|
||||||
|
list = deleteNode(list, test_name);
|
||||||
|
|
||||||
|
printAllNodes(list);
|
||||||
|
|
||||||
|
Node* listNodes = listAll(list);
|
||||||
|
printListNode(listNodes, getListNodeLength(list));
|
||||||
|
|
||||||
|
free(listNodes);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
86
stepushovgs/data-structures/sorce/main_tree.c
Normal file
86
stepushovgs/data-structures/sorce/main_tree.c
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "bin_search_tree/bst.h"
|
||||||
|
|
||||||
|
#define COUNT_NUMBERS 64
|
||||||
|
|
||||||
|
int isInArr(int arr[], int len, int target)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (arr[i] == target) return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char get_dozen(int number)
|
||||||
|
{
|
||||||
|
return (char)'0' + number % 10;
|
||||||
|
}
|
||||||
|
char get_units(int number)
|
||||||
|
{
|
||||||
|
return (char)'0' + number / 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
printf("hello world!\n");
|
||||||
|
|
||||||
|
//bst_node* head = create_bst_node("name", "phone");
|
||||||
|
bst_node* head = NULL;
|
||||||
|
|
||||||
|
int arr[COUNT_NUMBERS] = {0};
|
||||||
|
char name[NAME_LEN] = "name_xx";
|
||||||
|
char phone[PHONE_LEN] = "phone_xx";
|
||||||
|
int temp = 0;
|
||||||
|
for (int i = 0; i < COUNT_NUMBERS; i++)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
temp = rand() % 100;
|
||||||
|
}
|
||||||
|
while (isInArr(arr, i - 1, temp));
|
||||||
|
|
||||||
|
arr[i] = temp;
|
||||||
|
|
||||||
|
name[5] = get_dozen(temp);
|
||||||
|
name[6] = get_units(temp);
|
||||||
|
|
||||||
|
phone[6] = get_dozen(temp);
|
||||||
|
phone[7] = get_units(temp);
|
||||||
|
|
||||||
|
|
||||||
|
head = bst_insert(head, name, phone);
|
||||||
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n\ninorder traversal: \n");
|
||||||
|
bst_inorder_traversal(head);
|
||||||
|
|
||||||
|
printf("\n\npreorder traversal: \n");
|
||||||
|
bst_preorder_traversal(head);
|
||||||
|
|
||||||
|
char tar_name[NAME_LEN] = "name_44";
|
||||||
|
|
||||||
|
printf("\n\nУдаляем элемент с значением %s:\n", tar_name);
|
||||||
|
|
||||||
|
head = bst_delete(head, tar_name);
|
||||||
|
|
||||||
|
bst_inorder_traversal(head);
|
||||||
|
|
||||||
|
|
||||||
|
printf("\n\nВывод дерева:\n");
|
||||||
|
|
||||||
|
printTree(head, 0);
|
||||||
|
|
||||||
|
printf("\n\nОбход в ширину:\n");
|
||||||
|
treeLevelTraversal(head);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
delete_bst(head);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
40
stepushovgs/data-structures/sorce/swap.c
Normal file
40
stepushovgs/data-structures/sorce/swap.c
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int Partition_Hoa(int arr[], int l, int r)
|
||||||
|
{
|
||||||
|
int p = arr[(l + r) / 2];
|
||||||
|
int i = l;
|
||||||
|
int j = r;
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
// #print(p)
|
||||||
|
while (arr[i] <= p) i++;
|
||||||
|
while (arr[j] > p) j--;
|
||||||
|
|
||||||
|
if (i >= j) return j;
|
||||||
|
|
||||||
|
swap(arr[i], arr[j]);
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void QuickSort(int arr[], int l, int r)
|
||||||
|
{
|
||||||
|
if (l < r):
|
||||||
|
{
|
||||||
|
int s = Partition_Hoa(arr, l, r);
|
||||||
|
QuickSort(arr, l, s-1);
|
||||||
|
QuickSort(arr, s+1, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int arr[] = {2, 56, 10, 5, 2, 6, 9, 6, 3, 923, 3, 2, 1};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user