ADT Stack & Queue - Case Studies TCP1201: 2013/2014.

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.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
COSC 2006 Chapter 7 Stacks III
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CMPT 225 Stacks.
Chapter 3 Stacks.
Topic 15 Implementing and Using Stacks
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Infix, Postfix, Prefix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Topic 15 Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
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,
Stack and Queue.
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.
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 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
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.
Programming Abstractions
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack.
Introduction to Data Structures
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack and Queues Stack implementation using Array
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
5.1 The Stack Abstract Data Type
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:

ADT Stack & Queue - Case Studies TCP1201: 2013/2014

ADT Stack: Checking for Balanced Braces  A stack can be used to verify whether a program contains balanced braces o An example of balanced braces - abc{defg{ijk}{l{mn}}op}qr o An example of unbalanced braces - abc{def}}{ghij{kl}m Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0.

ADT Stack: Checking for Balanced Braces  Requirements for balanced braces  Each time you encounter a “}”, it matches an already encountered “{”  When you reach the end of the string, you have matched each “{”  Every time we see a start ‘{', push onto the stack.  Every time we see an end ‘}', pop off the stack.  So the parentheses are not balanced if: 1. We try to pop, but the stack is empty. 2. When we are done, the stack is not empty Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0.

ADT Stack: Checking for Balanced Braces Figure 6-3 Traces of the algorithm that checks for balanced braces Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0.

ADT Stack: Checking for Balanced Braces bool isBalanced (string expression) { Stack s; char temp; for (int i=0; i<expression.length(); i++) { if ( expression[i] == '{' ) s.push (expression[i]); if ( expression[i] == '}' ) if ( s.isEmpty() ) return false; else s.pop(temp); } if ( !s.isEmpty() ) return false; return true; }

ADT Stack: Checking for Balanced Braces int main() { string expression; cout << "Enter a string: "; cin >> expression; if (isBalanced(expression)) cout << "Expression is BALANCED!" << endl; else cout << "Expression is NOT BALANCED!" << endl; } OUTPUT:

 In Postfix Notation (RPN), mathematical expressions can be written without parentheses.  * = ((1+2)+4)*3  Some programming languages such as Postscript use Postfix Notation.  C++ is built to interpret the standard Infix Notation.  We can get C++ to interpret Postfix Notation using a stack. ADT Stack: Evaluating Postfix Expressions

8  A postfix calculator  When an operand is entered, the calculator  Pushes it onto a stack  When an operator is entered, the calculator  Applies it to the top two operands of the stack  Pops the operands from the stack  Pushes the result of the operation onto the stack Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0. ADT Stack: Evaluating Postfix Expressions

9 Figure 6-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0. ADT Stack: Evaluating Postfix Expressions

10 ADT Stack: Evaluating Postfix Expressions  To evaluate a postfix expression entered as a string of characters  Use the same steps as a postfix calculator  Simplifying assumptions  The string is a syntactically correct postfix expression  No unary operators are present  No exponentiation operators are present  Operands are single lowercase letters that represent integer values Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0. Take Home Exercise Implement the postfix calculator using the ADT Stack.

ADT Queue: Character Categorization Write a program that reads a set of characters as input (each character is separated by a space), and categories the characters into 4 different groups, namely number, small letter, capital letter, and others. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0. Input characters (Press ENTER to stop): w 1 ^ B A i 2 l _ 3 4 l N i ^ | a | m | Numbers: Small Letters: w i l l i a m Capital Letters: B A N Others: ^ _ ^ | | |

ADT Queue: Character Categorization Steps of the program: 1. Enqueue each character into an input queue. 2.Categorize the input queue into four queues according to the type of a character: number, small letter, capital letter, and others. 3.Display the characters in each queue by performing dequeue on each queue until it is empty Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver 5.0.

int main() { Queue input; char temp; char character = '\0'; // STEP 1: Enqueue each character into an input queue cout << "Input characters (Press ENTER to stop):" << endl; while (cin.peek() != '\n') { cin >> character; input.enqueue(character); } cout << endl; ADT Queue: Character Categorization

// STEP 2: Categorise the input queue into four queues // according to the type of a character const int NUM_OF_CATEGORY = 4; Queue category[NUM_OF_CATEGORY]; string caption[] = {"Numbers", "Small Letters", "Capital Letters", "Others"}; while (!input.isEmpty()) { input.peek( temp ); if ( temp >= '0' && temp <= '9') { category[0].enqueue( temp ); } else if ( temp >= 'a' && temp <= 'z'){ category[1].enqueue(temp); } else if (temp >= 'A' && temp <= 'Z') { category[2].enqueue(temp); } else { category[3].enqueue(temp); } input.dequeue(temp); } ADT Queue: Character Categorization

// Display the content of the queue for each category for (int i = 0; i < NUM_OF_CATEGORY; ++i) { cout << caption[i] << ": "; while (!category[i].isEmpty()) { category[i].dequeue(temp); cout << temp << " "; } cout << endl; } ADT Queue: Character Categorization