How do I convert the following pseudo code into an array instead of a stack. The algorithm is intended to construct a digraph given a certain input:
- Scan the formula from right to left calling symbols on a stack until 2 consecutive node symbols appear on top.
- Pop off these 2 node symbols and the ‘*’ just below them on the stack. Draw an edge from the first symbol to the second.
- Push the first symbol on the stack.
- Continue steps 1-3 until the formula is processed.
Here is a sample input and output:
Input:
***ABCD
Output:
*AB, *AC, *AD
‘*’ represents an edge
All inputs will be done using a scanner.
You have basically to perform operations on the array using the FIFO propriety.
Every time you add an element to your array (push) you do
topOfStack++;and insert the element on the"topOfStack"position of the array. By doingtopOfStack++you are keeping track of the array position that became the new top of the stack.When you do
popyou must check if the array is not empty, return the element onarray[topOfStack]and dotopOfStack--;. Because the top is now the previous position on the array.If you need more space for your array because the stack is full
(topOfStack == InitialSize)you have to create another array with more space, for example :For stating, this is roughly what you need to look for, naturally, there are many more details to look into. Nevertheless, try it out yourself, and when you have doubts look for resources such as this.