Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Develop a Stack abstract data type The Stack class should include the following methods: push(item) function to add a new item to the stack
Develop a Stack abstract data type
The Stack class should include the following methods:
- push(item) function to add a new item to the stack
- pop() function to remove the top item from the stack
- peek() to return the top item in the stack
- is_empty() to test whether the stack is empty
- size() to tell the size (number of items) in the stack
Develop a html_checker function
The function:
- Takes a string of HTML code as argument;
- Returns true if all tags are balanced; false if otherwise.
Here is what the function should do:
- Develop a new Stack object;
- Use the split() method of a string to split it into text lines;
- Develop a loop to read the text lines, and for each text line:
- If the text line matches an opening tag (e.g.
), add the tag name (e.g. "h1") to the stack;
- If the text line is ordinary content without a HTML tag, skip the line;
- If the text line is a closing tag (e.g. ), check whether the stack is empty():
- The HTML code is unbalanced if stack is currently empty;
- If not empty, use pop() to read and remove the top item from stack:
- The top item/tag matches the current closing tag, then the HTML code is balanced (true);
- Otherwise, the HTML code is unbalanced (false).
- If the text line matches an opening tag (e.g.
Program Execution
Develop two variables, one with balanced HTML code and the other with unbalanced HTML code. Run the html_checker function with each of the two variables as argument to test the program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started