2016-06-081Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
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.
The unorganized person’s data structure
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)
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
More About Stacks: Stack Applications Dan Nguyen CS 146, Spring 2004 Professor Sin-Min Lee.
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.
Objectives of these slides:
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.
Stack Applications.
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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.
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
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
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(),
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Data Structures & Algorithm Stack Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss,
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Stacks.
Infix to postfix conversion
Data Structures 5th Week
Stacks Chapter 7 introduces the stack data type.
Stacks.
STACKS.
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
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
Stack and Queues Stack implementation using Array
UNIT-I Topics to be covere d 1.Introduction to data structures.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Stack.
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

Prof. I. J. Chung Data Structure #5 Professor I. J. Chung

Prof. I. J. Chung Data Structure Stack : LIFO(Last-In-First-Out) linear data structure ; entries are deleted from the stack in the opposite order of their insertion. Entries should be inserted (pushed) and deleted at only the end, called the Top position. examples of stack : interrupt handling (AR), procedure (function) calls, PSN, evaluation of arithmetic expression, check of balanced parentheses, reversing the words, … (nodes; entries; items) books dishes Note :insertion of a node into stack : push deletion of a node from stack : pop

Prof. I. J. Chung Data Structure Note stack underflow : the condition trying to delete(access) a node from an empty-stack Stack overflow : the condition trying to insert a node to a full stack e.g. function to check a balanced parentheses boolis_balanced(const string& expression) // post condition : true if the given exp. are balanced parentheses // false otherwise {const char left_parentheses = ‘ ( ‘ ; const char right_parentheses = ‘ ) ’ ; stack store ;// stack to keep the left parentheses as they occur

Prof. I. J. Chung Data Structure string ::size_type i ; // index of the string char next;// the next char from the string bool failed = false;// is true if a needed parentheses is not found for ( i = 0 ; !failed && (i<expression.length()) ; ++i) {next = expression[i]; if (next == left_parentheses) store.push(next); else if ( ( next == right_parentheses) && (!store.empty())) store.pop(); else if ( ( next == right_parentheses) && (store.empty())) failed = true; } // end of for-loop return (store.empty() && !failed); } // end of function

Prof. I. J. Chung Data Structure stack : a linear list for which all insertions/deletions are made at one end of the list Note :1) LIFO structure 2) Imagine a pile of dishes or books 3) insertion (adding) / deletion (removing) are always done from the head (top) of the structure top bottom

Prof. I. J. Chung Data Structure A B A DBADBA A : empty P ← T P ← B P ← D P ← Ω BABA P ← d visit NODE(D)P ← RLINK(D) = Ω

Prof. I. J. Chung Data Structure e.g. EDCBAEDCBA top bottom stack o/f : attempting to push a node (item) into a full stack stack u/f : attempting to pop a node (item) from an empty stack empty -stack A Push (A) BABA Push (B) CBACBA Push (C) DCBADCBA Push (D) CBACBA Pop () BABA EBAEBA Push (E) stack implementation : ① array ② linked list EBA top = head_ptr

Prof. I. J. Chung Data Structure Applications of stack 1)interrupt handling 2)procedure (function : subroutine) processing 3)PSN (Polish String Notation) conversion 4)evaluation of arithmetic exp. 5)checking the matching of parentheses e.g. ( ( x + y ) + 2 ) – ( ( x + y ) / z )

Prof. I. J. Chung Data Structure PSN (Polish String Notation): arithmetic notation in which the operator is placed before(after) its 2 operands e.g.+AB-CD*EF/6H AB+CD-EF*6H- (merits of PSN) : 1)The order in which the operations are to be performed is completely decided by the position of the operators and operands in polish notation 2)There is no need for parentheses 3)There is no need to scan the entire exp. Computer : 1)infix → postfix 2)evaluate the postfix exp. with stack

Prof. I. J. Chung Data Structure PSN of arithmetic exp. precedence of op. max :↑ *, / min :+, - infix notation : the operator is placed between operands (disadvantage of infix notation) 1) we need parentheses 2) we need to scan the entire expression 3) we need to consider the precedence of operators (*, / have higher precedence over + and -)

Prof. I. J. Chung Data Structure evaluation of arithmetic exp. e.g. ABCD + E * + F + * ↑ ↑ ↑ ↑ ↑ ↑ ↑ ② ③ ④ ⑤ ⑥ ⑦ ⑧ DCBADCBA ① T 1 (C+D) B A ② + ET1BAET1BA ③ T 2 (E 1 *T 1 ) B A ④ T 2 (E 2+ B) A ⑤ FT3AFT3A ⑥ T 4 (F+T 3 ) A ⑦ T 5 (T 4 *A) ⑧

Prof. I. J. Chung Data Structure 40 (#(# * / )↑)↑ ICPISPoperator symbol ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ e.g. A / (B + C ) * D # S ABC ABC+ /#/# A 1- f 2 ISP(#) ≥ ICP(/) /#/# 2 f 4 ISP(/) ≥ ICP(C) /#/# -1 t 2 ISP(#) ≥ ICP(*) 2 t 2 ISP(/) ≥ ICP(*) (/#(/# *#*# A B A B C + / D 0 f 1 ISP(() ≥ ICP(+) A B C + / D * +(/#+(/#

Prof. I. J. Chung Evaluation of postfix notation (Fig pp.380) algo. ① add ‘#’ at the end of postfix notation ② scan the input symbol α from the input string. ③ while (α ≠ ‘ # ’ ) do { if (α = operand), put α onto stack S if (α = operator) { pop up 2 items from stack S; evaluate these two popped items with the operator α; // top item would be the 2 nd operand // next top item would be the 1 st operand put the evaluation result onto stack S; } // end of if-loop } // end of while loop

Prof. I. J. Chung Converting a fully parenthesized infix expression into a postfix expression stack S ← { ‘ # ’ } read a symbol α α ≠ ‘ Ω ’ check α print α pop S and print this elt until ‘ ( ’ pop ‘ ( ’ from S pop S and print this elt pop S and print the popped elt until ‘ # ’ END A push α onto S A A ISP(top( S )) ≥ ICP(α) A operand while-loop f operator or ‘ ( ’ f t conversion of IN to PN ‘)’‘)’

Prof. I. J. Chung Data Structure + / C↑ DEA B * f = A * B + C / D ↑ E postorder : AB * C DE ↑ / +