{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Stack - THE STACK Contents Definition and Examples...

This preview shows pages 1–4. Sign up to view the full content.

1 THE STACK Contents: Definition and Examples Representing stacks in C Example: infix, prefix, and postfix Exercises Definition and Examples A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. A stack is a dynamic, constantly changing object as the definition of the stack provides for the insertion and deletion of items. It has single end of the stack as top of the stack, where both insertion and deletion of the elements takes place. The last element inserted into the stack is the first element deleted- last in first out list (LIFO). After several insertions and deletions, it is possible to have the same frame again. Primitive Operations When an item is added to a stack, it is push ed onto the stack. When an item is removed, it is pop ped from the stack. Given a stack s , and an item i , performing the operation push(s,i) adds an item i to the top of stack s. push(s, H); push(s, I); push(s, J); Operation pop(s) removes the top element. That is, if i=pop(s) , then the removed element is assigned to i. pop(s);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Because of the push operation which adds elements to a stack, a stack is sometimes called a pushdown list. Conceptually, there is no upper limit on the number of items that may be kept in a stack. If a stack contains a single item and the stack is popped, the resulting stack contains no items and is called the empty stack. Push operation is applicable to any stack. Pop operation cannot be applied to the empty stack. If so, underflow happens. A Boolean operation empty(s) , returns TRUE if stack is empty. Otherwise FALSE, if stack is not empty Example To check the parentheses in any mathematical expression, are nested correctly, we need to ensure that, There are an equal number of right and left parentheses Every right parentheses is preceded by a matching left parentheses For instance, ((A+B) A+B( )A+B(-C (A+B))-(C+D Each left parentheses opens the scope and right parentheses closes the scope. The nesting depth at a particular point in an expression is the number of scopes that have been opened but not yet closed at that point. The parentheses count at a particular point in an expression is the number of left parentheses minus the number of right parentheses that have been encountered in scanning the expression from its left end up to that particular point. The two conditions that must hold if the parentheses in an expression form an admissible pattern are as follows: The parentheses count at the end of the expression is 0.
3 The parentheses count at each point in the expression is nonnegative.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}