删除节点会使分配了值的节点出现问题,为什么?为什么不能在打印功能中更改节点值?

时间:2019-04-27 19:56:25

标签: c visual-studio

3个主要问题:

  1. Visual Studio不会打印空白方块,心形,棍棒,黑桃等符号。
  2. 从链接列表中删除节点时,我将其值提供给另一个列表中的另一个节点,但是当我删除它时,它也删除了另一个列表中的节点的值,但是idk为什么。
  3. 在打印我的值时,我试图在打印前更改一个值,但是并不能更改所有值。它仅适用于标题。

我试图使我的打印功能接收到一个双星节点定义的功能,但它不是那样的,但是我做错了。我尝试复制并粘贴不同的锹和心脏符号,但它始终会打印一个问号。这是针对Visual Studio 2015 btw。而且我的电脑手功能还没有完成,只是打印电脑手的前三张卡。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<time.h>
#include <stdlib.h>

int rand_gen(int count) {
double frac;
frac = (double)rand() / ((double)RAND_MAX + 1);
return floor(count * frac);
}
typedef struct card_s {
char suit;
int suitnum;
int face;
struct card_s *next, *previous;
}card;

void deletecard(card *p, card **hl) {
if (p == *hl) {
    *hl = p->next;
}
else {
    p->previous->next = p->next;
}
if (p->next == NULL) {
    p->previous->next = NULL;
}
else {
    p->next->previous = p->previous;
}
free(p);
}
void createdeck(card **hl) {
int i = 1;
int j;
int y = 0;
card* temp;
temp = (card *)malloc(sizeof(card));
card* temp2;
temp2 = (card *)malloc(sizeof(card));
while (i <= 13) {
    j = 1;
    while (j <= 4) {
        temp2->face = i;
        temp2->suitnum = j;
        if (*hl == NULL) {
            temp->next = NULL;
            temp->previous = NULL;
            temp->face = 1;
            temp->suitnum = 1;
            (*hl) = temp;
        }
        else {
            temp->next = temp2;
            temp2->previous = temp;
            temp2->next = NULL;
            temp = temp->next;
            temp2 = (card *)malloc(sizeof(card));
        }
        j++;
    }
    i++;
}
}
void printdeck(card *currentNode) {
printf("\n\n\n");
if (currentNode->suitnum == 1) {
    currentNode->suit = '♣';
}
else if (currentNode->suitnum == 2) {
    currentNode->suit = '♦';
}
else if (currentNode->suitnum == 3) {
    currentNode->suit = '♥';
}
else {
    currentNode->suit = '♠';
}
while (currentNode != NULL) {
    printf("Face: %d, Suit: %c\n", currentNode->face, currentNode->suit);
    currentNode = currentNode->next;
}
}

void shuffle(card *current) {
int x = 1;
while (x < 1000) {
    int i = 0;
    int c = 0;
    int y = 0;
    card* temp;
    card *temp2;
    card* temp3;
    temp3 = (card *)malloc(sizeof(card));
    temp2 = (card *)malloc(sizeof(card));
    temp = (card *)malloc(sizeof(card));
    while (i < 52) {
        int c = 0;
        int j = rand_gen(51);
        temp = current;
        while (c < j) {
            temp = temp->next;
            c++;
        }
        int y = 0;
        temp2 = current;
        while (y < i) {
            temp2 = temp2->next;
            y++;
        }
        temp3->face = temp2->face;
        temp3->suitnum = temp2->suitnum;
        temp2->face = temp->face;
        temp2->suitnum = temp->suitnum;
        temp->face = temp3->face;
        temp->suitnum = temp3->suitnum;
        i++;
    }
    x++;
}
}

void drawhand(card **player, card **computer, card **headl) {
int i = 0;
card* temp = (card *)malloc(sizeof(card));
temp = *headl;
*player = temp;
card* temp2 = (card *)malloc(sizeof(card));
temp2 = temp->next;
deletecard(temp, headl);
*computer = temp2;
card* temp3 = (card *)malloc(sizeof(card));
temp3 = temp2->next;
deletecard(temp2, headl);
(*player)->next = temp3;
card* temp4 = (card *)malloc(sizeof(card));
temp4 = temp3->next;
deletecard(temp3, headl);
(*computer)->next = temp4;
card* temp5 = (card *)malloc(sizeof(card));
temp5 = temp4->next;
deletecard(temp4, headl);
(*player)->next->next = temp5;
card* temp6 = (card *)malloc(sizeof(card));
temp6 = temp5->next;
deletecard(temp5, headl);
(*computer)->next->next = temp6;
card* temp7 = (card *)malloc(sizeof(card));
temp7 = temp6->next;
deletecard(temp6, headl);
(*player)->next->next->next = temp7;
card* temp8 = (card *)malloc(sizeof(card));
temp8 = temp7->next;
deletecard(temp7, headl);
(*computer)->next->next->next = temp8;
card* temp9 = (card *)malloc(sizeof(card));
temp9 = temp8->next;
deletecard(temp8, headl);
(*player)->next->next->next->next = temp9;
card* temp10 = (card *)malloc(sizeof(card));
temp10 = temp9->next;
deletecard(temp9, headl);
deletecard(temp10, headl);
(*player)->next->previous = *player;
(*player)->next->next->previous = (*player)->next;
(*player)->next->next->next->previous = (*player)->next->next;
(*player)->next->next->next->next->previous = (*player)->next->next- 
>next;
(*player)->next->next->next->next->next = NULL;
(*computer)->next->previous = *computer;
(*computer)->next->next->previous = (*computer)->next;
(*computer)->next->next->next->previous = (*computer)->next->next;
(*computer)->next->next->next->next->previous = (*computer)->next- 
>next->next;
(*computer)->next->next->next->next->next = NULL;
}
void valueprint(int x) {
printf("Royal Flush          10♠ J♠ Q♠ K♠ A♠\n\n");
printf("Straight Flush       2♣ 3♣ 4♣ 5♣ 6♣\n\n"); 
printf("Four of a Kind       9♣ 9♠ 9♥ 9♦ ■\n\n");
printf("Full House           9♣ 9♠ 9♥ 3♥ 3♠\n\n");
printf("Flush                ■♥ ■♥ ■♥ ■♥ ■♥\n\n");
printf("Straight             4■ 5■ 6■ 7■ 8■\n\n");
printf("Three of a Kind      9♣ 9♠ 9♥ ■ ■\n\n");
printf("Two Pair             K♠ K♣ 6♠ 6♥ ■\n\n");
printf("Jacks or Better      J♣ J♠ ■ ■ ■\n\n");
}
void computerhand(card *currentnode) {
printf("\n\n\n");

}
int main(void) {
int x = 1;
valueprint(x);
srand((int)time(NULL));
card *headl = NULL;
createdeck(&headl);
printdeck(headl);
shuffle(headl);
printdeck(headl);
card* player = NULL;
card* computer = NULL;
drawhand(&player, &computer, &headl);
printf("Computer/Dealer's hand:\n");
computerhand(computer);
printf("Your hand:\n");
printdeck(player);

0 个答案:

没有答案