Skip to content

Instantly share code, notes, and snippets.

@romec512
Created April 3, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romec512/9327d936fcda8b7e71d1605b16a2309a to your computer and use it in GitHub Desktop.
Save romec512/9327d936fcda8b7e71d1605b16a2309a to your computer and use it in GitHub Desktop.
Lab4
#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", &current);
pop(list, current);
system("pause");
}
else if (select == 3)
{
show(list);
system("pause");
}
else if (select == 4)
{
int current;
printf("Введите элемент, который хотите найти.\n");
scanf("%d", &current);
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