Loading
View RSS Feed

Angad Kumar Shukla's blog

All Algorithms Related To Stack

Rating: 5 votes, 4.00 average.
Stack is a collation of an array arr of size MAXSIZE and an integer top. Top is initialized by −1;

1)

Algorithm for checking whether the stack is full or not.



isfull (stack *s)

{
If s−>top is equals to MAXSIZE −1 return true;
Else return false;
}

2)

Algorithm for inserting an element into the stack.



Push (stack *s, item)

{
If (! isfull(s))
{
Increment s−>top by 1;
Assign item into s−>arr [s−>top];
}
}

3)

Algorithm for checking whether the stack is empty or not.



isempty (stack *s)

{
If s−>top is equals to −1 return true;
Else return false;
}

4)

Algorithm for deleting an element from the stack.



Item Pop (stack *s)

{
If (!isempty(s))
{
Assign s−>arr [s−>top] into item;
Decrement s−>top by 1;
Return item;
}
}



5)

Algorithm for viewing the topmost element of the stack.



Item Peek (stack *s)
{
If (! isempty(s))
{
Assign s−>arr [s−>top] into item;
Return item;
}
}


6)

Algorithm for evaluating a postfix expression.



Evaluate (input string)

{
Initialize stack;
While (input string is not empty)
{
Scan each digit from the input string and assign it into symbol.
If (symbol is an operand) push symbol into the stack.
Else
{
Pop two elements from the stack and perform the appropriate operation and push the result into the stack.
}
}
Pop the element from the stack and return it.
}

7)

Algorithm for converting an infix expression to postfix expression.



Convert (input string)

{
Initialize stack;
While (input string is not empty)
{
Scan each digit from the input string and assign it into symbol.
If (symbol is an operand) place it into the output string.
Else
{
If (symbol is equals to ‘(‘) push symbol into the stack.
Else if (symbol is equals to ‘)’) pop the elements from the stack and assign it into the output string until ‘(‘.

Else
{
While (precedence of symbol is les than or equals to the precedence of peek (stack))
Pop the elements and place those elements into the output string.
Push symbol into the stack.
}
}
}
Pop the elements from the stack and place those elements into the output string until stack is not empty.
Print output string.
}

Submit "All Algorithms Related To Stack" to Digg Submit "All Algorithms Related To Stack" to del.icio.us Submit "All Algorithms Related To Stack" to StumbleUpon Submit "All Algorithms Related To Stack" to Google

Updated 07-21-2012 at 06:42 PM by angad

Categories
Data Structure

Comments

  1. Harsh's Avatar
    Very Nice post Angad...Itz really awesome..I couldnt resist myself to approve the post...Please keep posting like this...
  2. sonuaks's Avatar
    yess..... thanks angad...it really much of help.
  3. sonuaks's Avatar
    i am thanful to you for such precious information
  4. angad's Avatar
    thnx sonuaks...i wl add more info 2 dis
  5. Hasan's Avatar
    what is the algorithm of two joining stack?
  6. sachdev's Avatar
    two joining stack? you mean how to join two stack? pretty interseting... let me know about this
  7. abhishek.panja's Avatar
    hi hasan..
    This works with two stacks of cards, one of them is called A, the other is called B. There is a third stack that is empty at the start, called C. At the end, it will contain the result.
    1.If either stack A or stack B are empty, put all the cards of the stack that is not empty on top of stack C; you are done, stack C is the result of the merge. (Note: take the whole stack, and put it on stack C; doing it card by card will change the order and will not work as it should)
    2.Look at the top cards of stack A and stack B; put the one with the lower number on top of stack C. If stack C had no cards in it, it will now have one card.
    3.If either stack A or stack B has cards left, go back to step 1, to sort them.
  8. abhishek.panja's Avatar
    can you please elucidate yourb problem a bit? good question asked by Sachdev.
  9. rishab's Avatar
    hello.. i guess i have the same problem related to hasan. well good added by abhishek but my problem is:
    If you have two stacks st1 and st2 with integer values,
    write C++ program to merge the two stack in a third stack
    named st3.
    can anyone help?



Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.