SHARE
TWEET

Untitled

a guest Feb 14th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define EMPTY -1
  5.  
  6. struct node {
  7.     int data;
  8.     struct node* next;
  9. };
  10.  
  11. struct queue {
  12.     struct node* front;
  13.     struct node* back;
  14. };
  15.  
  16. void init(struct queue* qPtr);
  17. int enqueue(struct queue* qPtr, int val);
  18. int dequeue(struct queue* qPtr);
  19. int empty(struct queue* qPtr);
  20. int front(struct queue* qPtr);
  21.  
  22.  
  23. int main(void) {
  24.  
  25.     struct queue* MyQuePtr = (struct queue *)malloc(sizeof(struct queue));
  26.     init(MyQuePtr);
  27.  
  28.     enqueue(MyQuePtr, 3);
  29.     enqueue(MyQuePtr, 5);
  30.     enqueue(MyQuePtr, 7);
  31.  
  32.     printf("\n dequeue %d ", dequeue(MyQuePtr));
  33.     printf("\n front of the queue %d", front(MyQuePtr));
  34.     printf("\n dequeue %d ", dequeue(MyQuePtr));
  35.     printf("\n dequeue %d ", dequeue(MyQuePtr));
  36.  
  37.     printf("\n is empty %d ", empty(MyQuePtr));
  38.  
  39.     return 0;
  40. }
  41.  
  42. void init(struct queue* qPtr) {
  43.     qPtr->front = NULL;
  44.     qPtr->back = NULL;
  45. }
  46.  
  47. int enqueue(struct queue* qPtr, int val) {
  48.     struct node* temp;
  49.     temp = (struct node*)malloc(sizeof(struct node));
  50.     if(temp != NULL) {
  51.         temp->data = val;
  52.         temp->next = NULL;
  53.  
  54.         if(qPtr->back != NULL)
  55.             qPtr->back->next = temp;
  56.        
  57.         qPtr->back = temp;
  58.  
  59.         if(qPtr->front == NULL)
  60.             qPtr->front = temp;
  61.  
  62.         return 1;
  63.     }
  64.     else
  65.         return 0;
  66.        
  67.     }
  68.  
  69. int dequeue(struct queue* qPtr) {
  70.     struct node * temp;
  71.     int retval;
  72.     if(qPtr->front == NULL)
  73.         return EMPTY;
  74.     retval = qPtr->front->data;
  75.     temp = qPtr->front;
  76.     qPtr->front = qPtr->front->next;
  77.  
  78.     if(qPtr->front == NULL)
  79.         qPtr->back = NULL;
  80.  
  81.     free(temp);
  82.  
  83.     return retval;
  84. }
  85.  
  86. int empty(struct queue* qPtr) {
  87.     return qPtr->front == NULL;
  88. }
  89.  
  90. int front(struct queue* qPtr) {
  91.     if(qPtr->front != NULL)
  92.         return qPtr->front->data;
  93.     else
  94.         return EMPTY;
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top