Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAXVAL 100
- int to_int(char array) {
- return ((int)array - '0');
- }
- typedef struct Node {
- unsigned char name[MAXVAL];
- unsigned char address[MAXVAL];
- int number[MAXVAL];
- struct Node *next;
- }Node;
- typedef struct {
- Node *head;
- } list;
- void add_new(Node **head) {
- Node *tmp = (Node*)malloc(sizeof(Node));
- unsigned char name[MAXVAL];
- unsigned char address[MAXVAL];
- char number[MAXVAL];
- printf("name:");
- gets_s(name, MAXVAL);
- strcpy(tmp->name, name);
- printf("address:");
- gets_s(address, MAXVAL);
- strcpy(tmp->address, address);
- printf("number:");
- gets_s(number, MAXVAL);
- int i = 0;
- while (1) {
- if (number[i] == '\0') break;
- tmp->number[i] = to_int(number[i]);
- i++;
- }
- tmp->next = (*head);
- (*head) = tmp;
- }
- void list_foreach(list *list, void(*function)(Node** head)) //применаем функцию function к каждому узлу списка
- {
- Node *tmp = list->head;
- while (tmp!= NULL) {
- function(&tmp);
- tmp = tmp->next;
- }
- }
- int list_find(Node* list, int(*cmp_func)(char*, char*),unsigned char* value) { // возвращает тот узел, для которого результат функции сравнения равен нулю
- int i = 0,counter=0;
- while (list!= NULL) {
- if ((*cmp_func)(value, list->name) == 0) {
- printf("name:");
- puts(list->name);
- printf("address:");
- puts(list->address);
- printf("number:");
- while (list->number[i] >= 0) {
- printf("%d", list->number[i++]);
- }
- printf("\n");
- return counter;
- }
- else {
- list = list->next;
- counter++;
- }
- }
- return -1;
- }
- int cmp(const unsigned char* a,const unsigned char* b) {
- return strcmp(a, b);
- }
- void print_all(Node** head) {
- int i = 0;
- printf("\n");
- printf("name:");
- puts((*head)->name);
- printf("address:");
- puts((*head)->address);
- printf("number:");
- while ((*head)->number[i] >= 0) {
- printf("%d", (*head)->number[i++]);
- }
- printf("\n");
- }
- Node* getNth(Node* head, int n) {
- int counter = 1;
- while (counter < n && head) {
- head = head->next;
- counter++;
- }
- return head;
- }
- void deleteNth(Node **head, int n) {
- if (n == 0) {
- Node *tmp = *head;
- (*head) = (*head)->next;
- free(tmp);
- }
- else {
- Node *prev = getNth(*head, n - 1);
- Node *elm = prev->next;
- prev->next = elm->next;
- free(elm);
- }
- printf("deleted");
- }
- int main(int argc, char** argv) {
- char buffer[MAXVAL];
- Node* head = malloc(sizeof(Node));
- head = NULL;
- list* tmp = malloc(sizeof(list));
- if (argc > 1 && strcmp(argv[1], "-i") != 0) {
- printf("neizvestnyi kluch");
- return 0;
- }
- if (argc == 2 && !strcmp(argv[1], "-i")) { //если введён ключ -i выполнять программу до ввода exit
- while (1) {
- gets_s(buffer, MAXVAL);
- if (strcmp(buffer, "exit") == 0)
- return 0;
- if (strcmp(buffer, "add") == 0)
- add_new(&head);
- if (strcmp(buffer, "printall") == 0) {
- tmp->head = head;
- list_foreach(tmp, &print_all);
- }
- if (strcmp(buffer, "find") == 0) {
- char example[MAXVAL];
- gets_s(example, MAXVAL);
- list_find(head, &cmp, example);
- }
- if (strcmp(buffer, "delete") == 0) {
- tmp->head = head;
- char example[MAXVAL];
- gets_s(example, MAXVAL);
- int position = list_find(head, &cmp, example);
- deleteNth(&head, position);
- }
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement