Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use a stack data structure to determine whether the string parameter satisfies the condition described above. You may define your own stack class or use the STL stack class. Write a driver program (i.e. a function main()) to test your function.
Deliverables:
1. a copy of your function and the test driver program source codes in a text file
2. screen shots of your test driver program runs showing it meets the problem's specifications. This is what I've done so far: I've copied the material from the book as well as from some sources on the internet. Could you edit my code and write it in C++. Some of my code is in Java.
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
// Stack template
template <class T>
class Queue
{
private:
T *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
//Constructor
Queue(int);
//Copy constructor
Queue(const Queue &);
//Destructor
~Queue();
//Queue operations
void enqueue(T);
void dequeue(T &);
bool isEmpty() const;
bool isFull() const;
void clear();
};
//This constructor creates an empty queue of a specified
size
template <class T>
Queue<T>::Queue(int s)
{
queueArray = new T[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
//Copy constructor
template <class T>
Queue<T>::Queue(const Queue &obj)
{
//Allocate the queue array.
queueArray = new T[obj.queueSize];
//Copy the other object's attributes.
queueSize = obj.queueSize;
front = obj.front;
rear = obj.rear;
numItems = obj.numItems;
//Copy the other object's queue array.
for (int count = 0; count < obj.queueSize;
count++)
queueArray[count] =
obj.queueArray[count];
}
//Destructor
template <class T>
Queue<T>::~Queue()
{
delete[] queueArray;
}
//Function enqueue inserts a value at the rear of the queue.
tmeplate <class T>
void Queue<T>::enqueue(T item)
{
if (isFull())
cout << "The queue is
full.\n";
else
{
//Calculate the new rear
position
rear = (rear + 1) %
queueSize;
//Insert new item
queueArray[rear] = item;
// Update item count
numItems++;
}
}
//Function dequeue removes the value at the front of the queue and copies t into num
template <class T>
void Queue<T>::dequeue(T &item)
{
if (isEmpty())
cout << "The queue is empy.
\n";
else
{
//Move front
front = (front + 1) %
queueSize;
//Retrieve the front item
item = queueArray[front];
// Update item count
numItems--;
}
}
template <class T>
bool Queue<T>::isEmpty() const
{
bool status;
if (numItems)
status = false;
else
status = true;
return status;
}
template <class T>
bool Queue<T>::isFull() const
{
bool status;
if (numItems < queueSize)
status = false;
else
status = true;
return status;
}
template <class T>
void Queue<T>::clear()
{
front + queueSize - 1;
rear = queueSize - 1;
numItems = 0;
}
//Driver program to test the above functions
public static void main(String[] args)
{
String exp1 = "{a(b+ac)d[xy],}, kab * cd";
if (areParenthesisBalanced(exp1.toCharArray()))
System.out.println("Balanced ");
else
System.out.println("Not Balanced ");
String exp2 = "ac)cd(e(k, xy{za(dx)k";
if
(areParenthesisBalanced(exp2.toCharArray()))
System.out.println("Balanced
");
else
System.out.println("Not Balanced ");
}
}
#endif
Program1.cpp
---------------------------------------------------------------------------------------------------------
#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to check whether two characters are opening
// and closing of same type.
bool ArePair(char opening,char closing)
{
if(opening == '(' && closing == ')') return
true;
else if(opening == '{' && closing == '}')
return true;
else if(opening == '[' && closing == ']')
return true;
return false;
}
//Function to check having matching symbols
bool haveMatchingSymbol(string exp)
{
stack<char> S; //Taking STL stack class as
mentioned that we can take this too
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.empty() ||
!ArePair(S.top(),exp[i]))
return false;
else
S.pop();
}
}
return S.empty() ? true:false;
}
//Driver function to check the conditions
int main()
{
string expression;
cout<<"Enter an expression: "; // input
expression from STDIN/Console
cin>>expression;
if(haveMatchingSymbol(expression))
cout<<"Balanced and having
balancing condition\n";
else
cout<<"Not Balanced\n";
}
-----------------------------------------------------------------------------------------------------------------------------------------
Screenshots of test driver program:-
Write a function that takes a string parameter and determines whether the string contains matching grouping...
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...
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...
Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets []. For example, the string {a(b+ac)d} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac)d} do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Write...
Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...
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;...
Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...
Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
PROBLEM: string CBQueue::dequeue( ) This method should remove and return the item at the front of the queue- please add comments EXISTING CODE: #include // this allows you to declare and use strings #include using namespace std; struct qNode { string data; qNode* next; qNode* prev; }; class CBQueue { public: CBQueue(); int CBQueue::getSize( ); bool CBQueue::isEmpty( ); private: qNode* front; qNode* rear; int size; }; #include "CBQueue.h" CBQueue::CBQueue() { front = NULL; rear = NULL; size = 0; }...
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...