Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures

Similar presentations


Presentation on theme: "Algorithms and Data Structures"— Presentation transcript:

1 Algorithms and Data Structures
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Stack Course No.: Fall 2014

2 Stack Stack is a linear data structure which can be accessed only at one of its ends for storing and retrieving data. A stack is a container that implements the last-in-first-out (LIFO) protocol. This means that the only accessible object in the container is the last one among them that was inserted. There are two main operations applicable to a stack : 1. Push for insert. 2. Pop for deletion. Both operations are done using the pointer (top) and the maximum item used defined by (size ).

3 Stack Operations Push: an item is put on top of the stack, increasing the stack size by one. As stack size is usually limited, this may provoke a stack overflow if the maximum size is exceeded Pop : the top item is taken from the stack, decreasing stack size by one. In the case where there was no top item (i.e. the stack was empty), a stack underflow occurs

4 Representation of Stack
Two ways to representation of stacks in a memory 1- One dimensional array 2- Single Linked List We will implement these operations on a stack: InitializeStack() :Initializes the stack to an empty state. IsEmptyStack() : Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. IsFullStack() : Determines whether the stack is full. If the stack is full, it returns the value true; otherwise, it returns the value false. Push() :Adds a new element to the top of the stack. The input to this operation consists of the stack and the new element. Prior to this operation, the stack must exist and must not be full. Top() : Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. Pop() : Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty.

5 Stack in one dimensional array
# include<iostream.h> class stack { int top; unsigned size; char *stk; public: stack(unsigned s) { size = s ; stk = new char[size] ;} ~ stack( ) { delete [ ] stk ; } void init( ) { top = 0 ; } void destroy( ) { top = 0 ; } void push(char ch){ if( isFull( )) { cerr<<" The stack is Full ! " ; return ;} stk [top++] = ch ; } char pop( ){ if ( isEmpty ( ) ) { cerr<<" \n The stack is empty ! "; return -1;} return stk[ --top ]; int isFull( ) {return top == size ; } int isEmpty( ) {return top == 0 ; } };

6 Cont. int main ( ) { Stack s; s.init ( ) ; s.push(‘A') ; s.push(‘B') ; s.push(‘C') ; s.push(‘D') ; // last in cout<<s.pop( )<<" "; // first out cout<<s.pop( )<<" "; s.pop( ); // Try to popping from an Empty stack return 0;} The result: D C B A The stack is empty! Write the implementation of stack using array in class and let the user insert the size of stack

7 Linked Implementation of Stacks
The operation of adding an item to the head of a single linked list is quite similar to that of pushing an item on to a stack; similarly, the operation of deleting the first node from a single linked list is analogous to popping a stack A stack may be represented by a single (linear) linked list. The first node (head) of the list is the top of the stack

8 Linked Implementation of Stacks
# include<iostream.h> struct node { char data; node *link; }; class stack{ node *s; public: void init(){ s = NULL ; } void push( char x){ node *p = new node; p -> data = x ; p ->link = s ; s = p ;} char pop(){ char x ; node * p1 ; if ( !s ) { cout<<" The stack is Empty ! ";} p1 = s ; s=s -> link ; x = p1 -> data ; delete p1 ; return x ;}

9 Main function main ( ) { stack s ; char item, size =5; s.init(); for(int i = 0;i<size;i++) cin>>item; s.push(item); } for(int j=0;j<size;j++) cout<<s.pop()<<" ";

10 Application of Stacks A classical application deals with evaluation of arithmetic expression; the compiler uses a stack to translate input arithmetic expression into their corresponding object code. Stack as return Address

11 Evaluation of Arithmetic Expressions
An arithmetic expression consists of operands and operators. Operands are variables or constants and operators are of various types like arithmetic unary and binary operators, as in the next table.

12 Evaluation of Arithmetic Expressions..
A simple arithmetic expression is cited below: A + B * C / D – E ^ F * G The problem is to evaluate this expression, one of the most popular way is parenthesize the expression fully, therefore the expression will be as follows: ((A + B) * ((C / D) – (E ^ (F * G)))) With these parenthesizing, the innermost parenthesis part (called subexpression) will be evaluated first, and then the next innermost and so on; such a sequence is shown as follows:

13 Notation for arithmetic expressions
Infix notation: the operators come in between the operands <operand> <operator> <operand> . Following are simple expressions in infix notation: A+B, C-D Prefix notation, uses the convention: <operator> <operand> <operand> Here, the operators come before the operands. Following are simple expressions in prefix notation: + A B, C D, * E F, / G H Suffix (or postfix) notation: where the operator is suffixed by operands: <operand> <operand> <operator> Following expressions are in postfix notation: A B +, C D -, E F *, G H /

14 Convert expressions An expression given in infix notation can be easily converted into its equivalent prefix or postfix notation. Following rule is applied to convert an infix expression into a postfix form: Consider the parentheses version for the infix expression. Move all operators so that they replace their corresponding right part of parentheses. Remove all parentheses Ex .

15 Convert expressions… Similar technique can be applied to obtain prefix notation for a given infix notation by moving operators correspond to left parentheses. Three notations for the given arithmetic expression are listed below: Infix: ( ( A + ( ( B ^ C ) – D ) ) * ( E – ( A / C ) ) ) Prefix: * + A - ^ B C D – E / A C Postfix: A B C ^ D - + E A C / - *

16 Convert expressions… Out of these three notations, postfix notation has certain advantages over other notations. The main advantage is its evaluation. During the evaluation of an expression in postfix notation it only requires to scan the expression from left to right several times, but exactly once. This can be done by using stack. Therefore the evaluation of an expression is a two step process: convert the expression into postfix notation evaluate the converted expression both steps need the stack data structure.

17 Conversion of an infix to postfix
We have two priority values: a symbol will be pushed onto the stack if its incoming priority value is greater than the in-stack priority value of the top-most element a symbol will be popped from the stack if its in-stack priority value is greater than or equal to the incoming priority value of the incoming element Here’s an example,Given the infix expression, with its corresponding numbers:

18 Read Symbol Stack Output 1 ( 2 (( 3 ((( 4 A 5 (((+ 6 AB 7 AB+ 8 ((^ 9
AB+C 10 AB+C^ 11 (- 12 (-( 13 (-(( 14 15 (-((* AB+C^D 16 AB+C^DE 17 AB+C^DE* 18 (-(/ 19 AB+C^DE*F 20 AB+C^DE*F/ 21 AB+C^DE*F/-

19 Evaluation of postfix expression
To evaluate a postfix expression using a stack, we can study the following postfix expression: A B C ^ D - + E A C / - * using the following information: A = 4, B = 3, C = 2, D = 5, E = 3.

20 Symbol Opernd1 Opernd2 Value Operand Stack 4 3 4 3 2 4 3 2 ^ 9 4 9 5 4 9 5 - 4 4 + 8 8 3 8 3 4 / 8 3 2 8 1 *

21 Stack as return Address
The stack is using as a place to store the return address of the a program that call a subprograms.

22 Stack as return Address…


Download ppt "Algorithms and Data Structures"

Similar presentations


Ads by Google