Question

In: Computer Science

In this lab, using C++, you will create two data structures: a stack and a queue....

In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs.


Queue


For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the generated number in the buffer. For example, if the user enters 25, then there is a 25% chance of the randomly generated number been put into the queue. You will have another user specified percentage, which is the chance of a value being removed from the buffer. For example, if the user enters 50, then there is a 50% chance of the number in the buffer been removed in this round.
You need to record the length of the queue in each round. After each round of the simulation, calculate and display the average length of the queue. Equation to calculate the average length would be: ALi = (ALi-1 * (i-1) + Li ) / i, where ALi and ALi-1 are the average length in the ith and i-1th round and Li is queue length in the ith round. Does your buffer behave as expected, i.e. get longer if the input chance is greater, and get shorter if the removing chance is greater? Your program needs to handle the situation when the queue is empty and still try to remove a value. You can start the simulation with a queue of certain length.


Stack


Use a stack to create a function that creates a palindrome, i.e. a string that is the same forwards and backwards. It does not need to be an actual word. The function will receive a string from user input and it will return the string with the palindrome. For example, if you enter a string “abc”, your program will return “abccba”.


Testing program


Create a menu program for the user to test your buffer and to create a palindrome. For the queue, prompt the user to enter the two chances and the total number of rounds. Display the results to the screen in each round. For the stack, prompt them to enter a string. Create the palindrome and then display it. You can determine how this menu program should look like as long as it test the queue and stack properly


What to submit
 Code to implement your stack, both header and source files
 Code to implement your queue, both header and source files
 Code to test the operation of your stack and queue.

Solutions

Expert Solution

#include <queue>
#include <stack>
#include <time.h>
#include<iostream>
#include <string>
using namespace std;


int RandomNumber()
{
   srand (time(NULL));
   return rand() % 100 + 1;
}

void QueueGame()
{
   queue<int> buffer;
   int simulations;
   cout<<"Enter number of simulations:";
   cin>>simulations;
   int a=0; //avg lenght in ith round
   int b=0; //avg lenght in i-1 th round
   //fill the buffer with some initial data.
   for( int i = 1; i <= 25; i++)
   {
       buffer.push(i);
   }

   for( int i = 1; i <= simulations; i++)
   {
       int push,pop;
       cout<<"Enter Push %:";
       cin>>push;

       cout<<endl<<"Enter Pop %:";
       cin>>pop;

       int random = RandomNumber() ;
       if ( random <= push )
       {
           buffer.push(random);
       }

       if ( random <= pop )
       {
           if (buffer.size() == 0)
           {
               cout<<"Buffer is empty!!";
           }
           else
               buffer.pop();
       }

       a = ( b * (i-1));
       a += buffer.size() ;
       a/= i;
       cout <<endl<<"Average Length in Round "<<i<<" is "<<a<<endl;
       b = a;
   }
}

void StackGame()
{
   string str;
   cout<<"Enter a string to make it pallindrome:";
   getline(cin,str);
   getline(cin,str);
   stack<char> bucket;

   for( int i=0; i<str.length(); i++)
   {
       bucket.push(str[i]);
   }

   string reverse;
   //print reverse
   for( int i=0; i<str.length(); i++)
   {
       reverse += bucket.top();
       bucket.pop();
   }

   cout<<"Pallindrome:"<<str<<reverse<<endl;
}

void main()
{
   int game_num;
   bool choice = false;

   do {
       cout<< "\n\n1. Queue \n2. Stack\nEnter your choice:";
       cin>>game_num;
       switch (game_num)
       {
       case 1: QueueGame();
           break;
       case 2: StackGame();
           break;
       }
       cout<<"Play again? (0 = No, 1 = Yes): ";
       cin>>choice;
   }
   while(choice);
}


Related Solutions

C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data...
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data structure. Your queue should fully implemnted the following methods: first, push_back (enqueue), pop_front (dequeue), size, and isEmpty. Make sure to include a driver to test your newly implemented queue
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
Are you able to implement a stack using just one queue? If yes, please provide the...
Are you able to implement a stack using just one queue? If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If no, explain.
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
In java thanks! Design a Stack that is composed ONLY of one or two Queue objects...
In java thanks! Design a Stack that is composed ONLY of one or two Queue objects ergo the ONLY instance variables that exist in this stack are queues. Stack class should contain the following methods: Print Pop Push Top Size isEmpty copy [(2)] Design a Queue that is composed ONLY of two Stacks objects ergo the ONLY instance variables that exist in this queue are stacks. Queue class should contain the following methods:   Print Enqueue Dequeue Front Rear Size isEmpty...
//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef...
//Source: Gilberg et al., Data Structures: A Pseudocode Approach with C //Queue ADT Type Defintions typedef struct node { void* dataPtr; struct node* next; } QUEUE_NODE; typedef struct { QUEUE_NODE* front; QUEUE_NODE* rear; int count; } QUEUE; //Prototype Declarations QUEUE* createQueue (void); QUEUE* destroyQueue (QUEUE* queue); bool dequeue (QUEUE* queue, void** itemPtr); bool enqueue (QUEUE* queue, void* itemPtr); bool queueFront (QUEUE* queue, void** itemPtr); bool queueRear (QUEUE* queue, void** itemPtr); int queueCount (QUEUE* queue); bool emptyQueue (QUEUE* queue); bool fullQueue...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT