Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 6/main_sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "sorted_list.h"

void main(int argc, char* argv[])
{
List list;
initList(&list);

printf("0 – выйти\n1 – добавить значение в сортированный список\n2 – удалить значение из списка\n3 – распечатать список\n\n");

char inp;
printf("Введите команду: ");
scanf("%c", &inp);

while (inp != '0') {
int value;
switch (inp) {
case '1':
printf("Добавить: ");
scanf("%d", &value);

addValue(&list, value);

break;

case '2':
printf("Удалить: ");
scanf("%d", &value);

removeValue(&list, value);

break;

case '3':
printf("Список: ");
printList(&list);
break;
default:
printf("Введите команду: ");
break;
}
scanf("%c", &inp);
}
}
106 changes: 106 additions & 0 deletions 6/sorted_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "sorted_list.h"

void initList(List* list)
{
(*list).head = NULL;
(*list).size = 0;
}

Node* createNode(int value, Node* next)
{
Node* node = malloc(sizeof(Node));
if (node == NULL) {
printf("Ошибка выделения памяти\n");
exit(-1);
}

(*node).next = next;
(*node).value = value;

return node;
}

void addValue(List* list, int value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, эту функцию можно сделать короче. И removeValue тоже.

{
Node* head = (*list).head;

if (head == NULL) {
Node* node = createNode(value, NULL);

(*list).head = node;
(*list).size++;
return;
}

Node* buff = NULL;

while (head->value < value) {
if ((*head).next == NULL) {
Node* node = createNode(value, NULL);

(*head).next = node;

(*list).size++;
return;
}

buff = head;
head = (*head).next;
}

Node* node = createNode(value, head);

(*list).size++;

if (buff != NULL) {
(*buff).next = node;
}

else {
(*list).head = node;
}
}

void removeValue(List* list, int value)
{
Node* iter = (*list).head;

if (iter == NULL) {
return;
}

Node* buff = NULL;

while (iter != NULL) {
int listValue = (*iter).value;
if (listValue == value) {
if (buff != NULL) {
(*buff).next = (*iter).next;
}

else {
(*list).head = (*iter).next;
}
Comment on lines +77 to +83

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Форматирование должно быть не такое


free(iter);
(*list).size--;

iter = (*list).head;
continue;
}

buff = iter;
iter = (*iter).next;
}
}

void printList(List* list)
{
Node* iter = (*list).head;
while (iter != NULL) {
int value = (*iter).value;
printf("%d ", value);
iter = (*iter).next;
}
printf("\n");
}
22 changes: 22 additions & 0 deletions 6/sorted_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>

struct Node {
int value;
struct Node* next;

} typedef Node;

struct List {
Node* head;
int size;
} typedef List;

void initList(List* list);
Node* createNode(int value, Node* next);

void addValue(List* list, int value);
void removeValue(List* list, int value);

void printList(List* list);