Advertisement
maxim_shlyahtin

q_example

Apr 17th, 2023 (edited)
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define QMAX 100
  5.  
  6. typedef struct queue {
  7.     int qu[QMAX];
  8.     int rear, frnt;
  9. } queue;
  10.  
  11. void init(struct queue *q) {
  12.     q->frnt = 1;
  13.     q->rear = 0;
  14. }
  15.  
  16. void insert(struct queue *q, int x) {
  17.     if (q->rear < QMAX - 1) {
  18.         q->rear++;
  19.         q->qu[q->rear] = x;
  20.     } else
  21.         printf("Q is full!\n");
  22. }
  23.  
  24. int isempty(struct queue *q) {
  25.     if (q->rear < q->frnt) return 1;
  26.     else return 0;
  27. }
  28.  
  29. void print(struct queue *q) {
  30.     int h;
  31.     if(isempty(q)==1) {
  32.         printf("Q is empty!\n");
  33.         return;
  34.     }
  35.     for(h = q->frnt; h<= q->rear; h++)
  36.         printf("%d ",q->qu[h]);
  37. }
  38.  
  39. int removex(struct queue *q) {
  40.     int x, h;
  41.     if(isempty(q)==1) {
  42.         printf("Q!\n");
  43.         return 0;
  44.     }
  45.     x = q->qu[q->frnt];
  46.     for(h = q->frnt; h < q->rear; h++) {
  47.         q->qu[h] = q->qu[h+1];
  48.     }
  49.     (q->rear)--;
  50.     return x;
  51. }
  52.  
  53. int main() {
  54.     struct queue *q;
  55.     int a;
  56.     q = (queue*)malloc(sizeof(queue));
  57.     init(q);
  58.     print(q);
  59.     for(int i=0;i<8;i++) {
  60.         printf("Insert queue elem: ");
  61.         scanf("%d", &a);
  62.         insert(q, a);
  63.     }
  64.     printf("\n");
  65.     print(q);
  66.     while(q->frnt <= q->rear) {
  67.         a = remove(q);
  68.         printf("\nDeleted elem %d\n", a);
  69.         print(q);
  70.     }
  71.     getchar(); getchar();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement