Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Evaluation of Expressions
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stacks  Introduction  Applications  Implementations  Complex Applications.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures & Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structures & Algorithm CS-102
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Review Use of Stack Introduction Stack in our life Stack Operations
Chapter 4 Stacks
Data Structures Using C++ 2E
Stacks Stacks.
Stacks and Queues Chapter 4.
Revised based on textbook author’s notes.
CE 221 Data Structures and Algorithms
CS 201 Data Structures and Algorithms
MEMORY REPRESENTATION OF STACKS
Stacks.
Algorithms and Data Structures
STACKS.
Stacks Chapter 4.
Data Structures – Week #3
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Mark Allen Weiss, Addison Wesley
More About Stacks: Stack Applications
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks.
CE 221 Data Structures and Algorithms
(Part 2) Infix, Prefix & Postfix
Stack.
Data Structures – Week #3
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

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

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 ).

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

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.

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 ; } };

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

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

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 ;}

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()<<" ";

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

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.

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:

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 /

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 .

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 / - *

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.

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:

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/-

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.

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 4 2 / 8 3 2 8 1 *

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

Stack as return Address…