The Card Stack Data Structure
The CARDSTACK data structure
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
1
2
3
4
5
6
8
7
1
2
3
4
5
6
8
7
1
2
3
4
5
6
8
7
1
2
3
4
5
6
8
7
1
2
3
4
5
6
8
7
1
2
3
4
5
6
8
7
...after the riffle shuffle.
Introducing...
< maintain an ordered stack of cards >
The Data
how many cards in the stack?
Operations & Queries
pull out the top \(k\) cards
pull out the bottom \(k\) cards
cut shuffle at \(k\) cards
typedef struct s_card {
int cardvalue;
} t_card;
42
typedef struct s_card {
int cardvalue;
struct s_card *next;
struct s_card *prev;
} t_card;
last
first
typedef struct s_cardstack {
struct s_card *first;
struct s_card *last;
} t_cardstack;
t_cardstack* cardstackInit()
{
t_cardstack* cardstack;
cardstack = malloc(sizeof(t_cardstack));
cardstack->first = NULL;
cardstack->last = NULL;
return cardstack;
}
The initialization is pretty straightforward.
Next up...
...let us write a simple helper function
to check if our stack of cards is empty.
int isEmpty(t_cardstack *cardstack)
{
return !cardstack->first;
}
int isEmpty(t_cardstack *cardstack)
{
return cardstack->first;
}
int isEmpty(t_cardstack *cardstack)
{
return !cardstack->last;
}
int isEmpty(t_cardstack *cardstack)
{
return cardstack->last;
}
A
B
C
D
Here is what the card stack looks like, schematically.
first
last
pushFront(cardstack, card)
first
last
first
last
first
last
first
last
first
last
first
last
first
last
first
last
void pushFront(t_cardstack* cardstack, int cardvalue)
{
t_card *node = malloc(sizeof(t_card));
node->cardvalue = cardvalue;
node->prev = NULL;
node->next = cardstack->first;
if(cardstack->last) // the stack is NOT empty
cardstack->first->prev = node;
else
cardstack->last = node;
cardstack->first = node;
}
popBack(cardstack)
first
last
first
last
first
last
first
last
first
last
int popBack(t_cardstack *cardstack)
{
t_card *node;
int cardvalue;
if (isEmpty(cardstack))
return -1;
node = cardstack->last;
cardstack->last = node->prev;
if (!cardstack->last)
cardstack->first = NULL;
else
cardstack->last->next = NULL;
cardvalue = node->cardvalue;
free(node);
return cardvalue;
}
Front | Back | |
---|---|---|
Add | allowed | allowed |
Remove | allowed | allowed |
DEQUES
Front | Back | |
---|---|---|
Add | allowed | ❌ |
Remove | allowed | ❌ |
STACKS
Front | Back | |
---|---|---|
Add | ❌ | allowed |
Remove | ❌ | allowed |
STACKS
Front | Back | |
---|---|---|
Add | ❌ | allowed |
Remove | allowed | ❌ |
QUEUES
Front | Back | |
---|---|---|
Add | allowed | ❌ |
Remove | ❌ | allowed |
QUEUES
Any number of cut shuffles are done.
Five people pull out the top five cards in order.
A deck is given to someone in class.
Magic with Cards
The ones with red cards stand up.
All cards are identified.
The Card Identifier
X Y A B C
00 - ♣️ | 01 - ♠️ | 10 - ♦️ | 11 - ♥️
Five bits correspond to a card
A B C - value of the card (000 interpreted as 8)
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
The Sequence
Want: 32 bits so that each location gives us a unique read.
00000100101100111110001101110101
ABCD
WXYZ
When should you add this edge?