Array of Linked Lists?

I'm having issues trying to convert my code into an array of linked lists. I currently have a program that generates random cards until all the different face cards have been seen at least twice for a given suit. To simplify the code, I must add an array of linked lists: the length of the array is fixed at 4 for each suit and the length of each linked list varies from suit to suit (depending on the value of cards drawn). Each new card must be added to the list through the use of an insert() function as well. If you have any advice on creating an array of linked lists, it would be much appreciated! I have supplied my code down below as a reference.




C++ Code:
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int RandomNumberGenerator();
//Function defined below int main()



int main() {
srand(time(0));
int random = rand();
int random_number;

int spade[13] = {0};
int heart[13] = {0};
int diamond[13] = {0};
int club[13] = {0};
//Creates enough variables to form a deck of 52 unique cards

bool s = false;
bool h = false;
bool d = false;
bool c = false;
//A bool variable for each suit to test for the termination rules

while (true)
{
spade[RandomNumberGenerator()-1] = spade[RandomNumberGenerator()-1]+1;
heart[RandomNumberGenerator()-1] = heart[RandomNumberGenerator()-1]+1;
diamond[RandomNumberGenerator()-1] = diamond[RandomNumberGenerator()-1]+1;
club[RandomNumberGenerator()-1] = club[RandomNumberGenerator()-1]+1;
//generates random cards using the function defined below int main

if (spade[10]>=2 && spade[11]>=2 && spade[12]>=2) {
s = true;
break;
}


if (heart[10]>=2 && heart[11]>=2 && heart[12]>=2) {
h = true;
break;
}


if (diamond[10]>=2 && diamond[11]>=2 && diamond[12]>=2) {
d = true;
break;
}


if (club[10]>=2 && club[11]>=2 && club[12]>=2) {
c = true;
break;
}
//if statements check for termination boundaries for each suit
}


//card values are displayed and each suit is checked for the termination output

return 0;

}

int RandomNumberGenerator() {
return rand() % 13 + 1;
}
//Function returns a random number between 1 and 13
is just a little syntax heavy:

list<type> arrlist[4];
arrlist[0] //first list.... etc

… are you supposed to make your own linked list here? That is a whole new bunch of code...
Last edited on
yes, I am supposed to have a single linked list. He has given some hints...

1) Hint: You are using a single linked list which means you cannot go back once you have a match on the rank index. One option is to look ahead instead of advancing and then taking a look a the rank index. Another option is to maintain two pointers, namely, one pointing to previous node and one pointing to current node.

2) Hint: The list::insert() function is where the fun work takes place. The function takes a single integer argument, namely, the rank index of a card where index refers to the position of the rank in the global rank string array. A new node is created and added to the front of the list that stores the rank index value. A search is then carried out to see if the rank index value is found downstream from the new node; if so, the older node is removed from the list.
if you write your own list, you hopefully have been following your studies on pointers.

the basic idea is this:

struct list
{
int data; //whatever data the list holds.
list* next; //this is what makes it work, a pointer to its own type,
};


the idea being that the last item's next pointer is null.
so you have something like
list* head;

//bunch of code to insert and so on, say you insert 10 items

cout << head->next->next->next->data; //its all chained together.
did you cover any of this in your book or class???

Last edited on
Registered users can post here. Sign in or register to post.