13767 - PME-20221214_3   

Description

// debug and correct answer

#include <stdio.h>

#include <stdlib.h>

#define FULL 10

 

struct node {

    int data;

    struct node *next;

};

typedef struct node node;

 

typedef struct queue {

    int count;

    node *front;

    node *rear;

}QQ;

 

void initialize(QQ *q){

    q->count = 0;

    q->front = NULL;

    q->rear = NULL;

}

 

int isempty(QQ *q){

    return (q->rear == NULL);

}

void enqueue(QQ *q, int value) {

    if (q->count < FULL) {

        node *tmp;

        tmp = malloc(sizeof(node));

        tmp->data = value;

        tmp->next = NULL;

        if(!isempty(q)) {

            q->rear->next = tmp;

            q->rear = tmp;

        } else {

            q->front = q->rear = tmp;

        }

        q->count++;

    } else {

        printf("List is full\n");

    }

}

int dequeue(QQ *q) {

    node *tmp;

    int n = q->front->data;

    tmp = q->front;

    q->front->next = q->front; 

    q->count--;

    free(tmp);

    return(n);

}

void display(node *head) {

    if(head == NULL) {

        printf("Queue_rear\n");

    } else {

        printf("%d<-", head -> data);

        display(head->next);

    }

}

int main() {

    char ch;

    int num, run=1;

    QQ *q;

    q = (QQ *)malloc(sizeof(QQ));

    initialize(q);

    while(run) {

        scanf("%c",&ch);

        switch (ch){

        case 'e':

            scanf("%d", &num);

            enqueue(q, num);

            break;

        case 'd':

            dequeue(q);

            break;

        case 'q':

            display(q->front);

            run=0;

            break;

        default:

            continue;

        }

    }

    return 0;

}

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss