ARTICLE

Stack with an Algorithm

Posted by Manish Tewatia Articles | Algorithms and VB.NET January 27, 2011
A stack is an ordered list in which all insertions and deletions are made at one end, called the top.
 
Reader Level:

Stack : A stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a linear data structure which works based on the strategy last-in-first out (LIFO). A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds an item to the top of the stack, hiding any items already on the stack, or initializing the stack if it is empty It is a linear data structure which is open for operations at only one end (both insertions and deletions are defined at only one end). One natural example of stack, which arises in computer programming, is the processing of procedure calls and their terminations. Stacks are generally used to remember things in reverse. It finds a major use in backtracking approaches. Some of the functions related to a stack are

Create ( ) :- Q;

Insertion(Q, e) :- updated Q;

Deletion (Q) :- Q, e;

Isfull(Q) :- Boolean;

Isempty(Q) :- Boolean;

Top(Q) :- e;

Destroy(Q)

It has to be noted that with respect to a stack, insertion and deletion operations are, in general, called PUSH and POP operations respectively. Following are the algorithms for some functions of stack.

Algorithm: Create

Output: S, Stack created

Method
:

    Declare S[SIZE] //Array of size=SIZE

    Declare and Initialize T=0 //Top pointer to remember the number of elements

Algorithm ends


Algorithm: Isempty

Input: S, stack

Output: Boolean

Method:

    If (T==0)
        Return (yes)
    Else
        Return (no)
    If end

Algorithm ends

Algorithm: Isfull

Input: S, stack

Output: Boolean

Method:

    If (T==SIZE)
        Return (yes)
    Else
        Return (no)
    If end

Algorithm ends

Algorithm: Push

Input: (1) S, stack; (2) e, element to be inserted; (3) SIZE, size of the stack;
         (4) T, the top pointer

Output: (1) S, updated; (2) T, updated

Method:

    If (Isfull(S)) then
        Print ('stack overflow')
    Else
        T=T+1;
        S[T] =e
    If end

Algorithm ends

Algorithm: Pop

Input: (1) S, stack;

Output: (1) S, updated; (2) T, updated (3) 'e' element popped

Method:

    If (Isempty(S)) then
        Print ('stack is empty')
    Else
        e = S[T]
        T=T-1;
    If end

Algorithm ends

Summary

This article helps you to learn about stack with the help of an algorithm.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor