Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define QMAX 100
- typedef struct queue {
- int qu[QMAX];
- int rear, frnt;
- } queue;
- void init(struct queue *q) {
- q->frnt = 1;
- q->rear = 0;
- }
- void insert(struct queue *q, int x) {
- if (q->rear < QMAX - 1) {
- q->rear++;
- q->qu[q->rear] = x;
- } else
- printf("Q is full!\n");
- }
- int isempty(struct queue *q) {
- if (q->rear < q->frnt) return 1;
- else return 0;
- }
- void print(struct queue *q) {
- int h;
- if(isempty(q)==1) {
- printf("Q is empty!\n");
- return;
- }
- for(h = q->frnt; h<= q->rear; h++)
- printf("%d ",q->qu[h]);
- }
- int removex(struct queue *q) {
- int x, h;
- if(isempty(q)==1) {
- printf("Q!\n");
- return 0;
- }
- x = q->qu[q->frnt];
- for(h = q->frnt; h < q->rear; h++) {
- q->qu[h] = q->qu[h+1];
- }
- (q->rear)--;
- return x;
- }
- int main() {
- struct queue *q;
- int a;
- q = (queue*)malloc(sizeof(queue));
- init(q);
- print(q);
- for(int i=0;i<8;i++) {
- printf("Insert queue elem: ");
- scanf("%d", &a);
- insert(q, a);
- }
- printf("\n");
- print(q);
- while(q->frnt <= q->rear) {
- a = remove(q);
- printf("\nDeleted elem %d\n", a);
- print(q);
- }
- getchar(); getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement