-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Message_Queue.c
117 lines (94 loc) · 2.11 KB
/
3_Message_Queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5
struct queue
{
int front, rear;
char data[SIZE][20];
};
typedef struct queue QUEUE;
void send(QUEUE *q, char item[20])
{
if (q->front == (q->rear + 1) % SIZE)
printf("\n Queue full");
else
{
q->rear = (q->rear + 1) % SIZE;
strcpy(q->data[q->rear], item);
if (q->front == -1)
q->front = 0;
}
}
char *receive(QUEUE *q)
{
char *del;
if (q->front == -1)
{
printf("\n Queue empty");
return NULL;
}
else
{
del = q->data[q->front];
if (q->front == q->rear)
{
q->front = -1;
q->rear = -1;
}
else
{
q->front = (q->front + 1) % SIZE;
}
return del;
}
}
void display(QUEUE q)
{
int i;
if (q.front == -1)
printf("\n Queue Empty");
else
{
printf("\n Queue content are\n");
for (i = q.front; i != q.rear; i = (i + 1) % SIZE)
printf("%s\n", q.data[i]);
printf("%s\n", q.data[i]);
}
}
int main()
{
int ch;
char *del;
char item[20];
QUEUE q;
q.front = -1;
q.rear = -1;
for (;;)
{
printf("\n1. Send\n2. Receive\n3. Display\n4. Exit");
printf("\nRead Choice :");
scanf("%d", &ch);
getchar();
switch (ch)
{
case 1:
printf("\n Read msg to be send :");
fgets(item, sizeof(item), stdin);
item[strcspn(item, "\n")] = '\0'; // Remove trailing newline
send(&q, item);
break;
case 2:
del = receive(&q);
if (del != NULL)
printf("\n Element deleted is %s\n", del);
break;
case 3:
display(q);
break;
default:
exit(0);
}
}
return 0;
}