Queue

1. Write a C program to simulate the working of Messaging System in which a message is placed in a Queue by a Message Sender, a message is removed from the queue by a Message Receiver, which can also display the contents of the Queue.


//program starts here
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 20
struct msge{
     int front,rear;
     char msg[SIZE][25];
};
void insert(struct msge *s,char m[])
{
     if(s->front==((s->rear+1)%SIZE) && s->front==-1 )
     {
          printf("Q full\n");
     }
     else
     {
          s->rear = (s->rear+1)%SIZE;
          strcpy(s->msg[s->rear],m);
    }
}


void del(struct msge *s,char rec[])
{
     if(s->front==0 && s->rear==-1){
          printf("empty\n");
          exit(0);
     }
     else
     {
          strcpy(rec,s->msg[s->front]);

          if(s->front== s->rear)
          {
              s->front=0;
              s->rear=-1;
          }
          else
          {
              s->front=(s->front+1)%SIZE;
          }
     }
}
void display(struct msge *s)
{
     int i;
     if(s->front > s->rear)
    {
          for(i=s->front;i<SIZE;i++)
              printf("%s\t",s->msg[i]);
          for(i=0;i<=s->rear;i++)
              printf("%s\t",s->msg[i]);
          printf("\n");
     }
     else
     {
          for(i=s->front;i<=s->rear;i++)
              printf("%s\t",s->msg[i]);
          printf("\n");
     }
}
     main()
     {
          struct msge s;
          s.front=0;
          s.rear=-1;
          int i,ch,j,k,n;
          char ms[25],rec[25];
          ch=0;
          while(ch!=4)
          {
              printf("1.insert\t2.del\t3.display\t4.break\nENTER YOUR CHOICE\n");
              scanf("%d",&ch);
              switch(ch)
              {
                   case 1:printf("enter message\n");
                          scanf("%s",ms);
                          insert(&s,ms);
                          break;
                   case 2:del(&s,rec);
                          printf("deleted mwssage: %s\n",rec);
                          break;
                   case 3:display(&s);
                          break;
                   case 4: break;
              }
          }
     }


No comments:

Post a Comment