Friday, January 24, 2020

Posted on Leave a Comment

Static implementation of circular queue for characters

Write a menu driven program using ‘C’ for static implementation of Circular Queue for characters. The menu includes



 – Insert

 – Delete

 – Display

 – Exit


Source Code

#include
#define max 5
int front, rear;
char q[max];
void enqueue(void);
void dequeue(void);
void qdisplay(void);
void main(void) {
  int c;
  clrscr();
  front = rear = -1;
  do {
    printf(“1: insert\ n2: deletion\ n3: display\ n4: exit\ nenter choice: ”);
    scanf(“ % d”, & c);
    switch (c) {
    case 1:
      enqueue();
      break;
    case 2:
      dequeue();
      break;
    case 3:
      qdisplay();
      break;
    case 4:
      printf(“pgm ends\ n”);
      break;
    default:
      printf(“wrong choice\ n”);
      break;
    }
  } while (c != 4);
  getch();
}
void enqueue(void) {
  char x;
  if ((front == 0 && rear == max - 1) || (front == rear + 1)) //condition for full Queue
  {
    printf(“Queue is overflow\ n”);
    return;
  }
  if (front == -1) {
    front = rear = 0;
  } else {
    if (rear == max - 1) {
      rear = 0;
    } else {
      rear++;
    }
  }
  printf(“enter the character\ n”);
  flushall();
  scanf(“ % c”, & x);
  q[rear] = x;
  printf(” % c succ.inserted\ n”, x);
  return;
}
void dequeue(void) {
  char y;
  if (front == -1) {
    printf(“q is underflow\ n”);
    return;
  }
  y = q[front];
  if (front == rear) {
    front = rear = -1;
  } else {
    if (front == max - 1) {
      front = 0;
    } else {
      front++;
    }
  }
  printf(“ % c succ.deleted\ n”, y);
  return;
}
void qdisplay(void) {
  int i, j;
  if (front == rear == -1) {
    printf(“q is empty\ n”);
    return;
  }
  printf(“elements are: \n”);
  for (i = front; i != rear; i = (i + 1) % max) {
    printf(“ % c\ n”, q[i]);

  }

  printf(“ % c\ n”, q[rear]);
  return;
}



OUTPUT:


1:insert

2:deletion

3:display

4:exit

enter choice:1

enter the no:

a

a succ. inserted

1:insert

2:deletion

3:display

4:exit

enter choice:1

enter the no:

b

b succ. inserted

1:insert

2:deletion

3:display

4:exit

enter choice:1

enter the no:

c

c succ. inserted

1:insert

2:deletion

3:display

4:exit

enter choice:3

elements are :

a

b

c

1:insert

2:deletion

3:display

4:exit

enter choice:2

a succ. deleted

1:insert

2:deletion

3:display

4:exit

enter choice:3

elements are :

b

c

1:insert

2:deletion

3:display

4:exit

enter choice:4


0 comments:

Post a Comment

Codearea.in is featured by projectsgeek.com. Powered by Blogger.