Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma hdrstop
- #pragma argsused
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef char _TCHAR;
- #define _tmain main
- struct Node {
- int num;
- struct Node* next;
- };
- struct Node* head = NULL;
- void freeList() {
- struct Node* temp;
- while (head != NULL) {
- temp = head;
- head = head->next;
- free(temp);
- }
- printf("Список очищен.\n");
- }
- void appendInList(int num) {
- struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
- struct Node* last = head;
- new_node->num = num;
- new_node->next = NULL;
- // если пустой, список в голову
- if (head == NULL) {
- head = new_node;
- return;
- }
- // проход по списку
- while (last->next != NULL) {
- last = last->next;
- }
- // переход на новый узел
- last->next = new_node;
- }
- void inputBase() {
- srand(time(NULL));
- for (int i = 0; i < 20; i++) {
- int random_number = rand() % 100 + 1;
- appendInList(random_number);
- }
- }
- void checkBase() {
- struct Node* current = head;
- if (current == NULL) {
- printf("База данных пуста.\n");
- return;
- }
- printf("Вывод чисел:");
- while (current != NULL) {
- printf("%d ", current->num);
- current = current->next;
- }
- }
- void outputBase() {
- struct Node* current = head;
- struct Node* lastEven = NULL;
- while (current != NULL) {
- if (current->num % 2 == 0) {
- lastEven = current;
- }
- current = current->next;
- }
- current = head;
- struct Node* new_node;
- while (current != NULL) {
- if (current->num % 2 != 0) {
- new_node = (struct Node*)malloc(sizeof(struct Node));
- new_node->num = lastEven->num;
- new_node->next = current->next;
- current->next = new_node;
- current = new_node->next;
- } else {
- current = current->next;
- }
- }
- checkBase();
- }
- void chooseProc() {
- int choose;
- printf("\n1: Посмотреть базу\n");
- printf("2: Вывести (по заданию)\n");
- printf("3: Выход\n");
- scanf("%d", &choose);
- switch (choose) {
- case 1:
- checkBase();
- break;
- case 2:
- outputBase();
- break;
- case 3:
- exit(0);
- break;
- default:
- printf("Ошибка\n");
- break;
- ;
- }
- chooseProc();
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- system("chcp 1251");
- inputBase();
- chooseProc();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement