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

Help with Assignment, Need help with Data Structures assignment requiring C...

Need help with Data Structures assignment requiring C++ program

How can a lock object be called in the transaction, How can a lock object b...

How can a lock object be called in the transaction? By calling Enqueue and Dequeue in the transaction.

Write stream analogues of list processing functions, (a) Write (delay ) as...

(a) Write (delay ) as a special form for (lambda () ) and (force ), as discussed in class. (b) Write (stream-cons x y) as a special form, as discussed in class. (c) Write

Compare two functions, Comp are two functions n 2    and  2 n  / 4...

Comp are two functions n 2    and  2 n  / 4  for distinct values of n.   Determine When s ec on d function b ec om es l a r g er th an f i r st functi

A tree having ''m'' nodes has (m-1) branches. prove., Q. Prove the hypothes...

Q. Prove the hypothesis that "A tree having 'm' nodes has exactly (m-1) branches".      Ans: A tree having m number of nodes has exactly (m-1) branches Proof: A root

What is an unreachable code assertion, What is an unreachable code assertio...

What is an unreachable code assertion An unreachable code assertion can be placed at the default case; if it's every executed, then program is in an erroneous state. A loop in

Hash function, Q. Define the graph, adjacency matrix, adjacency list, hash ...

Q. Define the graph, adjacency matrix, adjacency list, hash function, adjacency matrix, sparse matrix, reachability matrix.

Bubble sort, In this sorting algorithm, multiple swapping occurs in one pas...

In this sorting algorithm, multiple swapping occurs in one pass. Smaller elements move or 'bubble' up to the top of the list, so the name given to the algorithm. In this method,

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