I'm working on creating a blackjack cards deck and I wrote a linked list that was working properly where the data was an int (card num) and string (card type). When I changed my linked list to take an object from a card class I wrote as its data it stopped working properly. If I only insert one node it works and I can remove it and add again. But if I insert more than one node into the list it stops working.
My insert functions:
node* createNode(card c){
node* temp = (node*)malloc(sizeof(node));
temp->nodeCard = c;
temp->next = NULL;
return temp;
}
bool listEmpty(node* h){
if(h == NULL){
return true;
}
return false;
}
node* insertFront(node* h,card c){
node* temp = createNode(c);
temp->next = h;
return temp;
}
node* insertBack(node* h, card c){
node* temp = createNode(c);
if(listEmpty(h)){
return temp;
}
node* last = h;
while(last->next != NULL){
last=last->next;
}
last->next = temp;
return h;
}
Card class:
class card {
public:
int cardNum;
char cardColor;
string cardName;
card(int n, char c){
cardColor = c;
cardNum = n;
}
void setCardName(){
string xNumber = cardNumberMap[cardNum];
string xColor = cardColorMap[cardColor];
string x = xNumber + " of " + xColor;
cardName = x;
}
};
Main function:
int main(){
int cardNumsArr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
char cardColorsArr[] = {'h','c','d','s'};
node* deck = NULL;
for(int i = 0; i < 13;i++){
for(int j = 0; j < 4;j++){
card newCard(cardNumsArr[i],cardColorsArr[j]);
newCard.setCardName();
deck = insertBack(deck, newCard);
}
}
shuffle(deck);
printList(deck);
sleep(1);
return 0;
}
Edit: Sorry if I wasn't specific enough, this is my first time using this website. What I meant by the stops working is when I insert into the list for the second time, after that line nothing will run. So if I insert card1 and cout 5 and then insert card2 and cout 6, the program will display 5 and stop. I'm going to upload 2 images of the console now.
Console when I use the given loop to insert: enter image description here
Console when I only insert one node: enter image description here