C language [Problem Description]: Use 3 queues to retain the last 10 calls on the mobile phone, (0) missed calls, (1) received calls, and (2) dialed calls.

[Input format]: All call records, one record per line, the total length is less than 10,000 lines.
Each record contains two numbers, the first number represents the record type, and the second number represents the mobile phone number.
Arrange the calls in chronological order from front to back, with the most recent call at the end, and so on.


[Output format]: Output missed calls, received calls, and dialed calls in three columns.
Columns are separated by spaces, followed by phone numbers are output first, and if there are less than 10 lines, 0 is used as a placeholder.
【样例输入】
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

Without further ado, here’s the code:

#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;
}

Guess you like

Origin blog.csdn.net/qq_62088638/article/details/133816989