Advertisement
ramytamer

Untitled

Apr 29th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TYPE int
  5. #define MAXSIZE 3
  6.  
  7. typedef struct Queue{
  8.     TYPE data[MAXSIZE];
  9.     int head,tail,numberOfElements;
  10. }Queue;
  11.  
  12. void initialize(Queue *q){
  13.     q->head = q->tail = q->numberOfElements = 0;
  14. }
  15.  
  16. int isFull(Queue *q){
  17.     return q->numberOfElements==MAXSIZE;
  18. }
  19.  
  20. void insert(Queue *q,TYPE val){
  21.     if(!isFull(q)) {
  22.         q->data[q->head] = val;
  23.         q->head++; q->numberOfElements++;
  24.     }
  25. }
  26.  
  27. int isEmpty(Queue *q){
  28.     return !q->numberOfElements;
  29. }
  30.  
  31. TYPE take(Queue *q){
  32.     if(!isEmpty(q)) {
  33.         TYPE x = q->data[q->tail];
  34.         q->tail++;
  35.         q->numberOfElements--;
  36.         return x;
  37.     }
  38. }
  39.  
  40. void display(Queue q){
  41.     if(!isEmpty(&q)) {
  42.         printf("QUEUE : ");
  43.         while(!isEmpty(&q))
  44.             printf("[%d]->", take(&q));
  45.         printf("\b\b  \n");
  46.     }
  47. }
  48.  
  49. int main(){
  50.     system("CLS");
  51.     Queue q; initialize(&q);
  52.     insert(&q,1); display(q);
  53.     insert(&q,2); display(q);
  54.     insert(&q,3); display(q);
  55.     take(&q); display(q);
  56.     take(&q); display(q);
  57.     insert(&q,3); display(q);
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement