(c) Given a deck of cards stored in a singly linkedList, write code that deals t
ID: 3590547 • Letter: #
Question
(c) Given a deck of cards stored in a singly linkedList, write code that deals two hands where each hand is half the cards. It should accomplish this by dealing the first half of the cards into handl and the rest into hand2. The deck will be empty when the dealing is completed. You must use an iterator to complete this task. As an example if deck contains cards [2,3,4,5], at the end of dealing, handl should contain (2,3), and hand2 should contain (4,5) and the deck is empty. You can assume that deck starts with an even number of cards, that the LinkedList has method size() that returns the size of the list, and it implements the Iterable interface LinkedList deck; //There is code here to place cards in deck. You do not need to worry about this pa LinkedList hand1; LinkedList hand2; //write your code to do deal the hands beIow 7Explanation / Answer
//This is just piece of code to deal cards
//code to deal cards to hand1 and hand2
for (int i = 0; i < size; i++)
{
if (i < size/2 )
hand1.insert(deck[i]);
if ( i >= size/2)
hand2.insert(deck[i]);
}
-------------------------------------------------------------------------------------
//but to understand program better I have written whole code to show the results
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
template<class T>
class LinkedList
{
Node *head;
int size;
public:
LinkedList()
{
head = NULL;
size = 0;
}
void insert(T num)
{
Node *newNode, *cur = head;
newNode = new Node;
newNode->data = num;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
size++;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newNode;
size++;
}
}
void print()
{
Node *cur=head;
while (cur != NULL)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
int getSize()
{
return size;
}
int operator[](int count)
{
Node *cur = head;
for (int i = 0; i < count; i++)
cur = cur->next;
if (cur != NULL)
return cur->data;
}
};
int main()
{
typedef int Card;
int card[4] = { 2,3,4,5 };
LinkedList<Card> deck;
for (int i = 0; i < 4; i++)
deck.insert(card[i]);
cout << "Deck has these cards: ";
deck.print();
LinkedList<Card> hand1;
LinkedList<Card> hand2;
//insert first half into hand1 and second half into hand2 from deck
int size = 4;
size = deck.getSize();
//code to deal cards to hand1 and hand2
for (int i = 0; i < size; i++)
{
if (i < size/2 )
hand1.insert(deck[i]);
if ( i >= size/2)
hand2.insert(deck[i]);
}
//now print hand1 and hand2
cout << "Hand1 has these cards after deal: ";
hand1.print();
cout << "Hand2 has these cards after deal: ";
hand2.print();
}
-------------------------------------------------------------
//output
Deck has these cards: 2 3 4 5
Hand1 has these cards after deal: 2 3
Hand2 has these cards after deal: 4 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.