Skip to content

Instantly share code, notes, and snippets.

@andermirik
Last active November 24, 2017 19:46
Show Gist options
  • Save andermirik/79324351c9833cf17b417bfdc1baf955 to your computer and use it in GitHub Desktop.
Save andermirik/79324351c9833cf17b417bfdc1baf955 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include "stdlib.h"
#include "locale.h"
struct Unit {
int key;
Unit *next;
};
float read_el(char *s = "") {
int k;
float r;
do {
do {
printf(s);
k = scanf_s("%f", &r);
while (getchar() != '\n');
} while (k != 1); k = 0;
} while (r < 0);
return r;
}
struct Unit * create(Unit **first) {
//return NULL;
*first = new Unit;
printf("ввод элементов структуры|\n");
(*first)->key = read_el("el: ");
(*first)->next = NULL;
Unit *end = *first;
int flag;
printf("выход: 0|");
flag = read_el();
if (!flag)
return *first;
flag = 0;
do
{
end->next = new Unit;
end = end->next;
end->key = read_el("el: ");
end->next = NULL;
printf("выход: 0|");
flag = read_el();
} while (flag);
return end;
}
void print(Unit *unit) {
Unit *print = unit;
while (print) {
printf("%d -> ", print->key);
print = print->next;
}
printf("NULL\n");
}
void doLast(Unit **first, int x, Unit **end) {
if ((*end) == NULL || *first == NULL) {
*first = new Unit;
(*first)->key = x;
(*first)->next = NULL;
*end = *first;
return;
}
if ((*end)->key == x)
return;
if ((*first)->next == NULL) {
(*end)->next = (Unit*)malloc(sizeof(Unit));
*end = (*end)->next;
(*end)->key = x;
(*end)->next = NULL;
return;
}
Unit *unit = *first;
if (unit->key == x)
{
(*end)->next = unit;
*first = unit->next;
(*end)->next->next = NULL;
return;
}
while (unit->next)
{
if (unit->next->key == x)
{
(*end)->next = unit->next;
(*end) = (*end)->next;
unit->next = (*end)->next;
(*end)->next = NULL;
return;
}
unit = unit->next;
}
(*end)->next = (Unit*)malloc(sizeof(Unit));
*end = (*end)->next;
(*end)->key = x;
(*end)->next = NULL;
}
void main()
{
setlocale(LC_ALL, "rus");
int x;
Unit *first = NULL;
Unit * end = create(&first);
print(first);
x = read_el("X: ");
doLast(&first, x, &end);
print(first);
}
//удалить и поставить в конец списка
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment