SAADQUAMER

queue

Dec 9th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6. int a;
  7. struct node *next;
  8. } node;
  9.  
  10. node* top=NULL,*head=NULL;
  11.  
  12. void enqueue(int x)
  13. {
  14. node*N=(node*)malloc(sizeof(node));
  15. N->a=x;
  16. N->next=NULL;
  17. if(top==NULL)
  18. {
  19. top=N;
  20. head=N;
  21. }
  22. else
  23. {
  24. node *list=top;
  25. while(list->next!=NULL)
  26. {
  27. list=list->next;
  28. }
  29. list->next=N;
  30. top=N;
  31. }
  32. }
  33. int dequeue()
  34. {
  35. int data=0;
  36. node *temp;
  37. temp = head;
  38. data = head->a;
  39. head = head->next;
  40. free(temp);
  41. return data;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. int main()
  48. {
  49.  
  50. int x,n,i,m,l;
  51. printf("ENTER QUEUE INPUT: ");
  52. scanf("%d",&n);
  53. for(i=0; i<n; i++)
  54. {
  55. scanf("%d",&x);
  56. enqueue(x);
  57. }
  58.  
  59.  
  60. printf("\n");
  61. for(i=0; i<n; i++)
  62. {
  63. x=dequeue();
  64. printf("QUEUE %d OUTPUT :",i+1);
  65. printf("%d ",x);
  66. printf("\n");
  67.  
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment