Friday, January 24, 2020

Posted on Leave a Comment

Dynamic queue linked list of characters

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

– Insert
– Delete
– Display
– Exit


Dynamic Queue Linked List

Dynamic queue linked list Source Code


/*program for dynamic implementation of queue of characters */ #include

#include

struct node {
  char info;
  struct node * next;
}* front, * rear;
void enqueue(char elt);
int dequeue();
void display();

void main() {
  int ch;
  char elt;
  clrscr();
  rear = NULL;
  front = NULL;
  do {
    printf("\n ** ** ** ** ** ** Menu ** ** ** ** ** ** ** * ");
    printf("\nEnter: \n1 - > Insert\ n2 - > Delete\ n3 - > Display\ n4 - > Exit\ n");
    printf("Enter your choice::");
    scanf(" % d", & ch);
    switch (ch) {
    case 1:
      printf("Enter The character\ n");
      flushall();
      scanf(" % c", & elt);
      enqueue(elt);
      break;
    case 2:
      elt = dequeue();
      printf("The deleted element = % c\ n", elt);
      break;
    case 3:
      display();
      break;
    default:
      printf("~~~Exit~~~");
      getch();
      exit(0);
      break;
    }
  } while (ch != 4);
}
void enqueue(char elt) {
  struct node * p;
  p = (struct node * ) malloc(sizeof(struct node));
  p - > info = elt;
  p - > next = NULL;
  if (rear == NULL || front == NULL)
    front = p;
  else
    rear - > next = p;
  rear = p;
}
int dequeue() {
  struct node * p;
  int elt;
  if (front == NULL || rear == NULL) {
    printf("\nUnder Flow");
    getch();
    exit(0);
  } else {
    p = front;
    elt = p - > info;
    front = front - > next;
    free(p);
  }
  return (elt);
}
void display() {
  struct node * t;
  t = front;
  while (front == NULL || rear == NULL) {
    printf("\nQueue is empty");
    getch();
    exit(0);
  }
  while (t != NULL) {
    printf("- > % c", t - > info);
    t = t - > next;
  }
}

Dynamic queue linked list OUTPUT


************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter The character
a
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter The character
b
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 1
Enter The character
c
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 3
->a->b->c
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 2
The deleted element = a
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice :: 3
->b->c
************ Menu ***************
Enter:
1->Insert
2->Delete
3->Display
4->Exit
Enter your choice ::4

Download Source Code

Download Source code


0 comments:

Post a Comment

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