通话记录

已知 10 条通话记录,通话记录有三种类型:0 代表未接来电,1 代表已接来电,2 代表已拨电话。要求分别将三种类型前 10 条通话记录以三列的形式输出。

输入格式
输入 10 条通话记录,每条通话记录都占一行。每一行的第一个数字代表通话记录的类型,第二个数字代表电话号码,电话号码均由 11 个数字组成。他们之间用一个空格隔开。

输出格式
分三列输出未接来电、已接电话和已拨电话。
每列之间用一个空格分割,最后一列后没有空格。每种类型输出前十个电话,先出现的通话记录先输出,不足十个的用 0 占位。

#include
#include

#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 *)calloc(sizeof(long), length);
    q->head = 0;
    q->tail = -1;
    q->length = length; 
}

int push(Queue *q, long element){
    if(q->tail + 1 >= q->length){
        return ERROR;
    }
    q->tail++;
    q->data[q->tail] = element;
    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 = 0;
    long str[11];
    Queue *q1 = (Queue *)malloc(sizeof(Queue));
    init(q1, 10);
    Queue *q2 = (Queue *)malloc(sizeof(Queue));
    init(q2, 10);
    Queue *q3 = (Queue *)malloc(sizeof(Queue));
    init(q3, 10);
    for(int i = 0; i < 10; i++){
        scanf("%d %ld", &m, &str[i]);
        if(m == 0){
            push(q1, str[i]);
        }
        else if(m == 1){
            push(q2, str[i]);
        }else{
            push(q3, str[i]);
        }
    }
    output(q1, q2, q3);
    clear(q1);
    clear(q2);
    clear(q3);
    return 0;
    
}

你可能感兴趣的:(通话记录)