Compare commits

..

7 Commits
main ... lab1

Author SHA1 Message Date
62e1b34fd0 Setup gitignore for data-structure 2026-04-20 11:03:49 +03:00
10f35b3ac3 update gitignore for c 2026-04-19 00:33:46 +03:00
d9d935c0bf implementing a linked list
- insert
- delete
- find
- listAll
2026-04-19 00:31:51 +03:00
c9947a713f move gitignore 2026-04-16 22:27:29 +03:00
10445d4940 add gitignore 2026-04-16 22:23:45 +03:00
5e85fa060b init lab1 2026-04-16 22:21:02 +03:00
7fec6872a1 [0] initial commit 2026-02-14 11:41:46 +03:00
7 changed files with 273 additions and 0 deletions

55
.gitignore vendored
View File

@ -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
View File

@ -0,0 +1 @@
*.o

1
stepushovgs/427 Normal file
View File

@ -0,0 +1 @@
427

View File

@ -0,0 +1,2 @@
.exe
.o

View File

@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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]);
}
}

View 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);

View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "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;
}