For more notes and topics VISIT: www.eITnotes.com IMPLEMENTATION OF STACKS eITnotes.com.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Stacks Chapter 11.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Data Structures and Algorithms (60-254)
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.
Topic 15 Implementing and Using Stacks
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS Data Structures Chapter 3 Stacks and Queues.
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.
Topic 3 The Stack ADT.
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,
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
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.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Stack Any Other Data Structure Array Linked List
Data Structures & Algorithms
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
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.
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(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Data Structure By Amee Trivedi.
Introduction Of Stack.
Revised based on textbook author’s notes.
Stacks.
STACKS.
Stacks Chapter 4.
Data Structures – Week #3
Algorithms and Data Structures
Stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Topic 15 Implementing and Using Stacks
Stack.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Presentation transcript:

For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com

INTRODUCTION ABOUT STACKS  The stack is a very common data structure used in programs which has lot of potential.  Stacks hold objects, usually all of the same type.  It follows the concept of LIFO – last in first out.  Stack is a linear list of items in which all additions and deletion are restricted to one end.  Some languages, like LISP and Python, do not call for stack implementations, since push and pop functions are available for any list.  All Forth-like languages (such as Adobe PhotoScript) are also designed around language-defined stacks that are directly visible to and manipulated by the programmer. eITnotes.com

STACKS VS ARRAYS  An array is a contiguous block of memory.  A stack is a first-in-last-out data structure with access only to the top of the data.  Since many languages does not provide facility for stack, it is backed by either arrays or linked list.  The values can be added and deleted on any side from an array.  But in stack, insertion and deletion is possible on only one side of the stack. The other side is sealed. Eg: a[10] –array a[10] - stack eITnotes.com

The bottom of a stack is a sealed end. Stack may have a capacity which is a limitation on the number of elements in a stack. The operations on stack are Push: Places an object on the top of the stack. Pop: Removes an object from the top of the stack. IsEmpty: Reports whether the stack is empty or not. IsFull: Reports whether the stack exceeds limit or not. (i)stack (ii)push(s,a) (iii)push(s,d)-stack overflow (iv)pop(s) (v)pop(s)-stack underflow aa b c STACK OPERATIONS eITnotes.com

CABDE A D E B STACK OPERATIONS eITnotes.com

STACK IMPLEMENTATION  Stack data structure is not inherently provided by many programming languages.  Stack is implemented using arrays or linked lists.  Let S be a stack, n be the capacity, x be the element to be pushed, then push and pop will be given as Push(S,x) and Pop(S)  Here we use “top” which keeps track of the top element in the stack.  When top = = 0, and pop() operation gives stack underflow as result.  When top = = n, and push() operation gives stack overflow as result.  The pop() operation just gives an illusion of deletion, but the elements are retained. Only the top is decremented. eITnotes.com

S[1:6] Top=1 Push(S,a)a Top=6 Push(S,a) Push(S,b) Push(S,c) push(S,d) Push(S,e) push(S,f) a b c d e f Top=6 = n Stack overflow Push(S,g)a b c d e f Top=5 Pop(S)a b c d e Top=1 Pop(S) Pop(S) pop(S) a Top=0 Pop(S) Top=0 stack underflow Pop(S) eITnotes.com

STACK APPLICATIONS  Recursion handling  Evaluation of expression Conversion of infix to postfix expression Computation of postfix expression  Parenthesis handling  Backtracking Conversion of decimal to other number system Maze tracer Undo operations eITnotes.com

RECURSION HANDLING  Without stack, recursion is difficult  Compiler automatically uses stack data structure while handling recursion.  All computer needs to remember for each active function call, values of arguments & local variables and the location of the next statement to be executed when control goes back.  Essentially what is happening when we call that method is that our current execution point is pushed onto the call stack and the runtime starts executing the code for the internal method call. When that method finally returns, we pop our place from the stack and continue executing. eITnotes.com

Eg: int f(int n) { int k,r; if(n==0) return 0; k=n*n; r=f(n-1); Return k+r; } nkrnkr nkrnkr Ans: eITnotes.com

PARENTHESIS CHECKING Procedure check() Declare a character stack S. Now traverse the expression. a) If the current character is a starting bracket then push it to stack. b) If the current character is a closing bracket then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced. After complete traversal, if there is some starting bracket left in stack then “not balanced” End procedure eITnotes.com

