Question

13. You are using a stack to analyze a String, checking for balanced delimiters. Draw the contents of the stock and indicate

0 0
Add a comment Improve this question Transcribed image text
Answer #1

13.

The algorithm for checking balanced delimiters using a Stack is:

1. Create an empty Stack

2. Scan through the String and for each symbol follow the below rules

2.1 If the character being considered is an opening delimiter (opening paranthesis or brackets) then push this character to the stack

2.2 If the character being considered is a closing delimiter (closing paranthesis or brackets) then pop the top element of the stack to see if it matches the opening delimiter from the stack. If the stack is empty or the top element of stack do not match the closing delimiter, print "Imbalanced detected" and exit . If it matches the delimiters then continue with the next character

2.3 Any other characters are ignored.

3. After scanning the input string , if the Stack is not empty, print "Imbalanced detected" and exit else print "Balanced delimiters".

4. Exit

Given Input string : "(<[()>])"

Let us create a stack s, to be empty

Iteration 1:

Input Character = '('

Since it is an starting delimiter, we push it to the stack

Stack :

top : ( => NULL

Iteration 2:

Input Character = '<'

Since it is an starting delimiter, we push it to the stack

Stack:

top: < => ( => NULL

Iteration 3:

Input character = '['

Since it is an starting delimiter, we push it to the stack

Stack:

top: [ => < => ( => NULL

Iteration 4:

Input character = '('

Since it is an starting delimiter, we push it to the stack

Stack:

top: ( = > [ => < => ( => NULL

Iteration 5:

Input character = ')'

Since it is a closing delimiter, we pop the top element from stack and check if it matches this closing delimiter

Top element = '('

Since Input character and top element matches , we continue

Stack :

top: [ => < => ( => NULL

Iteration 6:

Input character = '>'

Since it is a closing delimiter, we pop the top element from stack and check if it matches this closing delimiter

Top element = '['

Since Input character and Top element of the stack do not match ,we print "Imbalanced is detected" and exit.

Stack:

top: < => ( => NULL

At the time of detection of imbalance, character being considered in input string is '>' and the character being considered from the stack is '['

Stack contents :

top : < => ( => NULL (after removal of the top character)

Add a comment
Know the answer?
Add Answer to:
13. You are using a stack to analyze a String, checking for balanced delimiters. Draw the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • Write a program using java that determines whether an input string is a palindrome; that is,...

    Write a program using java that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. please type out the code :)

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack)...

    **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

  • You have to use a h file for class definitions and two (as specified below) cpp...

    You have to use a h file for class definitions and two (as specified below) cpp file for the C++ implementation. You have to generate the list from scratch and check for overflow I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. Please submit enough screenshots showing compiling and execution under Linux. Write a main program...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Please just fill in the codes where this CharacterOrganizer.java class asks for (Bolded). It's a simple...

    Please just fill in the codes where this CharacterOrganizer.java class asks for (Bolded). It's a simple Java Stacks assignment. I'll rate you thumbs up! Thank you Program Info: Your program needs to read in characters and push them into the stack called startStack. Then your program should remove each element from the startStack from its top one by one, and the objective to insert all characters into the stack called finalStack in the alphabetical order. If the top character of...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT