Linked list and pointers

I'm having troubles doing a shuffle for a linked list. I've tried numerous ways to make this work but I'm having issues. What I believe to be the problem with it now is that in the for loop when I make the next and previous portions of the list Null since its pointing to the actual head list it also makes it null then once it iterates through again its now set to null and breaks. How can I go about correcting this? Do I need to have more copies of the list? Or is there a completely better way of doing this?

http://pastebin.com/EW1j9au1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
bool Shuffle(Card* &head, Card* &tail, int ShuffleAmnt)
{

	Card* temp_tail = nullptr;
	Card* temp_head = head;
	Card* temp_list = new Card;
	for (int i = 1; i < 26; i++)
		temp_head = temp_head->next;




	/*
	temp_tail = temp_head->next;

	temp_tail->previous = nullptr;

	temp_head->next = nullptr;

	while (head->next != NULL)
		head = head->next;
	

	temp_head->previous = nullptr;
	temp_tail->next = nullptr;
	for (int i = 1; i < 26; i++)
	{

		temp_list->next = temp_head;
		temp_head = head->previous;
		temp_list = temp_list->next;
		temp_list->next = temp_tail;
	}

	head = temp_list;
	*/


	return(true);
}
Last edited on
What is the "shuffling" supposed to achieve?

Is it something entirely different from http://www.cplusplus.com/reference/algorithm/shuffle/
My bad. Whats suppose to happen which I didn't realize until now is that you split the list in half and you now have two lists. Left one and a right one. You take the bottom card off the left one and put it at the bottom of a new list, then you take the card off the bottom of the right one and add it on top of the new list.


So if you have left list = 1, 2 , 3 and right list = 4 , 5 ,6 when you shuffle the list they turn into.

Shuffled list = 3, 6 , 2, 5 , 1, 4
Last edited on
"Faro shuffle"?

Anyway, abstraction could help. Make logic that operates with lists rather than nodes. Then implement the necessary list operations.

1. You have list A. Create empty lists B and C.
2. Move second half of A into C and first half into B. A is now empty.
3. Append(move) head of B into A. Append(move) head of C into A.
4. Repeat 3 until B and C are empty.

Do not assume that input (A) has exactly 52 cards.

Steps 2 and 3 split list in two. Step 3 is special version, because it separates only the first node. Step 3 appends a node to a list.


PS. Please explain your line 6. What is that card? A joker?
I was attempting to generate an empty list to fill with the cards from the head and tail on line 6. Ill give it another shot, thanks for the input.
I can get them into two separate lists and start setting them into a new list. I can get the first two cards shuffled right but as I'm going through it the lists get jumbled. I just cant understand this stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool Shuffle(Card* &head, Card* &tail, int ShuffleAmnt)
{
	Card* temp_tail = nullptr;
	Card* temp_head = head;
	Card* temp_list = new Card;
	for (int i = 1; i < 26; i++)
		temp_head = temp_head->next;
	
	temp_tail = temp_head->next;

	temp_tail->previous = nullptr;

	temp_head->next = nullptr;

	while (head->next != NULL)
	{
		head = head->next;
		temp_tail = temp_tail->next;
	}
	bool FirstPass = true;
	for (int i = 1; i < 26; i++)
	{
		if (FirstPass == true)
		{
			temp_list = temp_head;
			temp_list->next = nullptr;
			temp_list->previous = temp_tail;
			temp_list->previous->next = temp_head;
		}
		temp_head = temp_head->previous;
		temp_tail = temp_tail->previous;
	}

	return(true);
}
Think about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef std::list<Foo> List;
void Shuffle( List & A ) {
  List B;
  List C;
  B.splice( B.begin(), A, A.begin(), A.begin() + A.size() / 2 );
  C.splice( C.begin(), A );

  while ( ! B.empty() && ! C.empty() ) {
    A.splice( A.end(), B, B.begin() );
    A.splice( A.end(), C, C.begin() );
  }
  A.splice( A.end(), B );
  A.splice( A.end(), C );
}

Is it logically correct?
If yes, how would you translate the operations into your Card-based list?
Yeah we haven't even touched base on list containers. I have no clue what any of that means.
The good thing about standard library is that it is documented. For example,
http://www.cplusplus.com/reference/list/list/splice/

Does that give you any ideas?
Topic archived. No new replies allowed.