(C ++)单一链接列表更改没有粘贴?

时间:2017-11-04 03:20:49

标签: c++ function linked-list

我在更改链接列表时遇到问题。这就是我到目前为止所拥有的

struct node 
{
  char suit;
  int value;
  node* next;
};

//Fill out the list with 52 playing cards
void fillWithCards(node *&head, node *&tail, node *&now, node *&temp)
{
  char suits [] = {'s', 'h', 'd', 'c'};
  int val [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
  vector <int> rand;

  for (int i = 0 ; i < 14 ; i++)
    rand.push_back(i);

random_shuffle (rand.begin(), rand.end());

srand(time(NULL));

for (int x = 0; x < 4 ; x++)
{
    for (int y = 0 ; y < 13 ; y++)
    {
        node *now = new node;
        now -> suit = suits [x];
        now -> value = val [y]; //rand.at(y)

        if (*&head == NULL && *&tail == NULL && *&temp == NULL)
        {               
            head = now;
            tail = now;
            temp = now;
        }
        else if (*&tail != NULL && *&temp != NULL)
        {
            tail -> next = now;
            temp -> next = now;
            tail = now;
            temp = now;
        }
        else 
        {
            tail -> next = NULL;
        }
     }
  }
}

//Print the list 
void print(node *head)
{
node *n = head;
while (n)
{
    cout << n-> suit;
    cout << n-> value << ", ";
    n = n -> next;
 }
}

//Remove the head of the list 
void removeHead (node *head)
{
  node *h = head;

  head = head -> next;
  h -> next = NULL;


  delete h;

  print (head);
}

问题在于,例如,如果我要调用我的函数来移除头部,它会这样做,我可以在removeHead中调用我的打印函数,它会打印出更改。

但是,如果我在调用main后调用removeHead中的打印功能而不是打印出更改后的列表,则会打印出原始列表。

所以我觉得使用我的removeHead参数,它会复制我的列表并更改复制的列表而不是实际列表。但是,我不确定。

感谢您的帮助!

0 个答案:

没有答案