In this assignment, you will be using the Stack abstract data type we developed this week and discussed in our weeks lectures, to implement 4 functions that use a stack data type to accomplish their algorithms. The functions range from relatively simple, straight forward use of a stack, to a bit more complex. But in all 4 cases, you should only need to use the abstract stack interface functions push(), pop(), top(), and isEmpty() in order to successfully use our Stack type for this assignment and the function you are asked to write. For this assignment you need to perform the following tasks. 1. In the first task, we will write a function that will check if a string of parenthesis is matching. Strings will be given to the function of the format “(()((())))”, using only opening “(” and closing “)”. Your function should be named doParenthesisMatch(). It takes a single 1 string as input, and it returns a boolean result of true if the parenthesis match, and false otherwise. The algorithm to check for matching parenthesis using a stack is fairly simple. A pseudo-code description migth be for each charcter c in expression do if c is an open paren ‘(‘ push it on stack if c is a close paren ‘)’: do if stack is empty answer is false, because we can’t match the current ‘)’ else pop stack, because we just matched an open ‘(‘ with a close ‘)’ done done Your function will be given a C++ string class as input. It is relatively simple to parse each character of a C++ string. Here is an example for loop to do this: s = “(())”; for (size_t index = 0; index < s.length(); index++) { char c = s[index]; // handle char c of the string expression s here } 2. In the next task, we will also write a function that decodes a string expression. Given a sequence consisting of 'I' and 'D' characters, where 'I' denotes increasing and 'D' denotes decreasing, decode the given sequence to construct a "minimum number" without repeated digits. An example of some inputs and outputs will make it clear what is meant by a "minimal number". 2 sequence output IIII -> 12345 DDDD -> 54321 ID -> 132 IDIDII -> 1325467 IIDDIDID -> 125437698 If you are given 4 characters in the input sequence, the result will be a number with 5 characters, and further only the digits ‘12345’ would be in the “minimal number” output. Each ‘I’ and ‘D’ in the input denotes that the next digit in the output should go up (increase) or go down (decrease) respectively. As you can see for the input sequence “IDI” you have to accommodate the sequence, thus the output goes from 1 to 3 for the initial increase, becase in order to then decrease, and also only have the digits ‘123’, we need 3 for the second digit so the third can decrease to 2. Take a moment to think how you might write an algorithm to solve this problem? It may be dicult to think of any solution involving a simple iterative loop (though a recursive function is not too dicult). However, the algorithm is relatively simple if we use a stack. Here is the pseudo-code: for each character c in input sequence do push character index+1 onto stack (given 0 based index in C) if we have processed all characters or c == ‘I’ (an increase) do pop each index from stack and append it to the end of result done done Your function should be named decodeIDSequence(). It will take a string of input sequence, like “IDI” as input, and it will return a string type, the resulting minimal number. Notice we will be constructing a string to return here, so simply start with an empty string ~string result = “”` and append the digits to the end when you pop them from the stack as described. 3 3. In the third task, you will write two functions that will be able to sort a stack. First of all, you should write a simpler method that, given an already sorted stack as input, and an item of the same type as the stack type, the item should be inserted into the correct position on the sorted stack to keep it sorted. For example, the stacks will be sorted in ascend…
Looking for solution of this Assignment?
WHY CHOOSE US?
We deliver quality original papers |
Our experts write quality original papers using academic databases.We dont use AI in our work. We refund your money if AI is detected |
Free revisions |
We offer our clients multiple free revisions just to ensure you get what you want. |
Discounted prices |
All our prices are discounted which makes it affordable to you. Use code FIRST15 to get your discount |
100% originality |
We deliver papers that are written from scratch to deliver 100% originality. Our papers are free from plagiarism and NO similarity.We have ZERO TOLERANCE TO USE OF AI |
On-time delivery |
We will deliver your paper on time even on short notice or short deadline, overnight essay or even an urgent essay |