Question

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 namis. push (、c, ) ; s .push (、d, ) ; s.push (e) s.push (、f, ) ; s.pushg) cout << end! << stack contents s.displayAll aftervoid push (el t)i void pop (el-t &); void getTop (el_t&) conat; void displayAl1) const; bool isEmpty const; // implement thes .pop (c): cout << endl << stack contents after popping 3 more elements: s.displayAl s.pop (c) <<endl .pop () s.push (a)

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

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

//main.cpp
#include "staticStack.h"
#include <iostream>

#define print(sent) cout << sent << endl //memes

using std::endl;
using std::cout;

int main()
{
    Stack s;
    character c;

    print("Initial stack contents:");
    s.displayAll();
    s.pop(c);
    s.push('a');
    print("Stack contents after pushing a: ");
    s.displayAll();
    print("");

    s.push('b');
    print("Stack contents after pushing b:");
    s.displayAll();
    print("");

    s.push('c');
    s.push('d');
    s.push('e');
    s.push('f');
    s.push('g');
    print("Stack contents after pushing c-g:");
    s.displayAll();
    print("");

    s.pop(c);
    print("Popping element: " << c);
    print("Stack contents after popping one element:");
    s.displayAll();
    print("");

    s.pop(c);
    print("Popping element: " << c);
    print("Stack contents after popping another element:");
    s.displayAll();
    print("");

    s.pop(c);
    s.pop(c);
    s.pop(c);
    print("Stack contents after popping 3 more elements:");
    s.displayAll();
    print("");

    s.pop(c);
    s.push('a');
    s.push('b');
    print("Final stack contents:");
    s.displayAll();

    return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "staticStack.h"
#include <iostream>

#define print(sent) cout << sent << endl

using std::cout;
using std::endl;

Stack::Stack()
{
    top = -1;
}

void Stack::push(character c)
{
    if (isFull())
    {
        print("Error(Stack Overflow): Stack is full, can not push new data to stack.\n");
        return;
    }
    else
    {
        top++;
        ar[top] = c;
    }
}

void Stack::pop(character &c)
{
    if (isEmpty())
    {
        print("Error(Stack Underflow): Stack is empty, can not pop data from stack.\n");
        return;
    }
    else
    {
        c = ar[top];
        top--;
    }
}

bool Stack::isEmpty()
{
    if (top <= -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Stack::isFull()
{
    if (top == MAX - 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Stack::displayAll()
{
    if (isEmpty())
    {
        print("[Empty] The stack is currently empty.\n");
    }
    else
    {
        for (int i = top; i >= 0; i--)
        {
            print(ar[i]);
        }
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------

#ifndef STATICSTACK_STATICSTACK_H
#define STATICSTACK_STATICSTACK_H

const int MAX = 5;
typedef char character;

class Stack
{
private:
    character ar[MAX];
    int top;
public:
    Stack();
    void push(character);
    void pop(character &);
    bool isEmpty();
    bool isFull();
    void displayAll();
};


#endif //STATICSTACK_STATICSTACK_H



EStaticStack [ /CL StaticStack] - ./main.cpp StaticStack main.cpp StaticStack DebugTG *-Δ cMakeLists.txt Project ▼ StaticStac

Add a comment
Know the answer?
Add Answer to:
Must be written in C++ Please Lab 11 Due Date: April 25, 2019 Total Points: 15...
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
  • 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;...

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

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

  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...

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

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

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

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • 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: ");...

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