Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,

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

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
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)
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.
EC-211 DATA STRUCTURES LECTURE 5. 2 Stack Data Structure Stack of Books Stack of Notes Stack of Gifts.
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
Topic 15 Implementing and Using Stacks
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
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 Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Stack Applications.
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.
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,
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.
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.
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)
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Stack Any Other Data Structure Array Linked List
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.
Data Structures & Algorithms
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
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.
Data Structures & Algorithm CS-102
Review Use of Stack Introduction Stack in our life Stack Operations
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.
Stacks Stacks.
Revised based on textbook author’s notes.
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Stacks Chapter 4.
Data Structures – Week #3
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
COMPUTER 2430 Object Oriented Programming and Data Structures I
Mark Allen Weiss, Addison Wesley
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
COMPUTER 2430 Object Oriented Programming and Data Structures I
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.
Data Structures – Week #3
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
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:

Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley

Stacks An ordered collection of items which items are inserted (pushed) into and are deleted (popped) from the stack at one end called the top of the stack. LIFO List. (last in first out)

Stacks ปลายด้านบนของกอง ที่เรียกว่า ท๊อปของสแตก (top of stack) คือกองซ้อนทับของ สิ่งของ ของในกองมีลำดับ การเอาของใส่กอง (push) และ การเอาของออกจากกอง (pop) ทำที่ Last In First Out List

Stack Data Structure กองทับซ้อน ของของ ของในกองมี ลำดับ  Type อะไรที่เก็บของ หลายชิ้นได้ โดยของที่เก็บมีลำดับ class stack { book item[10]; int top; }; template class stack { T item[MAX]; int top; }; stack s1; stack s2;

Basic Operations on stack s C++/C s.push(i) push(s, i) push i onto s i = s.pop() i = pop(s) pop i out off s stack s / init(s) Initialize empty s s.empty() / empty(s) empty s ? i = s.top() i = top(s) look at the top without poping

s.push(i) / push(s, i) Check Overflow CBACBA top = DCBADCBA top; item[top] = i; item[++top] = i; template class stack { T item[MAX]; int top; } D top = 3

i = s.pop() / i = pop(s) Check Underflow CBACBA top = BABA 1010 return item[top--]; template class stack { T item[MAX]; int top; } top = 1 popC

i = s.top() / i = top(s) CBACBA top = return item[top]; template class stack { T item[MAX]; int top; }

s.empty() ? / empty(s) ? CBACBA top = top = -1 return top == -1; template class stack { T item[MAX]; int top; }

stack s; / init(s) ? CBACBA top = top = -1 stack() : top(-1) { } template class stack { T item[MAX]; int top; }

Stack Applications Parenthesis Matching Evaluate Postfix Expression Infix to Postfix Conversion (Reverse Polish Notation) Function Call (in recursion)

Parenthesis Matching Are the parenthesis correct? (a+b-c)*[d+e]/{f*(g+h)} [(a+b-c}*[d+e]/{f*(g+h)} (a+b-c *[d+e]/{f*(g+h)}

Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty() ch = non_paren match(open,ch)

Warnier-Orr Diagram Design Tool Paren matching Init empty stack s Scan (not EOF && not error) read ch ch = open paren + ch = close paren s.push(ch) s.empty() + s.empty() error =true(no match-open-paren) open = s.pop() error = true (missmatch) notmatch(open,ch) error=false error + error no match-open-paren / missmatch MATCH open paren exceed s.empty() + s.empty()

Evaluate Postfix Notation Infix Form Prefix Form Postfix Form (Polish notation) a + b + a b a b + a + b * c + a * b ca b c * +

* * = ? input: 6523 input: + s.push(6) o2=s.pop() =3 s.push(5) o1=s.pop() =25 s.push(2) s.push( 2+3 ) 5 s.push(3)6 SS Evaluate Postfix Notation What ‘s next ?

* * = ? input: 6523input: + s.push(6)33=s.pop() s.push(5)22=s.pop()5 s.push(2)5s.push(2+3)5 s.push(3)66 ss input: 88input: * s.push(8)58= s.pop()40 55= s.pop()5 6s.push(5*8)6 input: -input: 3 40= s.pop()s.push(3)3 5= s.pop()-35 s.push(5-40)66 input: +input: * 3= s.pop()-32= s.pop() -35= s.pop()-326= s.pop() s.push(-35+3)6s.push(6*-32) -192

Infix to Postfix Conversion a*b+c ===> ab*c+a output stack b c+ *

Infix to Postfix Conversion a*b+c ---- > input: stack: output: a*b+ca a*b+c *a a*b+c *ab a*b+c +ab* a*b+c +ab*c a*b+cab*c+ ab*c+

Infix to Postfix Conversion a+b*c ---- > input: stack: output: a+b*ca a+b*c +a a+b*c +ab * * a+b*c +abc a+b*cabc*+ abc*+

Infix to Postfix Conversion a+b*c-d => input:stack: output: a+b*c-da a+b*c-d +a a+b*c-d +ab * * a+b*c-d +abc a+b*c-d -abc*+ a+b*c-d -abc*+d a+b*c-dabc*+d- abc*+d-

Infix to Postfix Conversion a+b*c-(d/e+f)*g => a+b*c-(d/e+f)*g +ab * a+b*c-(d/e+f)*g +abc a+b*c-(d/e+f)*g -abc*+ ( a+b*c-(d/e+f)*g -abc*+d / ( a+b*c-(d/e+f)*g -abc*+de abc*+de/f+g*-

Infix to Postfix Conversion (cont.) a+b*c-(d/e+f)*g => / ( a+b*c-(d/e+f)*g -abc*+de + ( a+b*c-(d/e+f)*g -abc*+de/f a+b*c-(d/e+f)*g -abc*+de/f+ * a+b*c-(d/e+f)*g -abc*+de/f+g a+b*c-(d/e+f)*g -abc*+de/f+g*- abc*+de/f+g*-