-1

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

Strattera
  • 1
  • 1
  • Allow me to gaze into my crystal ball ... it asks *How did you determine `shuffle()` is not the cause of your problems?* – jxh Jun 03 '22 at 22:20
  • 2
    Warning never ever `malloc` a `std::string` or a class containing a `std::string`. `malloc` provides blocks of memory. It does not construct objects. If a `std::string` is not properly constructed, it becomes a bomb. You can't even safely `temp->nodeCard = c;` because the state of the `string cardName` in `c` is completely undefined. It's just a blob of memory in the shape of a `std::string`, but not yet a `std::string`. This applies to any non-trivial C++ class. – user4581301 Jun 03 '22 at 22:29
  • *"it stops working"* -- could you be more specific? If not, I'll assume "stops working" means "my computer exploded". As part of this, please identify where it stops working. Is your *example* code truly minimal? Is the `sleep()` call required to reproduce the error? (If not, get rid of it.) Perhaps `printList()` is required to see the error, but does the list have to be shuffled first? Please give us a [mre] and enough details about your symptoms so that it is possible to determine if this is a duplicate of [Linked list with memory leak issues](https://stackoverflow.com/questions/55821967/). – JaMiT Jun 03 '22 at 22:58
  • 1
    When you used the debugger, which statement is causing the issue. Please edit your post and indicate which statement(s) are causing the issue. You may want to draw the linked list as you step through the code with the debugger. – Thomas Matthews Jun 03 '22 at 23:05
  • 1
    If creating a linked list is **not** a learning exercise, please consider using `std::list`, as it has already been tested and debugged. – Thomas Matthews Jun 03 '22 at 23:06

0 Answers0