Stacks, Data Structure & Algorithms

Assignment Help:

Q. Explain what are the stacks? How can we use the stacks  to check whether an expression is correctly parentheses or not. For example (()) is well formed but (() or )()( is not well formed.

 

Ans:

The stack is a data structure that organizes data in a similar way one organizes a pile of coins. The new coin is all the time placed on the top and the oldest is on the bottom of the stack. When we are accessing coins, the last coin on the pile is the first coin which was removed from the stack. If we want to reach the third coin, we should remove the first two coins from the top of the stack first so that the third coin comes on the top of the stack and we can easily remove it. There is no way at all to remove a coin from anywhere other than the top of the stack.

A stack is useful whenever we need to store data and retrieve data in last in, first out order. Let us take an example the computer processes instructions using a stack in which the next instruction to execute is at the top of the stack.

To determine whether an expression is well parentheses or not:- the two conditions should be fulfilled while pushing an expression into a stack. At first, whenever an opening bracket is pushed inside a stack, there should be an occurrence a closing bracket before we reach the last symbol. Whenever a closing bracket is encountered, the top of the stack is popped until the opening bracket is popped out and discarded. If no such type of opening bracket is found and stack is made empty, then this means that the expression is not well parentheses designed.

An algorithm to check that whether an expression is correctly parenthized or not is written below:

flag=TRUE;

clear the stack;

Read a symbol from input string;

while not end of input string and flag do

{

if(symbol= '( ' or symbol= '[' or symbol = '{' )

push(symbol,stack);

else  if(symbol= ') ' or symbol= '[' or symbol =

'{' )

if stack is empty flag=false;

printf("More right parenthesis than left

parenthises");

else c=pop(stack);

match c and the input symbol; If not matched

{     flag=false;

printf("Mismatched

parenthesis");

}

Read the next input symbol;

}

if stack is empty then

printf("parentheses are balanced properly");

else

printf(" More number of left parentheses than right parentheses");

 


Related Discussions:- Stacks

Er diagram, Ask queConsider the following functional dependencies: Applican...

Ask queConsider the following functional dependencies: Applicant_ID -> Applicant_Name Applicant_ID -> Applicant_Address Position_ID -> Positoin_Title Position_ID -> Date_Position_O

Explain in detail about the abstract data type, Abstract data type The ...

Abstract data type The thing which makes an abstract data type abstract is that its carrier set and its operations are mathematical entities, like geometric objects or numbers;

Determine the warnock algorithm, Warnock's Algorithm A divide and conqu...

Warnock's Algorithm A divide and conquer algorithm Warnock (PolyList PL, ViewPort VP) If (PL simple in VP) then Draw PL in VP, else Split VP vertically and horiz

Interest, I =PR/12 Numbers of years .Interest rate up to 1yrs ...

I =PR/12 Numbers of years .Interest rate up to 1yrs . 5.50 up to 5yrs . 6.50 More than 5 yrs . 6.75 design an algorithm based on the above information

What is string, What is String Carrier set of the String ADT is the s...

What is String Carrier set of the String ADT is the set of all finite sequences of characters from some alphabet, including empty sequence (the empty string). Operations on s

Functions for inserting and deleting at either end of deque, Q. Devise a re...

Q. Devise a representation for a given list where insertions and deletions can be made at both the ends. Such a structure is called Deque (which means Double ended queue). Write fu

Convertion, how we can convert a graph into tree

how we can convert a graph into tree

Explain divide and conquer algorithms, Explain divide and conquer algorithm...

Explain divide and conquer algorithms  Divide  and  conquer  is  probably  the  best  known  general  algorithm  design  method.  It   work according to the following general p

Stack and array, how to implement multiple stack using single dimension arr...

how to implement multiple stack using single dimension array in c

Question, A binary search tree is used to locate the number 43. Which of th...

A binary search tree is used to locate the number 43. Which of the following probe sequences are possible and which are not? Explain. (a) 61 52 14 17 40 43 (b) 2 3 50 40 60 43 (c)

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd