Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ For this project, you will create a program that will later be used as part

ID: 3606572 • Letter: C

Question

c++

For this project, you will create a program that will later be used as part of a magic trick. Your program will create a deck of playing cards. The user will be prompted to select a card from the list and that card will be put on the top of the deck.

Minimum Requirements:

Create a playing card node class and a deck of cards linked list class. You cannot use the STL list class for this assignment (5 points).

Include a function to add cards to the deck (5 points). The head node in the list represents the top of the deck. Each node will represent each playing card and should hold two private variables: a string containing the suit and another string with the value of a card. For example, the suit would include Spades, Hearts, Clubs and Diamonds. The values would include King, Queen, Ace, 2, 3, etc.

Include a display function in the deck class that displays the data from each card (5 points).

Include another function in the deck class that asks the user to select a card. Then, it.will find the selected card and move it to the top of the deck. This will move that card node from its original position in the list to the head of the list. (5 points)

The main method should call the add function to add a few cards to the deck for testing. It should then call the display function, then call the function that moves the card. Finally, it should call the display function again to show the deck with the selected card listed first.

Explanation / Answer

Solution:

card.h

#ifndef card_h
#define card_h
#include <iostream>
using std::string;
class card
{
private:
string suit;
string value;
card* next;//pointer to next card
public:
card(string suit, string value);
void setSuit(string suit);
void setValue(string value);
string getSuit();
string getValue();
card* getNext();
void setNext(card* n);
};

#endif /* card_h */

card.cpp


#include "card.h"
card::card(string suit, string value)
{
this->suit = suit;
this->value = value;
this->next = NULL;
}
void card::setSuit(string suit)
{
this->suit = suit;
}
void card::setValue(string value)
{
this->value = value;
}
string card::getSuit()
{
return suit;
}
string card::getValue()
{
return value;
}
card* card::getNext()
{
return next;
}
void card::setNext(card* n)
{
this->next = n;
}

deck.h

#ifndef deck_h
#define deck_h
#include "card.h"
class deck
{
private:
card* head;
public:
deck();
void addCard(string suit, string value);
void moveToTop(string suit, string value);
void select();
void display();
  
};


#endif /* deck_h*/

deck.cpp


#include "deck.h"
#include <iostream>
using namespace std;
deck::deck()
{
head = NULL;
}
void deck::addCard(string suit, string value)
{
card* c = new card(suit, value);
if(head == NULL)
head = c;
else
{
card* curr = head;
while(curr->getNext() != NULL)
curr = curr->getNext();
curr->setNext(c);
}
}
void deck::moveToTop(string suit, string value)
{
card* curr = head;
card* prev = NULL;
while(curr != NULL)
{
//check if this is the card being searched
if(curr->getSuit() == suit && curr->getValue() == value)
break;
prev = curr;
curr = curr->getNext();
}
  
if(curr == NULL) //not found
cout << " No card " << value << " of " << suit << endl << endl;
else
{
if(curr != head) //need to move only if its not the head node
{
prev->setNext(curr->getNext());
curr->setNext(head);
head = curr;
}
cout << " Moved " << value << " of " << suit << endl << endl;

  
}
  
}
void deck::select()
{
string suit, value;
cout << "Enter the details of the card to be moved: " << endl;
cout << "Enter suit: ";
cin >> suit;
cout << "Enter value: ";
cin >> value;
moveToTop(suit, value);
}
void deck::display()
{
card* curr = head;
cout << endl;
while(curr != NULL)
{
cout << curr->getValue() << " of " << curr->getSuit() << endl;
curr = curr->getNext();
}
cout << endl;
}

main.cpp


#include <iostream>

#include "deck.h"
using namespace std;
int main()
{
deck d;
d.addCard("Diamonds", "Ace");
d.addCard("Clubs", "5");
d.addCard("Hearts", "9");
d.addCard("Spades", "King");
d.addCard("Diamonds", "Jack");
  
cout << "The deck of cards is " << endl;
d.display();
  
d.select();
  
cout << "The deck of cards is " << endl;
d.display();

  
}

output

$ g++ card.cpp deck.cpp main.cpp
$ ./a.out
The deck of cards is

Ace of Diamonds
5 of Clubs
9 of Hearts
King of Spades
Jack of Diamonds

Enter the details of the card to be moved:
Enter suit: Spades
Enter value: King

Moved King of Spades

The deck of cards is

King of Spades
Ace of Diamonds
5 of Clubs
9 of Hearts
Jack of Diamonds

$ ./a.out
The deck of cards is

Ace of Diamonds
5 of Clubs
9 of Hearts
King of Spades
Jack of Diamonds

Enter the details of the card to be moved:
Enter suit: Hearts
Enter value: Ace

No card Ace of Hearts

The deck of cards is

Ace of Diamonds
5 of Clubs
9 of Hearts
King of Spades
Jack of Diamonds

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote