Created
April 3, 2018 13:52
-
-
Save romec512/9327d936fcda8b7e71d1605b16a2309a to your computer and use it in GitHub Desktop.
Lab4
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
#define _CRT_SECURE_NO_WARNINGS | |
#include <stdio.h> | |
#include <locale.h> | |
#include <stdlib.h> | |
const int N = 10; | |
struct List | |
{ | |
int mass[N]; | |
int count; | |
}; | |
int search(List *list, int current) | |
{ | |
if (list->count == 0) | |
{ | |
return -2; | |
} | |
for (int i = 0; i < list->count; i++) | |
{ | |
if (list->mass[i] >= current) | |
{ | |
return i; | |
} | |
} | |
return -1; | |
} | |
int searchDeleted(List *list, int current) | |
{ | |
if (list->count == 0) | |
{ | |
return -2; | |
} | |
for (int i = 0; i < list->count; i++) | |
{ | |
if (list->mass[i] == current) | |
{ | |
return i; | |
} | |
} | |
return -1; | |
} | |
void push(List *list) | |
{ | |
if (list->count == N) | |
{ | |
printf("Список заполнен!\n"); | |
return; | |
} | |
int inf; | |
printf("Введите информационную часть:\n"); | |
scanf("%d", &inf); | |
if (list->count != 0) | |
{ | |
int _search = search(list, inf); | |
if (_search == -1) | |
{ | |
list->mass[list->count] = inf; | |
list->count++; | |
return; | |
} | |
for (int i = list->count; i > _search - 1; i--) | |
{ | |
list->mass[i] = list->mass[i - 1]; | |
} | |
list->mass[_search] = inf; | |
} | |
else if (list->count == 0) | |
{ | |
list->mass[0] = inf; | |
} | |
list->count++; | |
} | |
void pop(List *list, int current) | |
{ | |
if (list->count == 0) | |
{ | |
printf("Список пуст!\n"); | |
return; | |
} | |
int _search = searchDeleted(list, current); | |
if (_search == -1) | |
{ | |
printf("Элемент не найден.\n"); | |
return; | |
} | |
for (int i = _search; i < list->count-1; i++) | |
{ | |
list->mass[i] = list->mass[i + 1]; | |
} | |
list->count--; | |
} | |
void show(List *list) | |
{ | |
if (list->count == 0) | |
{ | |
printf("Список пуст.\n"); | |
return; | |
} | |
for (int i = 0; i < list->count; i++) | |
{ | |
printf("%d\n", list->mass[i]); | |
} | |
} | |
void main() | |
{ | |
setlocale(LC_ALL, "rus"); | |
List *list = (List*)malloc(sizeof(List)); | |
list->count = 0; | |
int select = 0; | |
while (select != 5) | |
{ | |
printf("1)Добавить элемент.\n2)Удалить элемент.\n3)Вывод.\n4)Найти.\n5)Выход.\n"); | |
scanf("%d", &select); | |
if (select == 1) | |
{ | |
push(list); | |
system("pause"); | |
} | |
else if (select == 2) | |
{ | |
int current; | |
printf("Введите элемент, который хотите удалить.\n"); | |
scanf("%d", ¤t); | |
pop(list, current); | |
system("pause"); | |
} | |
else if (select == 3) | |
{ | |
show(list); | |
system("pause"); | |
} | |
else if (select == 4) | |
{ | |
int current; | |
printf("Введите элемент, который хотите найти.\n"); | |
scanf("%d", ¤t); | |
int _search = searchDeleted(list, current); | |
if (_search >= 0) | |
{ | |
printf("%d\n", _search); | |
} | |
else if(_search == -2){ | |
printf("Список пуст.\n"); | |
} | |
else { | |
printf("Не найдено.\n"); | |
} | |
system("pause"); | |
} | |
else if (select == 5) | |
{ | |
break; | |
} | |
system("cls"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment