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...

  • (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...

  • 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:...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

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