Question

Build the following data structures in c++: STACK (Do not use the STL library for your...

Build the following data structures in c++: STACK (Do not use the STL library for your containers, PREFERRED IF YOU USED LINKED LIST OR ARRAYS )

C++

Stack should contain

  • Insertion (Push)

  • Deletion (Pop)

  • Print/Display

  • Clear/empty

  • Size (if empty, print that it’s empty)

  1. Fill the stack with 52 values representing cards in a deck, then print a 7-card hand.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I code given below will suffice your requirements.

Try putting the cards into the stack as :

4 of Ace - Denoting the card of the suit Ace with the number 4 written on it.

Similarly, you can insert all the cards according to your need.

NOTE: You can adjust the capacity of the cards that can be held at a time by changing the value of my_stack variable.

#include <iostream>
#include<string>
using namespace std;

string my_stack[52];
int my_size = 52, top_element = -1;
void insertion (string val)
{
if (top_element >= my_size - 1)
cout << "Stack Overflow" << endl;
else
{
top_element++;
my_stack[top_element] = val;
}
}

void deletion ()
{
if (top_element <= -1)
cout << "Stack Underflow" << endl;
else
{
cout << "The popped element is " << my_stack[top_element] << endl;
top_element--;
}
}

void display_stack ()
{
if (top_element >= 0)
{
cout << "Stack elements are:";
for (int i = top_element; i >= 0; i--)
   cout << my_stack[i] << ", ";
cout << endl;
}
else
cout << "Stack is empty" << endl;
}

void clear_stack ()
{
do
{
top_element--;
}
while (top_element != -1);
cout << "Stack has been cleared" << endl;
}

void display_size ()
{
int size = sizeof (my_stack) / sizeof (my_stack[0]);
cout << size <<" cards can be holded inside this stack"<< endl;
}

void display_size_stack ()
{
cout <<"Your stack contains " << top_element +1 << " cards"<<endl;
}

int main ()
{
int choice;
string value;
cout << "1: Insert in stack" << endl;
cout << "2: Delete from stack" << endl;
cout << "3: Display stack" << endl;
cout << "4: Clear stack" << endl;
cout << "5: Display size of array of stack" << endl;
//In your case array denotes 52 cards hence it is going to show 52 always.
cout << "6: Display size of stack" << endl;
//This is going to show number of elements currently in the stack.
cout << "7: Exit" << endl;
do
{
cout << "Enter choice: " << endl;
cin >> choice;
switch (choice)
   {
   case 1:
   {
   cout << "Enter value to be pushed:" << endl;
   cin >> value;
   insertion (value);
   break;
   }
   case 2:
   {
   deletion ();
   break;
   }
   case 3:
   {
   display_stack ();
   break;
   }
   case 4:
   {
   clear_stack ();
   break;
   }
   case 5:
   {
   display_size ();
   break;
   }
   case 6:
   {
   display_size_stack ();
   break;
   }
   case 7:
   {
   cout << "Exit" << endl;
   break;
   }
   default:
   {
   cout << "Invalid Choice" << endl;
   }
   }
}
while (choice != 7);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Build the following data structures in c++: STACK (Do not use the STL library for your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers) C++ Stack and Queue should contain Insertion (Push/Enqueue) Deletion (Pop/Dequeue) Print/Display Sort (Stacks: Value or Color, Queue: Alphabetical) Search (Contains, position, and how many instances as three separate functions) Clear/empty Size (if empty, print that it’s empty) Each data structure should contain a set of overloaded operators (Respect innate object behavior): ‘+’ (addition) that allows you to add two objects of the same...

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games...

    Computer Science 182 Data Structures and Program Design Programming Project #3 – Link List Card Games One of the nice things about Java is it is very easy to do fun things with graphics. The start code provided here will display a deck of cards on the screen and shuffle them. Your mission, is to start with this code and build a card game. Blackjack, poker solitaire, what ever your heart desires. Blackjack is the easiest. Obviously any program you...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

  • Build a bank simulator system using the C++ STL queue library. You are required to create your ow...

    build a bank simulator system using the C++ STL queue library. You are required to create your own ADT for that system that: First, Read from a file the data of incoming customers which includes ArrivalID, StartTime, and ProcessTime. You have one line in your Bank in which you will serve your customers as per to their arrival time. For example if you read the following data from the file A 1 5 B 2 5 C 4 5 D...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT