title: Why are my linked lists values from another linked list getting deleted when I delete the node from the other linked list?
body: <p>I have a linked list that represents a deck of cards and when I go to give a card to a player and the computer(one to each at a time) I copy the values from the top card, give its values to the card in the hand but when I go to delete the node from the deck, its deletion messes with the values of the card in the hand.  its values when deleted are the exact same as the values of the card in the hand. </p>
  
  <p>Also, I included all of my code just in case it helps but its not all finished right now.  Just trying to get the hands to work before I move on.  </p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include&lt;stdio.h&gt;
  #include&lt;math.h&gt;
  #include&lt;string.h&gt;
  #include&lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;wchar.h&gt;
  #include &lt;conio.h&gt;
  
  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-&gt;next;
  }
  else {
      p-&gt;previous-&gt;next = p-&gt;next;
  }
  if (p-&gt;next == NULL) {
      p-&gt;previous-&gt;next = NULL;
  }
  else {
      p-&gt;next-&gt;previous = p-&gt;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 &lt;= 13) {
      j = 1;
      while (j &lt;= 4) {
          temp2-&gt;face = i;
          temp2-&gt;suitnum = j;
          if (*hl == NULL) {
              temp-&gt;next = NULL;
              temp-&gt;previous = NULL;
              temp-&gt;face = 1;
              temp-&gt;suitnum = 1;
              (*hl) = temp;
          }
          else {
              temp-&gt;next = temp2;
              temp2-&gt;previous = temp;
              temp2-&gt;next = NULL;
              temp = temp-&gt;next;
              temp2 = (card *)malloc(sizeof(card));
          }
          j++;
      }
      i++;
  }
  }
  void printdeck(card *currentNode) {
  printf("\n\n\n");
  if (currentNode-&gt;suitnum == 1) {
      currentNode-&gt;suit = '♣';
  }
  else if (currentNode-&gt;suitnum == 2) {
      currentNode-&gt;suit = '♦';
  }
  else if (currentNode-&gt;suitnum == 3) {
      currentNode-&gt;suit = '♥';
  }
  else {
      currentNode-&gt;suit = '♠';
  }
  while (currentNode != NULL) {
      printf("Face: %d, Suit: %c\n", currentNode-&gt;face, currentNode- 
  &gt;suit);
      currentNode = currentNode-&gt;next;
  }
  }
  
  void shuffle(card *current) {
  int x = 1;
  while (x &lt; 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 &lt; 52) {
          int c = 0;
          int j = rand_gen(51);
          temp = current;
          while (c &lt; j) {
              temp = temp-&gt;next;
              c++;
          }
          int y = 0;
          temp2 = current;
          while (y &lt; i) {
              temp2 = temp2-&gt;next;
              y++;
          }
          temp3-&gt;face = temp2-&gt;face;
          temp3-&gt;suitnum = temp2-&gt;suitnum;
          temp2-&gt;face = temp-&gt;face;
          temp2-&gt;suitnum = temp-&gt;suitnum;
          temp-&gt;face = temp3-&gt;face;
          temp-&gt;suitnum = temp3-&gt;suitnum;
          i++;
      }
      x++;
  }
  }
  
  void drawhand(card **player, card **computer, card **headl) {
  //not done yet, need to fix the cards' values from going deleted
  int i = 0;
  card* temp = (card *)malloc(sizeof(card));
  temp = *headl;
  *player = temp;
  card* temp2 = (card *)malloc(sizeof(card));
  temp2 = temp-&gt;next;
  deletecard(temp, headl);
  *computer = temp2;
  card* temp3 = (card *)malloc(sizeof(card));
  temp3 = temp2-&gt;next;
  deletecard(temp2, headl);
  (*player)-&gt;next = temp3;
  card* temp4 = (card *)malloc(sizeof(card));
  temp4 = temp3-&gt;next;
  deletecard(temp3, headl);
  (*computer)-&gt;next = temp4;
  card* temp5 = (card *)malloc(sizeof(card));
  temp5 = temp4-&gt;next;
  deletecard(temp4, headl);
  (*player)-&gt;next-&gt;next = temp5;
  card* temp6 = (card *)malloc(sizeof(card));
  temp6 = temp5-&gt;next;
  deletecard(temp5, headl);
  (*computer)-&gt;next-&gt;next = temp6;
  card* temp7 = (card *)malloc(sizeof(card));
  temp7 = temp6-&gt;next;
  deletecard(temp6, headl);
  (*player)-&gt;next-&gt;next-&gt;next = temp7;
  card* temp8 = (card *)malloc(sizeof(card));
  temp8 = temp7-&gt;next;
  deletecard(temp7, headl);
  (*computer)-&gt;next-&gt;next-&gt;next = temp8;
  card* temp9 = (card *)malloc(sizeof(card));
  temp9 = temp8-&gt;next;
  deletecard(temp8, headl);
  (*player)-&gt;next-&gt;next-&gt;next-&gt;next = temp9;
  card* temp10 = (card *)malloc(sizeof(card));
  temp10 = temp9-&gt;next;
  deletecard(temp9, headl);
  deletecard(temp10, headl);
  (*player)-&gt;next-&gt;previous = *player;
  (*player)-&gt;next-&gt;next-&gt;previous = (*player)-&gt;next;
  (*player)-&gt;next-&gt;next-&gt;next-&gt;previous = (*player)-&gt;next-&gt;next;
  (*player)-&gt;next-&gt;next-&gt;next-&gt;next-&gt;previous = (*player)-&gt;next-&gt;next- 
  &gt;next;
  (*player)-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = NULL;
  (*computer)-&gt;next-&gt;previous = *computer;
  (*computer)-&gt;next-&gt;next-&gt;previous = (*computer)-&gt;next;
  (*computer)-&gt;next-&gt;next-&gt;next-&gt;previous = (*computer)-&gt;next-&gt;next;
  (*computer)-&gt;next-&gt;next-&gt;next-&gt;next-&gt;previous = (*computer)-&gt;next- 
  &gt;next-&gt;next;
  (*computer)-&gt;next-&gt;next-&gt;next-&gt;next-&gt;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, int i) {
  printf("\n\n\n");
  //not done yet, need to print the computer's three cards and 2 blank 
  spaces
  }
  void bank(int balance) {
  printf("Your balance is: %d coins.\n", balance);
  }
  int bet(int *balance) {
  int bet;
  printf("Place your bet (1-%d) coins: \n", balance);
  scanf("%d", &amp;bet);
  while (bet &gt; balance || bet &lt; 0) {
      printf("Place your bet (1-%d) coins: \n", balance);
      scanf("%d", &amp;bet);
  }
  balance = balance - bet;
  printf("You bet %d coins.  Now you have %d coins.\n", bet, balance);
  return bet;
  }
  void swap(card **player) {
  int i = 0;
  int array[5];
  int value;
  int y = 0;
  while (y != -1) {
      printf("Pick cards (between 1-5) to swap (-1 to stop):");
      scanf("%d", &amp;value);
      if (value == -1) {
          y = -1;
      }
      else if (value &lt;= 5 || value &gt;= 0){
          array[i] = value;
          i++;
      }
  }
  i = 0;
  /*while(array[i]!=NULL){
  card* temp = (card *)malloc(sizeof(card));
  i++;*/
  //not done yet, still need to swap cards
  }
  void computerswap(card **computer) { 
  //not done yet, need to do it all
  }
  int winner(card *player, card *computer) {
  //not done yet, need to do it all
  }
  
  int main(void) {
  char answer = 'y';
  while (answer == 'y' || answer == 'Y') {
      int x = 1;
      valueprint(x);
      int *balance=100;
      while (balance &gt; 0) {
          srand((int)time(NULL));
          card *headl = NULL;
          createdeck(&amp;headl);
          printdeck(headl);
          shuffle(headl);
          int betvalue;
          printdeck(headl);
          card* player = NULL;
          card* computer = NULL;
          drawhand(&amp;player, &amp;computer, &amp;headl);
          printf("Computer/Dealer's hand:\n");
          computerhand(computer, 1);
          printf("Your hand:\n");
          printdeck(player);
  bank(balance);
          betvalue = bet(balance);
          swap(player);
          computerswap(computer);
          printf("Your hand: \n");
          printdeck(player);
          printf("Computer/Dealer's hand:\n");
          computerhand(computer, 2);
          if (winner(player, computer) == 1) { //player
              balance = balance + betvalue;
              printf("You won!!! Your balance is now %d", 
  balance);
          }
          else if (winner(player, computer) == 2) { //computer
              balance = balance - betvalue;
              printf("you lost.  Your balance is now %d", 
  balance);
          }
          printf("Hit Enter key to continue: ");
          _getch();
  }
  }
  }
  </code></pre>