Vous êtes sur la page 1sur 17

STACK

The various data structures are divided into following categories: Linear data structure- A data structure whose elements form a

sequence, and every element in structure has a unique predecessor and a unique successor. Examples of linear data structure are: Arrays Linked Lists Stacks Queues Non-Linear data structures- A data structure whose elements do not form a sequence. There is no unique predecessor or unique successor. Examples of non linear data structures are trees and graphs.

Definition

A stack is a homogeneous collection of items of any one type, arranged linearly with access at one end only, called the top. This means that data can be added or removed from only the top. Formally this type of stack is called a Last-In-First-Out (LIFO) stack.

Description

Push and Pop


Primary operations: Push and Pop Push Add an element to the top of the stack Pop Remove the element at the top of the stack

Stacks Example

Stack
The original idea of the stack is that there is no upper limit on the number of items that may be kept in a stack. Pushing another item onto a stack produces a larger collection of items. When the stack bean implemented in a program, it can be represented as an array, therefore we need to give the maximum size of this stack to be shrieked. So, we need as operation called Full (S) (which determines whether or not a stack is full (overflow) to be applied before push operation. On the other hand, if a stack contains a single item and the stack is poped, the resulting stack contains no item and is called an empty stack. Therefore, before applying the pop operator to a stack, we must ensure that a stack is not empty. The operation empty (s) determine whether or not a stack is empty.

Implementation of Stacks
Any list implementation could be used to implement a stack Arrays (static: the size of stack is given initially) Linked lists (dynamic: never become full)

Array Implementation

Insertion : the operation is also called push Deletion : the operation is also called pop

Top : A pointer, which keeps track of the top element in the Stack. If an array of size N is declared to be a stack, then TOP will be 1 when the stack is empty and is N when the stack is full

Expression Evaluation
Calculators employing reverse Polish notation use a stack structure to hold values. Expressions can be represented in prefix, postfix or infix notations. Conversion from one form of the expression to another form may be accomplished using a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. Before translating into low level code. Most of the programming languages are context-free languages allowing them to be parsed with stack based machines.

Application of stack
Infix notation: the operator comes between the two operands need to use parentheses to control the evaluation of the operators Postfix notation: the operator comes after its two operands : no need to use parentheses Prefix notation: the operator comes before its two operands : no need to use parentheses

Example:

a+b

Infix notation : a + b Prefix notation : + a b Postfix notation: a b +

Algorithm Transforming Infix Expression into Postfix Expression

Algorithm: POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P
Step 1: Push ( on to the STACK and add ) to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty Step 3: If an operand is encountered, add it to P Step 4: If left parentheses is encountered, add it to STACK Step 5: If an operator is encountered, then: (a) Repeatedly pop from STACK and add to P each operator which has same precedence or higher precedence than (b) Add to STACK Step 6: If a right parentheses is encountered , then: (a) Repeatedly pop from STACK and add to P each operator until a left parentheses is encountered (b) Remove the left parentheses [End of Step 2 Loop] Step 7: Exit

Example
1. 2.

Convert the following infix expression to postfix using stack: A + (B /C) (( A ( B + C)) * D) / (E +F)

Algorithm Transforming Infix Expression into Prefix Expression


Algorithm: [Polish Notation] PREFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent prefix expression P Step 1: Reverse the input string Step 2: Examine the next element in the input Step 3: If it is operand, add it to output string Step 4: If it is closing parentheses, push it on stack Step 5: If it is operator, then: (i) if stack is empty, push operator on stack (ii) if top of stack is closing parentheses, push operator on the stack (iii) If it has same or higher priority than top of stack, push operator on stack Else pop the operator from the stack and add it to output string, repeat step 5 Step 6: If it is an opening parentheses, pop operators from stack and add them to output string until a closing parentheses is encountered, pop and discard the closing parentheses. Step 7: If there is more input go to step 2 Step 8: If there is no more input, unstack the remaining operators and add them to output string Step 9: Reverse the output string

Example
1. 2.

Convert the following infix expression to postfix using stack: A + (B /C) (( A ( B + C)) * D) / (E +F)

Converting Exercise
1)Convert these INFIX to PREFIX and POSTFIX :
A/BC/D (A + B) ^ 3 C * D A ^ (B + C)

2)Convert these PREFIX to INFIX and POSTFIX :


+/ABC^DE +DE/XY ^+23CD

3)Convert these POSTFIX to INFIX and PREFIX :


ABC+ GH+IJ/* AB^ CD+

Vous aimerez peut-être aussi