Skip to main content

Dynamic Implemenatation Of Linear Queue

Dynamic Implemenatation Of Linear Queue


CODE

πŸ‘‡

  1. #include <stdio.h>
  2. typedef struct node
  3. {
  4. int info;
  5. struct node *next;
  6. }NODE;

  7. NODE *front, *rear;

  8. void initq()
  9. {
  10. front=rear=NULL;
  11. }

  12. int isempty()
  13. {
  14. return (front==NULL);
  15. }

  16. void addq(int num)
  17. {
  18. NODE *nn;
  19. nn = (NODE*)malloc(sizeof(NODE));
  20. nn->info=num;
  21. nn->next=NULL;
  22. if(front == NULL)
  23. rear=front=nn;
  24. else
  25. {
  26. rear->next=nn;
  27. rear=nn;
  28. }  
  29. }

  30. int removeq()
  31. {
  32. int num;
  33. NODE *temp=front;
  34. num = front->info;
  35. front = front->next;
  36. free (temp);
  37. if(front==NULL)
  38. rear=NULL;
  39. return (num);
  40. }


  41. void main()
  42. {
  43. int choice, num;
  44. initq();
  45. printf(">>Dynamic Implementation Of Linear Queue<<");
  46. do
  47. {
  48. printf("\n1.ADD \n2.DELETE \n3.EXIT\n");
  49. printf("Enter Your Choice:\t");
  50. scanf("%d", &choice);
  51. switch(choice)
  52. {
  53. case 1:
  54. printf("Enter The Element:\t");
  55. scanf("%d",&num);
  56. addq(num);
  57. break;
  58. case 2:
  59. if(isempty())
  60. printf("\n>>Queue Underflow<<");
  61. else
  62. printf("The Removed Element is:\t %d \n", removeq());
  63. break;
  64. case 3:
  65. printf("\t>>END<<");
  66. }
  67. }while(choice!=3);
  68. }

πŸ‘‰ExecuteπŸ‘ˆ

//Output
/*
>>Dynamic Implementation Of Linear Queue<<
1.ADD
2.DELETE
3.EXIT
Enter Your Choice:      1
Enter The Element:      5

1.ADD
2.DELETE
3.EXIT
Enter Your Choice:      2
The Removed Element is:  5

1.ADD
2.DELETE
3.EXIT
Enter Your Choice:      4
        >>Enter Right Choice<<
1.ADD
2.DELETE
3.EXIT
Enter Your Choice:      3
        >>END<<
*/




                                                                      //ThE ProRessoR

Comments