Académique Documents
Professionnel Documents
Culture Documents
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
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
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)
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)