天天看點

通話記錄

已知 10 條通話記錄,通話記錄有三種類型:0 代表未接來電,1 代表已接來電,2 代表已撥電話。要求分别将三種類型前 10 條通話記錄以三列的形式輸出。

輸入格式

輸入 10 條通話記錄,每條通話記錄都占一行。每一行的第一個數字代表通話記錄的類型,第二個數字代表電話号碼,電話号碼均由 11 個數字組成。他們之間用一個空格隔開。

輸出格式

分三列輸出未接來電、已接電話和已撥電話。

每列之間用一個空格分割,最後一列後沒有空格。每種類型輸出前十個電話,先出現的通話記錄先輸出,不足十個的用 0 占位。

#include <stdio.h>

#define ERROR 0
#define OK 1
typedef struct Queue {
    long *data;
    int head, tail, length;
}Queue;

void init(Queue *q, int length) {
    q->data = (long *)malloc(sizeof(long) * length);
    q->head = 0;
    q->tail = -1;
    q->length = length;
} 

int push(Queue *q, long element) {
    if (q->tail + 1 >= q->length) {
        //printf("error\n");
        return ERROR;
    }
    q->tail++;
    q->data[q->tail] = element;
    //printf("ok\n");
    return OK;
}

void output(Queue *q1, Queue *q2, Queue *q3) {
    for (int i = q1->head, j = q2->head, k = q3->head; i <= 9; i++, j++, k++) {
        printf("%ld %ld %ld\n", q1->data[i], q2->data[j], q3->data[k]);
    }
    printf("\n");
}

int empty(Queue *q) {
    return q->head > q->tail;
}

void clear(Queue *q) {
    free(q->data);
    free(q);
}

int main() {
    
    int m[3];
    long str[11];
    Queue *queue1 = (Queue *)malloc(sizeof(Queue));
    init(queue1, 10);
    Queue *queue2 = (Queue *)malloc(sizeof(Queue));
    init(queue2, 10);
    Queue *queue3 = (Queue *)malloc(sizeof(Queue));
    init(queue3, 10);
    for (int i = 0; i < 10; i++) {
        scanf("%d %ld", &m[i], &str[i]);
        if (m[i] == 0) {
            push(queue1, str[i]);
        } else if (m[i] == 1) {
            push(queue2, str[i]);
        } else {
            push(queue3, str[i]);
        }
    }
   
        output(queue1, queue2, queue3);
       
       
    clear(queue1);
    clear(queue2);
    clear(queue3);
    return 0;
}
           

繼續閱讀