Skip to content

Instantly share code, notes, and snippets.

@romec512
Created April 3, 2018 13:49
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/7b91826920af6e284c2652ab9b19ffbf to your computer and use it in GitHub Desktop.
Save romec512/7b91826920af6e284c2652ab9b19ffbf to your computer and use it in GitHub Desktop.
Lab3
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <time.h>
struct QueueEl {
int data;
QueueEl* next;
};
struct Queue {
QueueEl* first;
QueueEl* last;
int count;
};
Queue create()
{
Queue *queue = NULL;
queue = (Queue*)malloc(sizeof(Queue));
queue->last = NULL;
queue->first = NULL;
queue->count = 0;
return *queue;
}
bool isEmpty(Queue *queue)
{
if (queue->count == 0)
{
return true;
}
else
{
return false;
}
}
void push(Queue *queue, int value)
{
QueueEl* current = (QueueEl*)malloc(sizeof(QueueEl));
current->data = value;
current->next = NULL;
if (queue->count == 0)
{
queue->last = current;
queue->first = queue->last;
}
else
{
queue->last->next = current;
}
queue->last = current;
queue->count++;
}
void pop(Queue *queue)
{
if (isEmpty(queue) == true)
{
//printf("Очередь пуста!\n");
return;
}
QueueEl *current = queue->first;
queue->first = queue->first->next;
free(current);
queue->count--;
}
void show(Queue *queue)
{
if (isEmpty(queue) == true)
{
printf("Очередь пуста!\n");
return;
}
QueueEl *current = queue->first;
while (current->next != 0)//поставить здесь корректное условие, чтобы учитывалось, что очередь может быть заполнена
{
printf("%c\n", current->data);
current = current->next;
}
printf("%c<-последний элемент\nНажмите 'enter', чтобы продолжить.\n", current->data);
}
void destroy(Queue *queue)
{
free(queue);
queue = NULL;
}
void main()
{
setlocale(LC_ALL, "rus");
Queue queue = create();
printf("Чтобы выйти из программы нажмите 'q'.\nДля продолжения нажмите 'enter'.\n");
srand(time(NULL));
while (getchar() != 'q')
{
int i = rand() % 25 + 65;
if (i % 2 == 0)
{
push(&queue, i);
}
else
{
pop(&queue);
}
show(&queue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment