Created
November 23, 2017 17:38
-
-
Save andermirik/d56000ba01c109fc9d70205173dce587 to your computer and use it in GitHub Desktop.
удалить и поставить в конец списка
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include "stdlib.h" | |
#include "locale.h" | |
struct Unit { | |
int key; | |
Unit *next; | |
}; | |
float read_f(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) { | |
*first = new Unit; | |
printf("ввод элементов структуры|\n"); | |
(*first)->key = read_f("el: "); | |
(*first)->next = NULL; | |
Unit *end = *first; | |
int flag; | |
printf("выход: 0|"); | |
flag = read_f(); | |
if (!flag) | |
return *first; | |
flag = 0; | |
do | |
{ | |
end->next = new Unit; | |
end = end->next; | |
end->key = read_f("el: "); | |
end->next = NULL; | |
printf("выход: 0|"); | |
flag = read_f(); | |
} 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)->key == x || first == NULL) | |
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; | |
} | |
*first = unit; | |
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_f("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