Question

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 :: 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;

}

queueARRAY:

#include<iostream>

#define SIZE 100

using namespace std;

class Queue {

int arr[SIZE];

int front, rear;

public :

Queue() {

front = rear = -1;

}

void enqueue(int); // insert an element into queue

int dequeue(); // Remove the front element from queue

void display(); // display the queue elements

};

void Queue :: enqueue(int data) {

//queue is empty

if (rear == -1) {

rear = 0;

front = 0;

arr[rear] = data;

}

//queue is full

else if (rear == SIZE-1) {

cout << "Queue is full" << endl;

} else {

rear++;

arr[rear] = data;

}

}

int Queue :: dequeue() {

//queue is empty

if (front == -1) {

cout << "Queue is empty";

return -1;

} else {

//queue has only one element

if (front == rear) {

int val = arr[front];

front = rear = -1;

return val;

} else {

int val = arr[front];

front++;

return val;

}

}

}

void Queue :: display() {

if (rear == -1) {

cout << "Empty queue!" << endl;

return;

}

cout << "Displaying the queue" << endl;

for (int i = front ; i <= rear; i++) {

cout << i << " " << arr[i] << endl;

}

}

int main() {

Queue q;

q.display();

q.enqueue(7);

q.enqueue(11);

q.enqueue(8);

q.enqueue(2);

q.display();

q.dequeue(); // 7 is dequeued

q.dequeue(); // 11 is dequeued

q.enqueue(5);

q.display();

return 0;

}

Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions You could use either th

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;

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

return 0;

}

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

main.cpp曰 saved ▼ https://Wonde 63 cLang versi 64 struct Queue Stack stackl, stack2; 65 cLang++- ./main 10 67 void enQueue(in

main.cpp Esaved https://Wonderfullnc 84 clang version 7.0 int x = stack! pop(); 85 86 return x; cLang++-pth ./main 10 87 89
Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...
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
  • 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...

  • //Look for** to complete this program --C+ please --please add comments // using namespace std; #include...

    //Look for** to complete this program --C+ please --please add comments // using namespace std; #include <iostream> #include <stdlib.h> #include < string> #include "queue.h" //Purpose of the program: ** Algorithm: * int main() /** "A", "B", "C" in the queue //** while loop -- indefinitely { try {//** catches } //* end of loop } |/ II II II II II //File type: ** queue.cpp using namespace std; #include <iostream> #include "queue.h" // constructor queue::queue () } //destructor queue::~queue() queue::queue...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

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

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

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

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

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