通话记录

已知 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;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yitahutu79

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值