Question

In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

In C++

Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions.

You could use either the array or linked list implementation for stacks and queues.

Source for stack array:

---------------------------------------------------

#include<iostream>
#define SIZE 100
#define NO_ELEMENT -999999
using namespace std;

class Stack {
int arr[SIZE]; // array to store Stack elements
int top;
public:
Stack() {
top = -1;
}

void push(int); // push an element into Stack
int pop(); // pop the top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};

void Stack :: push(int data) {
//if stack is full tell the user sorry it is full
if (top == SIZE-1) {
cout << "Sorry, the stack is full" << endl;
} else {
top++;
arr[top] = data;
}
}


int Stack :: pop() {
//if stack is empty tell the user sorry it is empty
if (top == -1) {
cout << "Sorry, the stack is empty" << endl;
return NO_ELEMENT;
}
int val = arr[top];
top--;
return val;
}

int Stack :: topElement() {
//if stack is empty tell the user no it is empty
if (top == -1) {
cout << "Sorry, the stack is empty" << endl;
return NO_ELEMENT;
}
return arr[top];
}

void Stack :: display() {
cout << "Stack display:" << endl;
for (int i = top ; i >=0 ; i--) {
cout << arr[i] << endl;
}
}

int main() {
Stack s;
s.push(12);
s.push(5);
s.push(13);
s.display();
cout << "Top Element : " << s.topElement() << endl;
s.pop();
s.push(7);
s.display();
return 0;
}

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

#include <iostream>

#define SIZE 100

#define NO_ELEMENT -999999

using namespace std;

class Stack {

int arr[SIZE]; // array to store Stack elements

int top;

public:

Stack() {

top = -1;

}

void push(int); // push an element into Stack

int pop(); // pop the top element from Stack

int topElement(); // get the top element

void display(); // display Stack elements from top to bottom

int isEmpty();

};

void Stack :: push(int data) {

//if stack is full tell the user sorry it is full

if (top == SIZE-1) {

cout << "Sorry, the stack is full" << endl;

} else {

top++;

arr[top] = data;

}

}

int Stack :: isEmpty() {

return top==-1;

}


int Stack :: pop() {

//if stack is empty tell the user sorry it is empty

if (top == -1) {

cout << "Sorry, the stack is empty" << endl;

return NO_ELEMENT;

}

int val = arr[top];

top--;

return val;

}

int Stack :: topElement() {

//if stack is empty tell the user no it is empty

if (top == -1) {

cout << "Sorry, the stack is empty" << endl;

return NO_ELEMENT;

}

return arr[top];

}

void Stack :: display() {

cout << "Stack display:" << endl;

for (int i = top ; i >=0 ; i--) {

cout << arr[i] << endl;

}

}


struct Queue {

Stack stack1, stack2;

void enQueue(int x)

{

while (!stack1.isEmpty()) {

stack2.push(stack1.pop());

}

stack1.push(x);

while (!stack2.isEmpty()) {

stack1.push(stack2.pop());

}

}

int deQueue()

{

if (stack1.isEmpty()) {

cout << "Queue is Empty";

return -1;

}

int x = stack1.pop();

return x;

}

};

int main()

{

Queue q;

q.enQueue(10);

q.enQueue(22);

q.enQueue(45);

cout << q.deQueue() <<endl;

cout << q.deQueue() << endl;

return 0;

}

=================================================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...
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
  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • I need to modify my C++ code so it can get the min value of the...

    I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() {   top = -1; } void push(int x) {   if (top == MAX_SIZE - 1) {    cout << "Stack is Full." << endl;    return;   }   S[++top] = x; } void pop() {   if (top == -1) {    cout << "Stack is...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15...

    Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...

  • Can I get a C++ code and output for this program using classes instead of using...

    Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack. ( I want to use class for function definitions too and if main need.) #include <iostream> using namespace std; const int MAX = 100; struct stack {    int s[MAX]; // an array of integers    int top; // the index of the last number }; void push(stack &, int); void pop(stack&,...

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