Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Stacks, Queues, and Linked Lists
§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.
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.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
COSC 2006 Chapter 7 Stacks III
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
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:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
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.
CMPT 225 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Objectives of these slides:
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 7 Stacks II CS Data Structures I COSC 2006
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.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
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,
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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 & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
Data Structures: A Pseudocode Approach with C1 Chapter 3 Objectives Upon completion you will be able to Explain the design, use, and operation of a stack.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
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.
Stacks Chapter 3 Objectives Upon completion you will be able to
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Stacks.
Stacks Stack: restricted variant of list
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 6 Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stack.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Sample PMT online… Browse /sumII05/PMT/2004_1/ /sumII05/PMT/2004_1/

Stacks and Queues

Checking for Balanced Braces Balanced: abc{dc{dc}pa}{csd+} Unbalanced:

Pseudocode while (not end of line) { if (ch is ‘{‘) add ch to Stack else if (ch is ‘}’){ if (Stack is empty) return unbalanced; remove from Stack } if (Stack is empty) return balanced; return unbalanced;

Definition of Stack ADT level –A stack is an ordered group of homogeneous items, in which the removal and addition of stack items can take place only at the top of the stack A stack is a LIFO: last in, first out

Stack ADT 6 aStack.createStack(); while (not end of line) { if (ch is ‘{‘) aStack.push(ch); else if (ch is ‘}’){ if (!aStack.isEmpty()){ aStack.pop();} else{ aStack.destroyStack(); return unbalanced;} } //endelseif } //endwhile if (aStack.isEmpty()) return balanced; aStack.destroyStack(); return unbalanced; Using ADT stack for brace matching

A Stack -- Graphic

Array-Based Implementation class Stack { Public: Stack(); ~Stack(); bool isEmpty(); void push(Item item); void pop(); Item getTop(); Private: Item items[MAX]; int top; };

Array-Based Implementation Stack::Stack:top(-1){} Stack::~Stack(){} bool Stack::isEmpty(){ return top==-1; } void Stack::push(Item item){ if (top>=MAX-1) exit(0); items[++top] = item; } void Stack::pop(){ if (isEmpty()) exit(0); --top; } Itemp Stack::getTop(){ if (isEmpty()) exit(0); return items[top]; }

ADT List-Based Implementation class Stack { public: … private: List aList; };

ADT List-Based Implementation Stack::Stack{} Stack::~Stack(){} bool Stack::isEmpty(){ return aList.isEmpty(); } void Stack::push(Item item){ aList.insert(item, 1); }

ADT List-Based Implementation void Stack::pop(){ aList.remove(1); } Item Stack::getTop(){ return aList.retrieve(1); }

Compare Three Implementations Array v.s. LinkedList –Fixed size v.s. dynamic size LinkedList v.s. ADT List –Efficiency v.s. Simplicity

Application: Algebraic Expressions Infix expressions –Every binary operator appears between its operands: a+b*c, (a+b)*c Prefix expressions –Operator appears before its operands: +a*bc, *+abc Postfix expressions –Operator appears after its operands: abc*+, ab+c*

Evaluating Postfix Expressions

Pseudocode Stack aStack; for (each token t in the input){ if (t is an operand) aStack.push(t); else { operand2 = aStack.getTop(); aStack.pop(); operand1 = aStack.getTop(); aStack.pop(); result = operand1 t operand2; aStack.push(result); }