Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Advertisements

Stacks Chapter 11.
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)
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
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 Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
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,
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 7 Ming Li Department of.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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.
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.
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.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Containers Overview and Class Vector
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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)
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
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.
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 Chapter 5.
Stacks Stacks.
MEMORY REPRESENTATION OF STACKS
Homework 4 questions???.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stacks Chapter 7 introduces the stack data type.
Cinda Heeren / Geoffrey Tien
Stacks Chapter 4.
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.4 Additional Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
COMPUTER 2430 Object Oriented Programming and Data Structures I
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
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:

Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass Stack (3 slides) Class Stack Using a Stack to Create a Hex # Using a Stack to Create a Hex # Uncoupling Stack Elt’sUncoupling Stack Elt’s (6 slides) Uncoupling Stack Elt’s Activation Records Activation Records RPN Stacks Infix Notation Infix Notation Summary SlidesSummary Slides (4 slides) Summary Slides

Main Index Contents 2 Stacks A stack is a sequence of items that are accessible at only one end of the sequence.

Main Index Contents 3 Pushing/Popping a Stack Because a pop removes the item last added to the stack, we say that a stack has LIFO (last- in/first-out) ordering.

Main Index Contents 4 4 Main Index Contents CLASS stack Constructor stack(); Create an empty stack CLASS stackOperations bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. The stack API

Main Index Contents 55 Main Index Contents CLASS stack Operations void pop(); Remove the item from the top of the stack. Precondition:The stack is not empty. Postcondition:Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top.

Main Index Contents 66 Main Index Contents CLASS stack Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition:The stack is not empty. const T& top() const; Constant version of top().

Main Index Contents 7 Stack operations #include … stack s; int i; for (i = 1; i <= 5; i++) s.push(i);// top  5, 4, 3, 2, 1 cout << "stack size = " << s.size( ) << endl;// size = 5 cout << "Popping the stack: << endl; while (!s.empty( )) { cout << s.top.( ) << '\t';// s.pop( ); } cout << endl; …

Main Index Contents 8 Stack operations (continued) top( ) returns a reference to the item at the top of the stack; allows the programmer to modify its value. stack s; s.push(22); s.push(33); s.push(99); cout << s.top( ) << endl; s.top( ) = 55;// 99 replaced by 55 while(!s.empty( )) { cout << s.top( ) << '\t'; s.pop( ); } …

Main Index Contents 9 Application: number system conversion The function multibaseOutput takes an arbitrary positive integer and an integer representing the target base of a new number system (2 for binary, 8 for octal, and 16 for hexadecimal). The function then converts the decimal number to the target base and displays the number in new number base as a string. string multibaseOutput(int num, int b) { string digitChar = " ABCDEF", numStr = ""; stack stk; // extract base-b digits of decimal num do { stk.push(digitChar[num % b); num /= b; } while (num != 0);

Main Index Contents 10 Application: number system conversion (continued) // flush the stack while (!str.empty( )) { numStr += stk.top( );// concatenate digit character on top // of stk stack to numStr string stk.pop( ); } return numStr; } // The following slide shows in a stepwise manner how a decimal integer 431 is converted and stored in numStr. Obviously, the same function works for octal and binary conversion as well. Exercise: design a driver main function to test the above function.

Main Index Contents 11 Main Index Contents Using a Stack to convert a decimal integer to a Hex Number

Main Index Contents 12 Main Index Contents Uncoupling Stack Elements: operation needs two stacks

Main Index Contents 13 Main Index Contents Uncoupling Stack Elements

Main Index Contents 14 Main Index Contents Uncoupling Stack Elements

Main Index Contents 15 Main Index Contents Uncoupling Stack Elements

Main Index Contents 16 Main Index Contents Uncoupling Stack Elements

Main Index Contents 17 Main Index Contents Uncoupling Stack Elements

Main Index Contents 18 Uncoupling example The function uncouple removes the first occurrence of a target (if any) from stack s. The function returns true if target is removed and false otherwise. template bool uncouple (stack &s, const T &target) { stack tmpStk; bool foundTaget = true;// assume the target is on the stack while (!s.empty( ) && s.top( ) != target) { tmp.push(s.top( )); s.pop( ); };

Main Index Contents 19 Uncoupling example (continued) if (!s.empty( ))// found target s.pop( ); else// target not on the stack s foundTarget = false; while (!tmpStk.empty( )) { s.push(tmpStk.top( )); tmpStk.pop( ); } return foundTarget; }

Main Index Contents 20 Main Index Contents

Main Index Contents 21 Why the pop( ) returns void? If the pop( ) returns an element by value, it is inefficient, because it involves a call to the copy constructor for type T. Returning a reference to the element on top of the stack is not feasible, because the element is no longer on the stack and must be save somewhere before returning a reference to it. If the choice is to use dynamic memory, a memory leak will result unless the dynamic memory is eventually deleted.

Main Index Contents 22 Other applications: recursive code and runtime stack Stack is extensively used for the operation of recursive algorithms. There are numerous interesting and practical applications in this regard. We will be discuss a few examples after the recursive algorithms are discussed.

Main Index Contents 23 Main Index Contents

Main Index Contents 24 Main Index Contents Infix Expression Rules The figure below gives input precedence, stack precedence, and rank used for the operators +, -, *, /, %, and ^, along with the parentheses. Except for the exponential operator ^, the other binary operators are left-associative and have equal input and stack precedence. Precedence SymbolInput precedence Stack precedence Rank * / %2 2 ^4 3 (5 0 )0 0 0

Main Index Contents 25 Main Index Contents Summary Slide 1 §- Stack -Storage Structure with insert (push) and erase (pop) operations occur at one end, called the top of the stack. -The last element in is the first element out of the stack, so a stack is a LIFO structure.

Main Index Contents 26 Main Index Contents Summary Slide 2 §- Recursion -The system maintains a stack of activation records that specify: 1)the function arguments 2)the local variables/objects 3)the return address -The system pushes an activation record when calling a function and pops it when returning.

Main Index Contents 27 Main Index Contents Summary Slide 3 §- Postfix/RPN Expression Notation -places the operator after its operands -easy to evaluate using a single stack to hold operands. -The rules: 1) Immediately push an operand onto the stack. 2) For a binary operator, pop the stack twice, perform the operation, and push the result onto the stack. 3) At the end a single value remains on the stack. This is the value of the expression.

Main Index Contents 28 Main Index Contents Summary Slide 4 §- Infix notation -A binary operator appears between its operands. -More complex than postfix, because it requires the use of operator precedence and parentheses. -In addition, some operators are left-associative, and a few are right-associative.