已知 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;
}