Queue size:5
in 101
in 102
in 103
In 104
In 105
In 106
calling
calling
calling
Calling
Calling
Calling
Empty, No one
#
101 joined, total 1
102 joined, total 2
103 joined, total 3
104 joined, total 4
105 joined, total 5
Full, 106 rejected
101 called, total 4
102 called, total 3
103 called, total 2
104 called, total 1
105 called, total 0
Empty, No one
#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxSize 20
typedef struct
{
int data[maxSize];
int rear;
int front;
int len;
int size;
} Queue;
void enQueue(Queue *q, int data)
{
if ((q->rear + 1) % q->size == q->front)
{
printf("Full, %d rejected\n", data);
return;
}
q->rear = (q->rear + 1) % q->size;
q->data[q->rear] = data;
q->len++;
printf("%d joined, Total %d\n", data, q->len);
}
void deQueue(Queue *q)
{
if (q->rear == q->front)
{
printf("Empty, No one\n");
return;
}
q->front = (q->front + 1) % q->size;
q->len--;
printf("%d called, total %d\n", q->data[q->front], q->len);
}
int main(void)
{
int size, id;
char keywords[10];
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = 0;
q->rear = 0;
q->len = 0;
printf("Queue size:");
scanf("%d", &size);
q->size = (size + 1);
while (1)
{
scanf("%s", &keywords);
if (!strcmp("In", keywords) || !strcmp("in", keywords))
{
scanf("%d", &id);
enQueue(q, id);
}
if (!strcmp("Calling", keywords) || !strcmp("calling", keywords))
{
deQueue(q);
}
if (!strcmp("#", keywords))
{
free(q);
return 0;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int data;
struct Node *next;
} Node;
typedef struct
{
Node *rear;
Node *front;
int len;
int size;
} Queue;
void enQueue(Queue *q, int data)
{
if (q->len == q->size)
{
printf("%d rejected\n", data);
return;
}
Node *node = (Node *)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (q->len)
{
q->rear->next = node;
q->rear = node;
}
else
{
q->rear = node;
q->front = node;
}
q->len++;
printf("%d joined, total %d\n", data, q->len);
return;
}
void deQueue(Queue *q)
{
if (!q->len)
{
printf("Empty, No one\n");
return;
}
int res = q->front->data;
q->front = q->front->next;
q->len--;
printf("%d called, Total %d\n", res, q->len);
}
int main(void)
{
int id;
char keywords[10];
Queue *q = (Queue *)malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
q->len = 0;
printf("Queue size:");
scanf("%d", &q->size);
while (1)
{
scanf("%s", &keywords);
if (!strcmp("In", keywords) || !strcmp("in", keywords))
{
scanf("%d", &id);
enQueue(q, id);
}
if (!strcmp("Calling", keywords) || !strcmp("calling", keywords))
{
deQueue(q);
}
if (!strcmp("#", keywords))
{
free(q);
return 0;
}
}
}