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

Complexity of an algorithm, Q. Explain the complexity of an algorithm?  Wha...

Q. Explain the complexity of an algorithm?  What are the worst case analysis and best case analysis explain with an example.

The # of times an algorithm executes, for(int i = 0; i for (int j = n -...

for(int i = 0; i for (int j = n - 1; j >= i ; j--){ System.out.println(i+ " " + j);

Determine the complexity, 1)    The set of the algorithms whose order is O ...

1)    The set of the algorithms whose order is O (1) would run in the identical time.  True/False 2)    Determine the complexity of the following program into big O notation:

Implementation of a binary tree, Like general tree, binary trees are implem...

Like general tree, binary trees are implemented through linked lists. A typical node in a Binary tree has a structure as follows struct NODE { struct NODE *leftchild; i

Determine the greatest common divisor, Determine the greatest common diviso...

Determine the greatest common divisor (GCD) of two integers, m & n. The algorithm for GCD might be defined as follows: While m is greater than zero: If n is greater than m, s

Explain backtracking, Explain Backtracking The  principal idea is to co...

Explain Backtracking The  principal idea is to construct solutions single component  at a time  and evaluate such  partially constructed candidates as follows. If a partiall

Generate a single sorted list of all n elements, Q. Assume that we have sep...

Q. Assume that we have separated n elements in to m sorted lists. Explain how to generate a single sorted list of all n elements in time O (n log m )?

Explain space complexity, Explain Space Complexity Space Complexity :...

Explain Space Complexity Space Complexity :- The space complexity of an algorithm is the amount of memory it requires to run to completion. Some of the reasons to study space

Procedure of analysis of algorithm, Example 1:  Following are Simple sequen...

Example 1:  Following are Simple sequence of statements Statement 1;  Statement 2; ... ... Statement k; The entire time can be found out through adding the times for

Write the algorithm to find input and output value, This algorithm inputs 5...

This algorithm inputs 5 values and outputs how many input numbers were positive and how many were negative. Data to be used: N = 1, -5, 2, -8, -7

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