Untitled
unknown
plain_text
2 years ago
6.2 kB
8
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct card {
int number;
struct card *previous;
struct card *next;
} card;
typedef struct deck {
card *top;
card *bottom;
} deck;
bool is_empty(deck *d) { return (d->top == NULL);}
// Helper functions to operate on cards and decks
card* create_card(int number) {
card *new_card = malloc(sizeof(card));
if (new_card != NULL) {
new_card->number = number;
new_card->previous = NULL;
new_card->next = NULL;
}
return new_card;
}
void add_card(deck *d, int number) {
card *new_card = create_card(number);
if (new_card == NULL) { return;}
if (is_empty(d)) {
// 牌堆为空时,新卡片成为牌堆的顶部和底部
d->top = new_card;
d->bottom = new_card;
} else {
// 牌堆不为空时,新卡片添加到顶部
new_card->previous = d->top; // 新卡片的previous指向原顶部卡片
d->top->next = new_card; // 原顶部卡片的next指向新卡片
d->top = new_card; // 更新牌堆的顶部为新卡片
}
}
void remove_top_card(deck *d) {
if (is_empty(d)) return;
card *temp = d->top;
if (d->top == d->bottom) {
d->top = NULL;
d->bottom = NULL;
} else {
d->top = d->top->previous;
d->top->next = NULL;
}
free(temp);
}
void move_cards(deck *source, deck *destination) {
if (is_empty(source)) { return; }
if (is_empty(destination)) {
// 如果目標牌堆是空的,則直接將源牌堆的卡片移動到目標牌堆
destination->top = source->top;
destination->bottom = source->bottom;
} else {
// 將源牌堆底部的卡片連接到目標牌堆頂部的卡片
source->bottom->previous = destination->top;
destination->top->next = source->bottom;
// 更新目標牌堆的頂部指針
destination->top = source->top;
}
// 清除源牌堆的指針
source->top = NULL;
source->bottom = NULL;
}
void merge_decks(deck *source, deck *destination) {
deck *temp_deck = malloc(sizeof(deck));
if (temp_deck != NULL) {
temp_deck->top = NULL;
temp_deck->bottom = NULL;
}
// 当两个牌堆中至少有一个不为空时,继续执行
while (!is_empty(source) || !is_empty(destination)) {
if (!is_empty(source)) {
// 从source牌堆顶部取一张牌
card *card_from_source = source->top;
if(source->top->previous != NULL){
source->top = card_from_source->previous;
} else {source->top= source->bottom=NULL ;}
// 将这张牌放到临时牌堆的底部
if (temp_deck->bottom == NULL) {
temp_deck->bottom = temp_deck->top = card_from_source;
card_from_source->next = card_from_source->previous = NULL;
// remove_top_card(source);
} else {
temp_deck->bottom->previous = card_from_source;
card_from_source->next = temp_deck->bottom;
card_from_source->previous = NULL;
temp_deck->bottom = card_from_source;
}
}
if (!is_empty(destination)) {
// 从destination牌堆顶部取一张牌
card *card_from_destination = destination->top;
destination->top = card_from_destination->previous;
// 将这张牌放到临时牌堆的底部
if (temp_deck->bottom == NULL) {
temp_deck->bottom = temp_deck->top = card_from_destination;
if (destination->top->previous != NULL){
card_from_destination->next = card_from_destination->previous = NULL;
}else{destination->top = destination->bottom = NULL;}
} else {
temp_deck->bottom->previous = card_from_destination;
card_from_destination->next = temp_deck->bottom;
card_from_destination->previous = NULL;
temp_deck->bottom = card_from_destination;
}
}
}
// 将临时牌堆的所有牌转移到目标牌堆
if (!is_empty(temp_deck)) {
destination->top = temp_deck->top;
destination->bottom = temp_deck->bottom;
}
}
int count_in_deck(deck *d){
card *current = d->bottom;
int cnt=0;
while (current != NULL) {
cnt++;
current = current->next;
}
return cnt;
}
// Implement other functions like remove_bottom_card, move_cards, merge_decks
void print_deck(deck *d) {
card *current = d->top;
printf("%d " , count_in_deck(d));
while (current != NULL) {
printf("%d ", current->number);
current = current->previous;
}
printf("\n");
}
int main() {
int decks_num, operation_nums;
scanf("%d%d", &decks_num, &operation_nums);
// 创建牌堆数组
deck *decks = calloc(decks_num, sizeof(deck));
for (int i = 0; i < operation_nums; i++) {
int operation_type, i, j, card_number;
scanf("%d", &operation_type);
switch (operation_type) {
case 1: // 添加卡牌
scanf("%d%d", &i, &card_number);
add_card(&decks[i-1], card_number);
break;
case 2: // 移除卡牌
scanf("%d", &i);
remove_top_card(&decks[i-1]);
break;
case 3: // 移动卡牌
scanf("%d%d", &i, &j);
move_cards(&decks[i-1], &decks[j-1]);
break;
case 4: // 合并牌堆
scanf("%d%d", &i, &j);
merge_decks(&decks[i-1], &decks[j-1]);
break;
}
}
// 打印所有牌堆的卡牌
for (int i = 0; i < decks_num; i++) {
print_deck(&decks[i]);
}
free(decks);
return 0;
}
Editor is loading...
Leave a Comment