Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.1 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#include <ctype.h>


struct queue {
	char qu[100];
	int head, tail;
}; *q;

void init(struct queue* q) {
	q->head = 1;
	q->tail = 0;
	return;
}

void insert(struct queue* q, char x) {
	if (q->tail < 99) {
		q->tail++;
		q->qu[q->tail] = x;
	}
	else {
		printf("Queue is crowded\n");
	}
	return;
}

int isempty(struct queue* q) {
	if (q->tail < q->head) {
		return 1;
	}
	else {
		return 0;
	}
}

void print(struct queue* q) {
	int h;
	if (isempty(q) == 1) {
		printf("Queue is empty\n");
		return;
	}
	for (h = q->head; h <= q->tail; h++) {
		printf("%c ", q->qu[h]);
	}
	return;
}

int main() {
	int n;
	char b;
	printf("Max elements of queue: ");
	scanf_s("%d", &n);

	printf("Enter the referens: ");
	rewind(stdin);
	scanf_s("%c", &b);
	

	struct queue* q;
	q = (struct queue*)malloc(sizeof(struct queue));

	init(q);
	print(q);

	for (int i = 0; i < n; i++) {
		char s;
		printf("Enter elements of queue:\n");
		rewind(stdin);
		scanf_s("%c ", &s);
		insert(q, s);
	} 
	printf("\Your queue: \n");
	print(q);

	return 0;
}