This is a C program which implements queue using linklist.


Source Code:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. //A structure is created for the node in queue

  5. struct queu
  6. {
  7. int info;
  8. struct queu *next;//Next node address
  9. };
  10. typedef struct queu *NODE;
  11. //This function will push an element into the queue

  12. NODE push(NODE rear)
  13. {
  14. NODE NewNode;
  15. //New node is created to push the data
  16. NewNode=(NODE)malloc(sizeof(struct queu));
  17. printf ("\nEnter the no to be pushed = ");

  18. scanf ("%d",&NewNode->info);
  19. NewNode->next=NULL;
  20. //setting the rear pointer
  21. if (rear != NULL)
  22. rear->next=NewNode;
  23. rear=NewNode;
  24. return(rear);
  25. }
  26. //This function will pop the element from the queue

  27. NODE pop(NODE f,NODE r)
  28. {
  29. //The Queue is empty when the front pointer is NULL

  30. if(f==NULL)
  31. printf ("\nThe Queue is empty");
  32. else
  33. {
  34. printf ("\nThe poped element is = %d",f->info);

  35. if(f != r)
  36. f=f->next;
  37. else
  38. f=NULL;
  39. }
  40. return(f);
  41. }
  42. //Function to display the element of the queue

  43. void traverse(NODE fr,NODE re)
  44. {
  45. //The queue is empty when the front pointer is NULL

  46. if (fr==NULL)
  47. printf ("\nThe Queue is empty");
  48. else
  49. {
  50. printf ("\nThe element(s) is/are = ");

  51. while(fr != re)
  52. {
  53. printf("%d ",fr->info);
  54. fr=fr->next;
  55. };
  56. printf ("%d",fr->info);
  57. }
  58. }
  59. int main()
  60. {
  61. int choice;
  62. char option;
  63. //declaring the front and rear pointer
  64. NODE front, rear;
  65. //Initializing the front and rear pointer to NULL

  66. front = rear = NULL;
  67. do
  68. {
  69. //clrscr();
  70. printf ("1. Push\n");
  71. printf ("2. Pop\n");
  72. printf ("3. Traverse\n");
  73. printf ("\n\nEnter your choice = ");

  74. scanf ("%d",&choice);
  75. switch(choice)
  76. {
  77. case 1:
  78. //calling the push function
  79. rear = push(rear);
  80. if (front==NULL)
  81. {
  82. front=rear;
  83. }
  84. break;
  85. case 2:
  86. //calling the pop function by passing
  87. //front and rear pointers
  88. front = pop(front,rear);
  89. if (front == NULL)
  90. rear = NULL;
  91. break;
  92. case 3:
  93. traverse(front,rear);
  94. break;
  95. }
  96. printf ("\n\nPress (Y/y) to continue = ");

  97. fflush(stdin);
  98. scanf ("%c",&option);
  99. }while(option == 'Y' || option == 'y');
  100. }

Output:

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 25

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 1

 

Enter the no to be pushed = 36

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 2

 

The poped element is = 14

 

Press (Y/y) to continue = Y

1. Push

2. Pop

3. Traverse

 

Enter your choice = 3

 

The element(s) is/are = 25 36

 

Press (Y/y) to continue = N

 

0 comments:

Post a Comment