diff --git a/stepushovgs/data-structures/sorce/linked_list.c b/stepushovgs/data-structures/sorce/linked_list.c new file mode 100644 index 0000000..04231ba --- /dev/null +++ b/stepushovgs/data-structures/sorce/linked_list.c @@ -0,0 +1,141 @@ +#include +#include +#include + +#include "linked_list.h" + +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* privious = HEAD; + Node* current = HEAD->next; + + while (current != NULL) + { + if (strcmp(target_name, current->name_) == 0) + { + privious->next = current->next; + break; + } + + privious = current; + current = current->next; + } + + 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]); + } +} + + + diff --git a/stepushovgs/data-structures/sorce/linked_list.h b/stepushovgs/data-structures/sorce/linked_list.h new file mode 100644 index 0000000..7fb4422 --- /dev/null +++ b/stepushovgs/data-structures/sorce/linked_list.h @@ -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); diff --git a/stepushovgs/data-structures/sorce/main.c b/stepushovgs/data-structures/sorce/main.c index 35f3250..8b73cdf 100644 --- a/stepushovgs/data-structures/sorce/main.c +++ b/stepushovgs/data-structures/sorce/main.c @@ -1,8 +1,44 @@ -#include +#include +#include +#include + +#include "linked_list.h" + +#define NAME_BUFF_SIZE 50 +#define PHONE_BUFF_SIZE 12+1 // +1 for end symbol int main() { - printf("hello world!\n"); + 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; } diff --git a/stepushovgs/data-structures/sorce/main.exe b/stepushovgs/data-structures/sorce/main.exe new file mode 100644 index 0000000..b7fdfff Binary files /dev/null and b/stepushovgs/data-structures/sorce/main.exe differ diff --git a/stepushovgs/data-structures/sorce/main.o b/stepushovgs/data-structures/sorce/main.o index 1537b41..e4e888a 100755 Binary files a/stepushovgs/data-structures/sorce/main.o and b/stepushovgs/data-structures/sorce/main.o differ