通过引用传递链表的结构

时间:2019-04-22 09:08:32

标签: c pointers linked-list

我正在做一个最终项目,该项目要求实现纸牌游戏的链表。发牌人的手和玩家的手都应链接到列表以及纸牌组中。

我遇到的问题是,当我尝试创建一个向其添加值(卡片组列表末尾的卡)的函数时。自身传递列表及其尾部不会更新其值。我可以使用该函数,使其返回头指针,但随后我将无法留意其尾部。

我真的很抱歉,因为这对我来说是新事物,我一生中从未尝试过编程。如果我的函数中的某些逻辑看起来不必要地难以理解,或者直截了当没有意义,请原谅我。

我已经竭尽全力尝试使这项工作变得可行,但是我感觉自己在做一些根本错误的事情

typedef struct card_s { 
    char suit[20]; 
    int face; 
    struct card_s *next, *previous; 
} card;

card* createHands(card* head, card *cards) {
    card *tail = NULL, *temp = NULL, *temp1;

    // Go to end of deck
    while (cards->next != NULL) {
        cards = cards->next;
        }

        temp = (card *)malloc(sizeof(card));
        strcpy(temp->suit, cards->suit);
        temp->face = cards->face;
        if (head == NULL) { // If the list for the hand doesn't exist, create head
            head = temp;
        }
        else {
            tail->next = temp;
        }
            tail = temp;
            tail->next = NULL;

        temp1 = cards->previous; 
        free(cards); // to delete the node added from the deck

        cards = temp1;
        cards->next = NULL;
        while (cards->previous != NULL) {
            cards = cards->previous;
        }

    return head;

}

很明显,这只会添加我给它的所有值而超过之前的值。它绝不是一个已连接的列表。

我将不胜感激!

2 个答案:

答案 0 :(得分:0)

您的代码中存在几个明显的问题:

  • while (cards->next != NULL)假定 cards 不是NULL
  • 如果 head 不为NULL,则执行tail->next = temp;,但 tail 为NULL
  • 您从卡中释放了最后一个单元格,而没有更新该列表,因此它的最后一个元素现已释放

如果我很好地理解 createHands 的第二个参数是套牌,则目标是提取其最后一张牌以将其添加到发牌人/玩家列表中并返回其新头。

>

请注意,提取最后一张卡片时,您需要将其牌组设为card **,才能将该列表设置为NULL。对于发牌者/玩家的列表,也可以采用相同的方法,但是显然,您更愿意返回新的头,而不是将第一个参数设为card ** head

假设必须将卡添加到头部,解决方案可以是:

card * createHands(card * head, card ** deck) {
  if (deck == NULL)
    // no available card
    return head;

  // Go to end of deck
  while ((*deck)->next != NULL) {
    deck = &(*deck)->next;
  }

  card * c = *deck; // the extracted card

  *deck = NULL; // remove it from the deck

  c->previous = NULL;

  if (head != NULL) {
    c->next = head;
    head->previous = c;
  }

  return c;
}

要检查的完整程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct card_s { 
    char suit[20]; 
    int face; 
    struct card_s *next, *previous; 
} card;

card * createHands(card * head, card ** deck) {
  if (deck == NULL)
    // no available card
    return NULL;

  // Go to end of deck
  while ((*deck)->next != NULL) {
    deck = &(*deck)->next;
  }

  card * c = *deck; // the extracted card

  *deck = NULL; // remove it from the deck

  c->previous = NULL;

  if (head != NULL) {
    c->next = head;
    head->previous = c;
  }

  return c;
}

// check

card * mk(const char * s, card * n)
{
  card * c = malloc(sizeof(card));

  strcpy(c->suit, s);
  c->next = n;

  if (n != 0)
    n->previous = c;

  return c;
}

void pr(const char * who, card * c)
{
  printf("%s cards :", who);
  while (c != NULL) {
    printf(" %s", c->suit);
    c = c->next;
  }
  putchar('\n');
}

int main()
{
  card * deck = mk("one", mk("two", mk("three", mk("four", mk("five", NULL)))));
  card * dealer = NULL;
  card * player = NULL;

  pr("deck", deck);

  puts("->dealer");
  dealer = createHands(dealer, &deck);
  pr("deck", deck);
  pr("dealer", dealer);

  puts("->player");
  player = createHands(player, &deck);
  pr("deck", deck);
  pr("player", player);

  puts("->dealer");
  dealer = createHands(dealer, &deck);
  pr("deck", deck);
  pr("dealer", dealer);

  puts("->player");
  player = createHands(player, &deck);
  pr("deck", deck);
  pr("player", player);

  puts("->dealer");
  dealer = createHands(dealer, &deck);
  pr("deck", deck);
  pr("dealer", dealer);

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall -g l.c
pi@raspberrypi:/tmp $ ./a.out
deck cards : one two three four five
->dealer
deck cards : one two three four
dealer cards : five
->player
deck cards : one two three
player cards : four
->dealer
deck cards : one two
dealer cards : three five
->player
deck cards : one
player cards : two four
->dealer
deck cards :
dealer cards : one three five

答案 1 :(得分:0)

对功能应该做的事情做很多假设

typedef struct card_s { 
    char suit[20]; 
    int face; 
    struct card_s *next, *previous; 
} card;

您想在手中添加卡

card* AddCard(card* hand, card* draw) {
    // check input
    if(draw == NULL ) {return NULL;} 

    // create a copy of the card
    card* newCard=malloc(sizeof(card));
    if(newCard == NULL) {return hand;} // check malloc result
    memcpy(newCard, draw, sizeof(card));
    newCard->next=NULL;
    newCard->previous=NULL;

    if(hand==NULL) {return newCard;} // if the hand was null just return the card

    // find where to append the card
    card* tail=hand; 
    while(tail->next != NULL) {tail = tail->next;}
    // append the card to the end of the hand
    tail->next = newCard;
    newCard->previous = tail;
    return hand;
}