I have implemented a stack using a singly linked list on c++. I need to use my s
ID: 3839592 • Letter: I
Question
I have implemented a stack using a singly linked list on c++. I need to use my stack to read in an html document and output every word in the file with the corresponding tags the word is inside. It should look like:
Sample output (for full credit, the tags should appear in order from oldest to newest):
<html> <body> <h3> My
<html> <body> webpage
<html> <body> <p> This
.....and so on with the tags appearing oldest to newest.
I need help on how to approach this problem, I have already read in all the words in the html document including the tags into a stack.
Explanation / Answer
algo:
----------------------------
a) read the charecters of html code
1)if charecter is < then
2)if next charecter is not / then read the word upto > and push it into stack
exmple:<html> --> html push into stack
3)else if next charecter is / then read the word betwenn </ , > let it be tag
4)if(tag == top of stack):
pop()
5)else:
not valid html code
6)repete the step 1 to 5 util reading all charectes
7)if stack is empty:
valid code
8)else:
Invalid code
Example::
<html>
<body>
<p></p>
</body>
</html>
stack:: null
< readed next charecter is not /, html is word between < and > so push html into stack
stack::
| html |
--------
< readed next charecter is not /, body is the word between < and > so push body into stack
stack::
| body |
--------
| html |
--------
< readed next charecter is not /, p is the word between < and > so push p into stack
stack::
| p |
--------
| body |
--------
| html |
--------
< readed next charecter is /, p is the word between </ and >
so tag=p
top of stack is p
so tag==top stack so pop()
stack::
--------
| body |
--------
| html |
--------
< readed next charecter is /, body is the word between </ and >
so tag=body
top of stack is body
so tag==top stack so pop()
stack::
--------
| html |
--------
< readed next charecter is /, body is the word between </ html >
so tag=html
top of stack is body
so tag==html stack so pop()
stack::
| |
--------
now stack is empty so it is a valid code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.