Question: 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).

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!