Question

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

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

#include<bits/stdc++.h>
using namespace std;

class Stack
{
public:      
   char stack[25];                           //declaring stack array in class Stack
   int top=-1;
  
   void push(char element)                   //push function
   {
       top++;
       stack[top]=element;
   }
  
   char pop()                               //pop function
   {
       char element=stack[top];
       top--;
       return element;
   }
  
   void print()                           //function for printing topmost element of stack
   {
       cout<<stack[top];
   }
};

class Queue
{
public:      
   char queue[25];                           //declaring queue array in class Queue
   int front=0,rear=0;
  
   void enqueue(char element)               //enqueue function
   {
   queue[rear] = element;
rear++;
   }
  
   char dequeue()                            //dequeue function
   {
       char element=queue[front];
   queue[front] = 0;
front++;
return element;
   }
  
   void print()                           //function for printing first element in queue
   {
       cout<<queue[front];
   }
};

int main()
{
   for(int i=1;i>0;i++)
   {
       string x;
       cout<<"Enter '1' to send a message or '2' to exit program and press Enter key(without quotes)"<<endl;
       getline(cin,x);
      
       if(x[0]=='1')
       {
           string s;
           Stack S;
           Queue Q;
           cout<<"Enter message to be send : ";
           getline(cin,s);
           int L=s.length();
          
           for(int i=0;i<L;i++)                       //message stored in queue(buffer according to question)
               Q.enqueue(s[i]);
              
           cout<<"Content of Queue : ";
           for(int i=0;i<L;i++)
           {
               Q.print();                               //printing the contents of Queue
               char element=Q.dequeue();               //transforming the message using Queue and Stack
               S.push(element);
           }
           cout<<endl;
          
           cout<<"Content of Stack : ";
           string transformed_message[L];
           for(int i=0;i<L;i++)
           {
               S.print();                               ////printing the contents of Queue
               transformed_message[i]=S.pop();
           }
           cout<<endl;
       }
       else
           break;
   }
}

Add a comment
Know the answer?
Add Answer to:
In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...
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
  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a firs...

    Using C++ Requirements Note: “deque” STL container is not allowed, for the simplicity for graders. Queue Queue is a first-in-first-out (FIFO) data structure. For the Queue container, we will implement a program that simulates a buffer. The simulation needs a function that randomly generate number. And the buffer simulation starts with no value in the buffer. At the start of simulation, the menu should ask user: how many rounds will be simulated. the percentage chance to put a randomly generated...

  • In C Write a function that asks for the user's first, middle, and last names. The...

    In C Write a function that asks for the user's first, middle, and last names. The names will be entered by the user on a single line and will be stored in three different C-strings. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example, if the user entered...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • Use c-strings for the following project: Write a C++ program that declares an array containing up...

    Use c-strings for the following project: Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered. Then, one by one, display each sentence entered by the user and present the following menu of operations on...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

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