forked from UNN/2026-rff_mp
implementing a linked list
- insert - delete - find - listAll
This commit is contained in:
parent
c9947a713f
commit
d9d935c0bf
141
stepushovgs/data-structures/sorce/linked_list.c
Normal file
141
stepushovgs/data-structures/sorce/linked_list.c
Normal 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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
29
stepushovgs/data-structures/sorce/linked_list.h
Normal file
29
stepushovgs/data-structures/sorce/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);
|
||||
|
|
@ -1,8 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
BIN
stepushovgs/data-structures/sorce/main.exe
Normal file
BIN
stepushovgs/data-structures/sorce/main.exe
Normal file
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user