Question

USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS.

class StaticStack

{

private:

int *stack;

int capacity;

int top; // index of the top element

public:

StaticStack(int size); // constructor

~StaticStack();

bool isFull();

bool isEmpty();

void push(int);

int pop();

};

#include "StaticStack.h"

#include <iostream>

using namespace std;

StaticStack::StaticStack(int size)

{

stack = new int[size]; // constructing a size sized array

capacity = size;

top = -1; // empty stack

  

}

StaticStack::~StaticStack()

{

delete [] stack;

}

bool StaticStack::isFull()

{

return (top == capacity -1);

}

bool StaticStack::isEmpty()

{

return (top == -1);

}

void StaticStack::push(int data)

{

if (isFull())

{

cout << "The stack is currently full " << "\n";

}

else

{

top++;

stack[top] = data; // Generally -> keep careful because stuff on the right is assigned to stuff on the left

}

}

int StaticStack::pop()

{

int num = 0;

if (isEmpty())

cout << "The stack is empty";

else

{

top--;

num = stack[top+1];

}

return num;

}


2. (Gaddis Exercises 18.15) Balanced Multiple Delimiters A string may use more than one type of delimiter to bracket information into blocks. For example, A string may use braces3, parentheses (), and brackets [ ] as delimiters. A string is properly delimited if each right delimiter is matched with a preceding left delimiter of the same type in such a way that either the resulting blocks of information are disjoint, or one of them is completely nested within the other. Write a program that uses a single stack to check whether a string containing braces, parentheses, and brackets is properly delimited.

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

We write the as follows as

class StaticStack // as class

int *stack, top, capacity;// Variables as private scope only in class access

public  StaticStack(int size); // constructor

~StaticStack(); // destructor

// Here we take some field as

bool isFull(); // It checks whether stack is full or not it returns true or false

bool isEmpty(); //  It checks whether stack is empty or not it returns true or false

void push(int); // this function push the data into the stach using push integer type element.

int pop(); // this function throws back the data from the stack.

// We declare StaticStack::StaticStack(int size) using namespace and use scope resolution operator ( : : ).

// We implement the above function as push, pop, isFull, isEmpty.

StaticStack::~StaticStack() // this is destructor implement when object goes out of scope this //autometically called and delete stack object.

{  delete [] stack; }

******************************************************************************************************

2. Balanced Multiple Delimeters:

#include<iostream>

#include<stack>

#include<string>

using namespace std;

bool Pair(char begin, char end){

if( begin == '( ' && end ==')' )

return true;

else if(begin == '{' && end =='}')

return true;

else if( begin == '['&& end ==']')

return true;

return false; }

bool AreBalanced(String exp){

stack<char> S;

for(int i=0;i<exp.length();i++)

{

if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
S.push(exp[i]);

else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')

{

if(S.isEmpty() || ! Pair(S.top, exp[i]))

return false;

else

S.pop();

} }

return S.empty() ? true:false;

}

int main(){

String Expression;

cout<< " Enter your expression : ";

cin>> expression;

if(AreBalanced(expression))

cout<< " expression is balanced";

else

coout<<" expression is not balanced";

}

*****************************************************************************************************

Add a comment
Know the answer?
Add Answer to:
USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...
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
  • 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...

  • - 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 to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

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

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

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

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

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

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