BCA II Data Structure Using C

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
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.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
30-Jun-15 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.
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 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching materials Generation Group.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
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.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
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(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
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.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures and Algorithms
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Applications of Stacks and Queues
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

BCA II Data Structure Using C Submitted By : Veenu Saini Department of Information Technology

Applications of Stacks Matching Balancing Parenthesis Evaluating an postfix expression Infix to postfix expression Applications of Stacks

Matching Balancing Parenthesis Given an expression of ‘(‘ and ‘)’, a ‘(‘ must match with a ‘)’, or else it is illegal. ( )( ), ( ( ( ) ) ), ( ( ( ) ) ( ) ) are all legal, while ( ( ) (, ) ( ) ( are all illegal. Obviously, counting the numbers of ‘(‘ and ‘)’ in the expression is not enough. Applications of Stacks

Matching Balancing Parenthesis Insights  By convention, the expression is reading from left-to-right. A ‘)’ is matched with the closest, unmatched ‘(‘ on its left. For example, ( ( ( ) ) ( ) ) Applications of Stacks

Matching Balancing Parenthesis Suppose we have a ‘)’. How do we know which ‘(’ is closest and unmatched? If we read the expression from left-to-right, the MOST RECENTLY UNMATCHED ‘(’ is cancelled with ‘)’. How can we keep track of the MOST RECENTLY READ (LAST) ‘(’ ? (keep in mind there are many pending unmatched ‘(’). Which data structure is keeping track of the most recent item ? Stack  LIFO structure Applications of Stacks

Matching Balancing Parenthesis When we see a ‘(‘, we push it into a stack. When we see a ‘)’, we pop ‘(‘ from the stack. This ‘(‘ matches with current ‘)’. What happen if the stack is empty (meaning there is no such ‘(’) at that moment ? What happen if we have finished reading the expression, but the stack is not empty ? It means there are more ‘(‘ than ‘)’ in the expression. It means the input is illegal. It means the input is illegal. Applications of Stacks

Infix to Postfix Expressions Infix expression Suppose the expression only has operators ‘*’ and ‘+’; and ‘*’ has a higher precedence than ‘+’. So, 5+2+3 = 10, and 1+2*4=9, etc. The expression may also have parenthesis to complicate the problem, i.e., (1+2)*4=12 (1+2*5+1)*3=36. (1+2*(5+1))*3=39. Applications of Stacks

Infix to Postfix Expressions 1 3 + 1 2 4 * + 1 2 + 4 * 6 5 2 3 + 8 * + 3 + * are all postfix expressions. No ‘(‘, ‘)’ is in the expression. To evaluate a postfix expression, we need a stack. Applications of Stacks

Infix to Postfix Expressions Evaluate a postfix expression. Read the expression from left-to-right. When a number is seen, it is pushed onto the stack. When an operator is seen, the operator is applied to the two numbers that are popped from the stack. The result is pushed on the stack. Applications of Stacks

Infix to Postfix Expressions Example : 6 5 2 3 + 8 * + 3 + * Applications of Stacks

Infix to Postfix Expressions How to convert an infix expression to a postfix expression? Use a stack. Read the infix expression from left-to-right. When an operand (number) is read, output it. If an operator (other than ‘(‘, ‘)’) is read, pop the stack (and output the operator) until a lower precedence operator or ‘(‘ is on the top of stack. Then push the current operator on the stack. If ‘(‘ is read, push it on the stack. If a ‘)‘ is read, pop the stacks (and output the operators), until we meet ‘(’. Pop that ‘(’ (do no output it). Finally, if we read the end of the expression, pop the stack (and output the operators) until the stack is empty. Applications of Stacks

Infix to Postfix Expressions Example : (1+2*(5+1))*3 postfix expression  1 2 5 1 + * + 3 * Applications of Stacks

Application of Queue Real life examples Waiting in line Waiting on hold for tech support Applications related to Computer Science Threads Job scheduling (e.g. Round-Robin algorithm for CPU allocation) Applications of queue