Skip to content

Instantly share code, notes, and snippets.

@romec512
Created April 1, 2018 18:38
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/02c760b1698c1c3a31664d5208f237d3 to your computer and use it in GitHub Desktop.
Save romec512/02c760b1698c1c3a31664d5208f237d3 to your computer and use it in GitHub Desktop.
Laba2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
struct Queue {
int data[3];
int first;
int last;
int count;
};
Queue create()
{
Queue *queue = NULL;
queue = (Queue*)malloc(sizeof(Queue));
queue->last = 0;
queue->first = 0;
queue->count = 0;
return *queue;
}
bool isEmpty(Queue *queue)
{
if (queue->count == 0)
{
return true;
}
else
{
return false;
}
}
bool isFull(Queue *queue)
{
if (queue->count == 3)
{
return true;
}
else
{
return false;
}
}
void push(Queue *queue, int value)
{
if (isFull(queue) == true)
{
printf("Очередь заполнена!\n");
return;
}
if (queue->last >= 3)
{
queue->last = 0;
}
queue->data[queue->last] = value;
queue->last++;
queue->count++;
}
void pop(Queue *queue)
{
if (isEmpty(queue) == true)
{
printf("Очередь пуста!\n");
return;
}
queue->data[queue->first] = NULL;
if (queue->first == 2)
{
queue->first = 0;
}
else
{
queue->first++;
}
queue->count--;
}
void show(Queue *queue)
{
if (isEmpty(queue) == true)
{
printf("Очередь пуста!\n");
return;
}
int i = queue->first;
while ((i != queue->last - 1))//поставить здесь корректное условие, чтобы учитывалось, что очередь может быть заполнена
{
printf("%d\n", queue->data[i]);
i = (i+1) % 3;
}
printf("%d\n", queue->data[queue->last-1]);
}
void destroy(Queue *queue)
{
free(queue);
queue = NULL;
}
void main()
{
setlocale(LC_ALL, "rus");
Queue queue = create();
int select = 0;
while (select != 4)
{
printf("1)Ввести значение.\n2)Удалить элемент.\n3)Вывод очереди.\n4)Выход.\n");
while (!scanf("%d", &select))
{
while (getchar() != '\n')
printf("Введите заново.\n");
}
if (select == 1)
{
printf("Введите значение:\n");
int value = 0;
while (!scanf("%d", &value))
{
while (getchar() != '\n')
printf("Введите заново.\n");
}
push(&queue, value);// функция push
system("pause");
system("cls");
}
else if (select == 2)
{
pop(&queue);
system("pause");
system("cls");
}
else if (select == 3)
{
show(&queue);
system("pause");
system("cls");
}
else if (select == 5)
{
destroy(&queue);
break;
}
}
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment