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.

Slides:



Advertisements
Similar presentations
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Advertisements

Stacks Chapter 11.
Prefix, Postfix, Infix Notation
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
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.
Stacks.
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.
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.
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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
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.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
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.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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.
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 STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
CSC 231 Stacks 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
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 Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
Click to edit Master text styles Stacks Data 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(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
Stacks Chapter 5.
Stacks Chapter 6.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Objectives In this lesson, you will learn to: Define stacks
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
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.
Infix to Postfix Conversion
Stack.
Data Structures and Algorithms 2/2561
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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 is designed to enforce this restriction This feature is useful for many day to day and programming operations Stack Top Accessible Item

An Abstract Data Type Stacks are defined primarily by – Interface Only first item should be accessible Last item should be popped out first – Operations performed on the interface Push Pop Peek Underlying mechanism of its implementation is not visible to the user. – Arrays, Structures or any other data types can be used for this purpose

Uses Stack can be used in – Balancing brackets, paranthesis and braces in a computer source program – Parsing an arithmetic expression like 3*(4+5) – Traversing Binary trees (don’t freak out we will discuss it later) – Microprocessors to keep the track of the functions call, especially recursive calls

Real world examples Postal service and office mail – throw all the received letters in the inbox or in-basket – Process each letter starting from the top most item and working their way down Task performed in a day to day life – You are working on project A – While working, a coworker asked for the help on project B – While working on B, your boss called you for his help with travel expenses – After completing the task at hand you have to return to the previous task until you reach project A

Operations on Stack Stack Size – Returns the number of items on the stack Push – Pushes an item on the top of the stack and increments the size and top pointer of the stack Pop – Pulls out the top most item from the stack and decrements the stack size and the top pointer of the stack Peek – Without removing the item from the stack returns the top most item of the stack. Does not effect top of the stack pointer and the stack size

Matching Delimiters, An Example (Pseudo Code) The program works as follows: 1.Reads one item from the given string 2.If character is an opening delimiter, It will be pushed on the stack 3.Else if it’s a closing delimiter then it checks a.If there is no item in the stack to match with then exit with an error message b.Else the top item of the stack will be popped out and compared with the read delimiter, if they match then go to step 1, else exit with the error message. 4.After reading the whole string, if there is any delimiter left in the stack then exit with a an error message

Working Example of Delimeter Matching Consider the string a{b(c[d]e)f} Stack a { b ( c [ d ] e ) f } { ( [

Converting Infix to Postfix An Infix notation: – 2*3-4/5 Equivalent postfix notation would be – 23*45/- In a postfix notation operators come after the operands Stacks can be very effectively used to convert an infix notation to a postfix notation

Conversion Algorithm Scan the Infix string from left to right. Initialisean empty stack. If the scanned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. – If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). – If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character. – Repeat this step till all the characters are scanned. If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty. Return the Postfix string.

A Working Example Infix String: a+b*c-d a + b * c - d abc+*d- +- * PostfixString