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

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

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

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

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

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

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

  • I'm trying to implement a queue using an array within C++. I cannot for the life...

    I'm trying to implement a queue using an array within C++. I cannot for the life of me figure out why some of my methods are not working properly. My exists method does not find a value that's in the queue and my duplicate method is duplicating the rear of my queue instead of the front. The duplicate method should take whatever is at the front of the queue (given it's not empty or full), copy that value, and put...

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

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