c语言【问题描述】:使用3个队列,分别保留手机上最近10个,(0)未接来电、(1)已接来电、(2)已拨电话。

【输入形式】:全部通话记录,每行一条记录,总长度小于10000行。
每条记录包含两个数字,第一个数代表记录类型,第二个数代表手机号码。
按照电话发生的时间顺序,由前向后排列,最近一个电话在最后,以此类推。


【输出形式】:分3列输出未接来电、已接来电、已拨电话。
列之间用空格分割,后接电话在最先输出,不足10条用0占位。
【样例输入】
2    18270477699
1    10149800116
0    19906559817
1    16209018105
1    16804212234
2    19289130583
1    17982711123
0    10897630486
1    11860787674
0    15192777554
【样例输出】
15192777554 11860787674 19289130583
10897630486 17982711123 18270477699
19906559817 16804212234 0
0 16209018105 0
0 10149800116 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0

话不多说 下面放代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACK_SIZE 10
#define STRING_SIZE 20

void push(char** stack, int* top, const char* item) {
	if (*top < STACK_SIZE - 1) {
		(*top)++;
		strcpy(stack[*top], item);
	}
}

char* pop(char** stack, int* top) {
	char* item = "0";
	if (*top >= 0) {
		item = stack[*top];
		(*top)--;
	}
	return item;
}

int main() {
	int n;
	char pn[STRING_SIZE];
	char* a[STACK_SIZE];
	char* b[STACK_SIZE];
	char* c[STACK_SIZE];
	int topA = -1;
	int topB = -1;
	int topC = -1;
	
	for (int i = 0; i < STACK_SIZE; i++) {
		a[i] = (char*)malloc(STRING_SIZE * sizeof(char));
		b[i] = (char*)malloc(STRING_SIZE * sizeof(char));
		c[i] = (char*)malloc(STRING_SIZE * sizeof(char));
		strcpy(a[i], "0");
		strcpy(b[i], "0");
		strcpy(c[i], "0");
	}
	
	while (scanf("%d %s", &n, pn) == 2) {
		if (n == 0) push(a, &topA, pn);
		if (n == 1) push(b, &topB, pn);
		if (n == 2) push(c, &topC, pn);
	}
	
	for (int i = 0; i < STACK_SIZE; i++) {
		printf("%s %s %s\n", pop(a, &topA), pop(b, &topB), pop(c, &topC));
	}
	
	for (int i = 0; i < STACK_SIZE; i++) {
		free(a[i]);
		free(b[i]);
		free(c[i]);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_62088638/article/details/133816989