我似乎无法弄清楚这一点。出于某种原因,我的链表不是创建一副牌,甚至不创建一个节点。如果有人可以提供帮助,我会非常感激。我的所有代码都在下面。如果你能告诉我我做错了什么,并指出我的方向非常有帮助。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SPADE "\x06"
#define CLUB "\x05"
#define DIAMONDS "\x04"
#define HEART "\x03"
typedef struct node {
int face_val;
char suit[10];
struct card_s * next;
} card;
void add_card(card **headp, card **tailp, int face, char suit) {
char suit2[10] = "Test";
*tailp = (card*)malloc(sizeof(card));
card *temp = (card*)malloc(sizeof(card));
temp->face_val = face;
temp->suit[10] = &suit2;
strcpy(temp->suit, suit2);
if (*headp == NULL) {
*headp = temp;
(*headp)->next = *tailp;
}
else {
(*tailp)->face_val = temp->face_val;
(*tailp)->suit[10] = temp->suit;
(*tailp)->next = malloc(sizeof(card));
*tailp = (*tailp)->next;
}
return;
}
void create_deck(card **headp, card **tailp) {
int i = 1;
while (i <= 13) {
int j = 1;
while (j <= 4) {
if (j == 1) {
add_card(headp,tailp,i,SPADE);
}
if (j == 2) {
add_card(headp,tailp,i,HEART);
}
if (j == 3) {
add_card(headp,tailp,i,DIAMONDS);
}
if (j == 4) {
add_card(headp,tailp,i,CLUB);
}
j++;
}
i++;
}
return;
}
int main(void) {
card *headp=NULL;
card *tailp=NULL;
create_deck(&headp,&tailp);
}
答案 0 :(得分:0)
正如其他人在评论中指出的那样,你做了很多错误,比如分配卡三次而不是一次,将字符串文字传递给char参数并立即覆盖tailp
而不检查它的先前值,毕竟这是你的add_card元素在大多数情况下会操作的内容。我测试了你的代码,我纠正了一些错误。我能够看到卡片:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define SPADE "SPADE"
#define CLUB "CLUB"
#define DIAMONDS "DIAMOND"
#define HEART "HEART"
typedef struct node {
int face_val;
char suit[10];
struct node * next;
} card;
void add_card(card **headp, card **tailp, int face, const char* suit) {
card *temp = (card*)malloc(sizeof(card)); // allocate new card
temp->face_val = face; // assign face val
strcpy(temp->suit, suit); // assign suit
if (*headp == NULL) {
*headp = temp;
*tailp = temp;
}
else {
(*tailp)->next = temp;
*tailp = temp;
}
}
void create_deck(card **headp, card **tailp) {
int i = 1;
while (i <= 13) {
int j = 1;
while (j <= 4) {
if (j == 1)
add_card(headp,tailp,i,SPADE);
if (j == 2)
add_card(headp,tailp,i,HEART);
if (j == 3)
add_card(headp,tailp,i,DIAMONDS);
if (j == 4)
add_card(headp,tailp,i,CLUB);
j++;
}
i++;
}
}
void printDeck(card *curNode) {
while(curNode != NULL) {
printf("face val : %d, SUIT : %s\n", curNode->face_val, curNode->suit);
curNode = curNode->next;
}
}
int main(void) {
card *headp=NULL;
card *tailp=NULL;
create_deck(&headp,&tailp);
printDeck(headp);
}
除此之外,您还可以了解here的链接列表的更多信息。
答案 1 :(得分:0)
这是代码的建议版本
cleanup
,但应该add_card()
#include
头文件,这些内容未被使用typedef
for()
语句而不是'笨重的'while()
语句现在代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> // perror()
#include <stdlib.h> // malloc(), exit(), EXIT_FAILURE
//#include <math.h>
#define MAX_SUITS 4
#define MAX_CARDS_IN_SUIT 13
#define SPADE 0x06
#define CLUB 0x05
#define DIAMONDS 0x04
#define HEART 0x03
struct node
{
int face_val;
char suit;
struct node * next;
};
typedef struct node card;
// prototypes
void add_card(card **headp, card **tailp, int face, char suit);
void create_deck(card **headp, card **tailp);
void add_card(card **headp, card **tailp, int face, char suit)
{
card *temp = malloc(sizeof(card));
if( !temp )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
temp->face_val = face;
temp->suit = suit;
temp->next = *headp;
if (*headp == NULL)
{
*headp = temp;
*tailp = temp;
}
else
{
(*tailp)->next = temp;
*tailp = temp;
}
} // end function: add_card
void create_deck(card **headp, card **tailp)
{
for ( int i=1; i <= MAX_CARDS_IN_SUIT; i++ )
{
for( int j=1; j <= MAX_SUITS; j++ )
{
switch( j )
{
case 1:
add_card(headp,tailp,i,SPADE);
break;
case 2:
add_card(headp,tailp,i,HEART);
break;
case 3:
add_card(headp,tailp,i,DIAMONDS);
break;
case 4:
add_card(headp,tailp,i,CLUB);
break;
} // end switch
} // end for each suit
} // end for each card in suit
} // end function: create_deck
int main(void)
{
card *headp=NULL;
card *tailp=NULL;
create_deck(&headp,&tailp);
} // end function: main