Eg: [a+(b*c)+{(d-e)}] [ [ ( [ [ { [ { ( [ { [ Push [ Push ( ) and ( matches, Pop ( Push { Push ( matches, pop ( Matches, pop { Matches, pop [ Thus, parenthesis match here eITnotes.com

CONVERSION OF INFIX TO POSTFIX  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.  We do not know what to do if an operator is read as an input character. By implementing the priority rule for operators, we have a solution to this problem.  The Priority rule: we should perform comparative priority check if an operator is read, and then push it. If the stack top contains an operator of priority higher than or equal to the priority of the input operator, then we pop it and print it. We keep on performing the priority check until the top of stack either contains an operator of lower priority or if it does not contain an operator. eITnotes.com

Eg: (A*B)+C ElementStackPrefixAction $(A*B)+C$$(A*B)+C$ A AB AB* AB*C AB*C+ Work stack Push ( Print A Push(*) Print B Pop *,( print * Push + Print C Pop + $ $ ( $ ( * $ $ + eITnotes.com

EVALUATION OF POSTFIX EXPRESSION  Postfix expression is easily evaluated by the compiler by using stack.  The procedure for the evaluation of postfix expression is given below: procedure eval(E) x=getnextchar(E); case x: x is an operand: push x into stack S. x is an operator: pop elements, perform operation and push result into stack. x is null: pop stack and print result end case End procedure eITnotes.com

Eg: AB*C+ A=2, B=3, C=5 ElementStackAction AB*C+$AB*C+$ Push A Push B Pop A and B, A*B, push 6 Push C Pop C and 6, C+6, push 11 Pop Result:11 A AB 6 6 C 11 eITnotes.com

BACKTRACKING  Backtracking is a simple, elegant, recursive technique which can be put to a variety of uses.  You start at the root of a tree, the tree probably has some good and bad leaves. You want to get to a good leaf. At each node, you choose one of its children to move to, and you keep this up in a stack until you get to a leaf.  Suppose you get to a bad leaf. You can backtrack to continue the search for a good leaf by revoking your most recent choice, and trying out the next option in that set of options.  If you run out of options, revoke the choice that got you here, and try another choice at that node.  If you end up at the root with no options left, there are no good leaves to be found. eITnotes.com

Starting at Root, your options are A and B. You choose A. At A, your options are C and D. You choose C. C is bad. Go back to A. At A, you have already tried C, and it failed. Try D. D is bad. Go back to A. At A, you have no options left to try. Go back to Root. At Root, you have already tried A. Try B. At B, your options are E and F. Try E. E is good. Eg: eITnotes.com

CONVERSION OF DECIMAL TO OTHER NUMBER SYSTEM  The given decimal number is divided by the base (2 or 8 or 16) repeatedly and the corresponding remainders are pushed into stack.  At last the elements are popped out of the stack to give the result. Eg. 84 – in binary 124 in octal 54 in hexadecimal eITnotes.com

84 / 2 = / 2 = / 2 = / 2 = 50 5 / 2 = 21 2 / 2 = push pop / 8 = / 8 = push pop 84 / 16 = pushpop eITnotes.com

MAZE TRACER All our mazes will be two-dimensional arrays of n rows and n columns. Each row, column cell is either open, or blocked by an internal wall. From any open cell, you may move left, right, up, or down to an adjacent empty cell. To solve a maze, you must find a path of open cells from a given start cell to a specified end cell. By default, you should assume that the start cell is in position (0,0). Sample: S O O O O O H H H H O H H O O O O H H O H H H H H O H O O O H O O O H E The path will be: [(0,0),(0,1),(0,2),(0,3),(0,4), (1,4),(2,4),(2,3), (2,2),(2,1),(3,1), (4,1),(5,1),(5,2),(5,3),(4,3),(4,4), (4,5),(5,5)] eITnotes.com

S O O O O O H H H H O H H O O O O H H O H H H H H O H O O O H O O O H E 0 0 Push start (0,0) 00 Push Pop Push Push eITnotes.com

Push Push Push Push End start eITnotes.com

UNDO OPERATION  An undo operation is a method for reverting a change to an object, along with the arguments needed to revert the change.  Undo operations are typically collected in undo groups, which represent whole revertible actions, and are stored on a stack.  To undo a single operation, it must still be packaged in a group.  Undo groups are stored on a stack, with the oldest groups at the bottom and the newest at the top.  The undo stack is unlimited by default, but you can restrict it.  When the stack exceeds the maximum, the oldest undo groups are dropped from the bottom. eITnotes.com

 Initially, both stacks are empty. Recording undo operations adds to the undo stack, but the redo stack remains empty until undo is performed.  Performing undo causes the reverting operations in the latest group to be applied to their objects.  Consecutive undos add to the redo stack. Subsequent redo operations pull the operations off the redo stack, apply them to the objects, and push them back onto the undo stack.  The redo stack’s contents last as long as undo and redo are performed successively. However, because applying a new change to an object invalidates the previous changes, as soon as a new undo operation is registered, any existing redo stack is cleared. UNDO OPERATION eITnotes.com

Thank you…! eITnotes.com