-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging_system_new.c
125 lines (104 loc) · 1.74 KB
/
messaging_system_new.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
118
119
120
121
122
123
124
125
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define capacity 1000
#define length 1000
typedef struct queue
{
char arr[capacity][length];
int front;
int size;
}queue;
void create(queue *q)
{
q->front=0;
q->size=0;
}
int getFront(queue *q)
{
if(q->size==0)
return -1;
return q->front;
}
int getRear(queue *q)
{
if(q->size==0)
return -1;
return (q->front+q->size-1)%capacity;
}
void enqueue(queue *q,char a[])
{
if(q->size==capacity)
{
printf("Message Buffer Full\n");
return;
}
int rear=getRear(q);
rear=(rear+1)%capacity;
strcpy(q->arr[rear],a);
q->size++;
}
void dequeue(queue *q)
{
if(q->size==0)
{
printf("Message Buffer Empty\n");
return;
}
q->front=(q->front+1)%capacity;
q->size--;
}
void read(queue *q)
{
if(q->size==0)
{
printf("No messages\n");
return;
}
printf("%s\n",q->arr[q->front]);
}
int main()
{
printf("________________Welcome to the messaging system____________________\n");
int opt;
queue q;
create(&q);
int o;
char mssg[length];
do
{
printf("Enter the choice\n");
printf("1.send message\t2.read message\t3.exit\n");
scanf("%d",&opt);
getchar();
switch(opt)
{
case 1:
printf("Enter the message\n");
scanf("%[^\n]",mssg);
enqueue(&q,mssg);
break;
case 2:
printf("You have %d new messages\n",q.size);
printf("Enter 1.For oldest one\t2.All message");
scanf("%d",&o);
if(o==1)
{
read(&q);
dequeue(&q);
}
else
{
while(q.size>0)
{
read(&q);
dequeue(&q);
}
}
break;
case 3:
break;
}
}while(opt!=3);
return 0;
}