Data Structures and Algorithms
|
|
- Catherine Fields
- 5 years ago
- Views:
Transcription
1 Data Structures and Algorithms Alice E. Fischer Lecture 6: Stacks 2018 Alice E. Fischer Data Structures L5, Stacks... 1/29 Lecture 6: Stacks / 29
2 Outline 1 Stacks C++ Template Class Functions 2 Stack Applications 3 Stack Application: Parsing 4 Infix, Postfix, and Prefix Expressions Alice E. Fischer Data Structures L5, Stacks... 2/29 Lecture 6: Stacks / 29
3 Stacks Stacks A stack is a linear LIFO data structure. All linear data structures have a beginning and an end, but in a stack, access is restricted to one end. All data is added and removed at the beginning of a stack. No action happens at the end. Alice E. Fischer Data Structures L5, Stacks... 3/29 Lecture 6: Stacks / 29
4 Stacks Applications of Stacks A stack is LIFO. LIFO situations are rare in the real world, but they do happen. But stacks are essential in the world of computing. All modern languages use a stack to manage allocation and deallocation of function parameters and local variable. Each function call puts a frame on top of the run-time stack and each function return removes a frame. The stack frames provide an ever-adapting context for the code. A stack is an essential tool for implementing recursion. A stack is also used during program translation. The nested control statements and curly braces are matched up and validated by the compiler using a stack. The familiar infix notation for expressions is translated, using a stack, to postfix notation that a computer can evaluate. Alice E. Fischer Data Structures L5, Stacks... 4/29 Lecture 6: Stacks / 29
5 Stacks Implementing a Stack Two kinds of stacks are illustrated here. First is a stack implemented by an ordinary array. It is simple and fast, but has limited capacity. Stack s; s.push('a'); s.push('z'); s.push('m'); s.push('p'); max top data a z m p?????? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] max top 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] data a z m p?????? Second is a stack implemented by a growing array. It is almost as simple and almost as fast. Alice E. Fischer Data Structures L5, Stacks... 5/29 Lecture 6: Stacks / 29
6 Stacks A Linked-List Stack This stack is implemented by a linked list. It can grow, as needed, but takes more memory and more run time than an array-based stack. Stack s; s.push('a'); s.push('z'); s.push('m'); s.push('p'); top count 4 p m z a nullptr Alice E. Fischer Data Structures L5, Stacks... 6/29 Lecture 6: Stacks / 29
7 Stacks Stack and Queue Implementations These structures can be implemented in an array, a growing array, or a linked list. Each implementation has its own and advantages / disadvantages. Stack Queue Array simple, fast, fast, limited size. limited size. Growing fast, fast, Array not complex, complex to program unlimited size unlimited size Linked easy not difficult List slower runtime slower runtime unlimited size unlimited size The nature of the application you are implementing may determine which implementation you should use. Alice E. Fischer Data Structures L5, Stacks... 7/29 Lecture 6: Stacks / 29
8 Stacks Traditional and Current Stack Function Names There is a long tradition behind the names of the functions used with stacks and queues. However, those names were invented long before OO, and are no longer useful. C++ provides template class implementations that honor some of these names, change some, add alternative names and provide additional functions. Traditional C++ Template Action for Stack S top() back() Access item at the top of S. push() push() Add item to the top of S pop() pop() Remove item from top of S empty() empty() Is S empty? full() Is S full? size() How many items are in S? emplace() Construct an item and add to top of S. Alice E. Fischer Data Structures L5, Stacks... 8/29 Lecture 6: Stacks / 29
9 Stacks C++ Template Class Functions stack<bt> is an adapter class It is built on top of another container that can be a vector<bt>, a list<bt> or a deque<bt>. A deque (double-ended queue) is the default. stack<int> st1; stack<int, vector<int>> st2; while (!st2.empty())... unsigned n = st2.size(); BT temp = st2.top( ); st2.push( item ); st2.emplace( BT( ) ); st2.pop(); swap( st1, st2); // Empty stack using deque // Using vector // Result is type bool // # of elements in container // Returns reference to stack top // Insert item above top of stack // Construct and push an item // Remove item at top of stack. Return is void // Swaps two entire stacks Alice E. Fischer Data Structures L5, Stacks... 9/29 Lecture 6: Stacks / 29
10 Stack Applications Stack Applications Reversing the order of any sequence Matching nested pairs of symbols In every compiler: Parsing source code Run time memory management Implementing recursion Alice E. Fischer Data Structures L5, Stacks... 10/29 Lecture 6: Stacks / 29
11 Stack Applications Reversing a Sequence Example: Base conversion. The algorithm to generate digits in the new base is easy, but it generates the least significant digit first. Solution: put the digits on a stack and print it when the algorithm finishes the conversion. Program: BaseConversion Alice E. Fischer Data Structures L5, Stacks... 11/29 Lecture 6: Stacks / 29
12 Stack Applications Code: Reversing a Sequence int n; int base; Stack<int> st; // input: the number to convert // input: base to which we will convert n // Store sequence of converted digits cin >> n >> base; // Generate digits of converted number, right to left. for (int power=0; n!= 0; ++power) { st.push( n % base ); // Isolate right-hand digit of n. n /= base; // then eliminate right-hand digit. } // Print digits in opposite order of generation. st.print(cout); Alice E. Fischer Data Structures L5, Stacks... 12/29 Lecture 6: Stacks / 29
13 Stack Applications Matching Nested Pairs Example: Matching brackets Many text-file formats, including program code and html, require tags to come in well-nested pairs. Solution: As the file is read, search for left- or open-symbols. When found, classify each one as a left-token and push onto a stack. Process the text following the open-symbols. When the right- or close-symbol arrives, compare it to the left-symbol at the top of the stack. If they match, a unit of text has been identified. If they don t match, there is a syntax error. Alice E. Fischer Data Structures L5, Stacks... 13/29 Lecture 6: Stacks / 29
14 Stack Applications Code: Classifying Tokens enum BracketType{BKT_SQ,BKT_RND,BKT_CURLY,BKT_ANGLE,BKT_NONE}; enum TokenSense{SENSE_LEFT, SENSE_RIGHT}; void Token:: classify( char ch ){ static const char* brackets = "[](){}<>"; char* p = strchr( brackets, ch ); if (p==null) { } type = BKT_NONE; sense = SENSE_LEFT; } else { } // arbitrary value int pos = p-brackets; // pointer - gives subscript. sense = (pos % 2 == 0)? SENSE_LEFT : SENSE_RIGHT; type = (BracketType)(pos/2); // int /, with truncation. Alice E. Fischer Data Structures L5, Stacks... 14/29 Lecture 6: Stacks / 29
15 Stack Applications Code: Matching brackets for (;;) { // Read and process the file. in.get(ch); // Don t skip leading whitespace. if (in.eof()) break; Token curtok( ch ); if (curtok.gettype()==bkt_none) continue; // skip non-tags switch (curtok.getsense()) { case SENSE_LEFT: stk.push( curtok ); break; case SENSE_RIGHT: if (stk.empty()) mismatch("too many right tags", curtok, false); toptok = stk.peek(); if (toptok.gettype()!= curtok.gettype()) mismatch("right tag has wrong type", curtok, false); stk.pop(); // Open- and close- tags match. break; }} Alice E. Fischer Data Structures L5, Stacks... 15/29 Lecture 6: Stacks / 29
16 Stack Applications Run Time Memory Management The run-time stack is used to manage allocation and deallocation of memory at run time. A frame is created to store a function s private information. The frame stores function arguments, the return address, and local variables for the function. A stack-pointer is also stored there. During execution, the code uses the variables in the frame at the top of the stack. Upon function return, the frame is deleted. The destructors for all objects declared in that frame will be executed. This automatic allocation and deallocation of memory is fundamental to implementing recursive functions. Alice E. Fischer Data Structures L5, Stacks... 16/29 Lecture 6: Stacks / 29
17 Stack Applications Recursion A frame is created at run time, when a function is called, to store its private information. A recursive function calls itself, possibly again and again. Each time, a new frame is laid on the stack. So there can be many frames on the run-time stack for the same function. When the function code refers to a variable, the instance in the newest frame is used. We say that the stack-top frame is the current context. When each call returns, the next-older frame becomes the current context. To understand recursion, you must understand how these frames are laid on the stack, and also how they are removed. Alice E. Fischer Data Structures L5, Stacks... 17/29 Lecture 6: Stacks / 29
18 Stack Applications Parsing Source Code Every modern compiler uses a stack to analyze the syntax of source code. Begin- and end- symbols must correspond. Expressions must be analyzed according to precedence and associativity. The scope of parameters and local variables must be defined properly. A variable name refers to the object defined in the local scope, if any. If not, it refers to a symbol defined in some enclosing block. A frame is the unit of data stored on the stack. A frame is created when a function is called and deleted when the function returns. Errors are identified when one of the stacks is empty when it should have something on it, or when there are operands on the operand stack when the last operator has been evaluated. Alice E. Fischer Data Structures L5, Stacks... 18/29 Lecture 6: Stacks / 29
19 Stack Application: Parsing Stack Application:Precedence Parsing The process of translating expressions in a program is based on the algorithm called precedence parse. Here, we describe a very simple precedence-parse algorithm for a language with only four operators and parentheses. Initially, the operands are numbers in a desktop calculator and either numbers or variable names in a compiler. As the process progresses, tree-nodes are created by combining two operands and an operator. Then a new node is pushed onto the operand stack in place of the two that were removed to build it. The operators are predefined, and every operator has a precedence. The inputs are read one at a time. Each one is classified as operand or operator. Whitespace is used to separate operators and operands from each other, but is otherwise ignored. Every expression must end in a newline. Alice E. Fischer Data Structures L5, Stacks... 19/29 Lecture 6: Stacks / 29
20 Stack Application: Parsing A Simple Expression Language These rules are similar to, but not quite like the rules in C. C has many operators with precedences from 1 to 17. We have only parentheses (, ) and four operators, all binary: +,,, /, Precedence values for these operators, as given in the C precedence table: + = 12, = 12, = 13, / = 13 Consider an expression with two operators such as a + b - c The operators +, - are surrounded by three operands, a, b, c If the precedence of the two operators is the same, the leftmost operator will grab the operand in the middle. Otherwise, the operator with higher precedence will grab it. Thus a + b - c becomes (a + b) - c but a + b * c becomes a + (b * c) Alice E. Fischer Data Structures L5, Stacks... 20/29 Lecture 6: Stacks / 29
21 Stack Application: Parsing The Precedence Parsing Algorithm Initialize the operator stack with a dummy element that has precedence 0. Read the next input symbol. If it is an operand, push it onto the operand stack. If a left-paren, (, push it onto the operator stack with precedence 0. If a right-paren, ), discard it and process all operators back to the matching (. Then pop the ( from the stack. Otherwise it is a binary operator. Compare its precedence to the precedence of the operator at the top of the stack. If the precedence of the new operator is greater than the stack-top operator, push the new operator onto the stack. Otherwise, do a reduce() operation. See the next slide. Quit if the stack-top operator is the dummy with precedence 0. Otherwise, repeat from the read operation. Alice E. Fischer Data Structures L5, Stacks... 21/29 Lecture 6: Stacks / 29
22 Stack Application: Parsing Reducing the Stack To reduce(): Pop an operator off the operator-stack. Pop two operands off the operand-stack. Make a new operand-node using the popped items. Be sure not to reverse the left and right sides. Push the new node onto the operand stack, Alice E. Fischer Data Structures L5, Stacks... 22/29 Lecture 6: Stacks / 29
23 Stack Application: Parsing Parsing a + b - c tree-stack: op-stack: a dum:0 b + :12 - :12 tree-stack: a b + c op-stack: dum:0 - :12 newline tree-stack: a b c op-stack: + - dum:0 Alice E. Fischer Data Structures L5, Stacks... 23/29 Lecture 6: Stacks / 29
24 Stack Application: Parsing Parsing a + b * c tree-stack: a b op-stack: dum:0 + :12 tree-stack: a b c newline op-stack: dum:0 + :12 * :13 tree-stack: a + b * c op-stack: dum:0 Alice E. Fischer Data Structures L5, Stacks... 24/29 Lecture 6: Stacks / 29
25 Stack Application: Parsing Parsing y * ( x - 2 ) tree-stack: op-stack: y dum:0 x 2 *:13 ( -:12 ) tree-stack: y x 2 - op-stack: dum:0 *:13 newline tree-stack: y x 2 op-stack: * - dum:0 Alice E. Fischer Data Structures L5, Stacks... 25/29 Lecture 6: Stacks / 29
26 Infix, Postfix, and Prefix Expressions Notations for expressions. In mathematics and in most programming languages, infix notation is used to write expressions. However, there are languages that use other notations. C, Java, Python, C# all use infix, with parentheses to modify the defaults. Scheme, Lisp, and Haskell all use prefix notation. The operator is written first, then its operands. Function-call parentheses are used in Lisp and Scheme but not in Haskell. Forth and Postscript both use postfix notation. Parentheses are unnecessary for grouping and not used. All three can be parsed into expression trees that mean the same thing. However, they do look different. Alice E. Fischer Data Structures L5, Stacks... 26/29 Lecture 6: Stacks / 29
27 Infix, Postfix, and Prefix Expressions Infix Notation We discussed the precedence parse algorithm in the prior section. It starts with a text expression and outputs an expression tree. 21 y 5 x * 21 = 21 y = ( x - 2 ) * 3 ; Infix: each operand is between its operators. Parentheses are often required in this notation. Alice E. Fischer Data Structures L5, Stacks... 27/29 Lecture 6: Stacks / 29
28 Infix, Postfix, and Prefix Expressions Prefix Notation Prefix notation relies only on position, not on precedence, associativity, and parentheses. A tree-stack and the op-stack are used, but reduce is called any time an operation is read and two operands exist on the tree-stack. 21 = 21 5 y * - x Prefix: The operator is written before its two operands. Alice E. Fischer Data Structures L5, Stacks... 28/29 Lecture 6: Stacks / 29
29 Infix, Postfix, and Prefix Expressions Postfix Notation Postfix notation relies only on position, not on precedence, associativity, and parentheses. Only one stack, the tree-stack, is needed. Reduce is called any time an operator is read as input y x 2-7 * = Postfix: The operator is written after its two operands. Alice E. Fischer Data Structures L5, Stacks... 29/29 Lecture 6: Stacks / 29
An Introduction to Trees
An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search
More informationAn Introduction to Classes
Chapter 4: An Introduction to Classes The most fundamental principles of OO design: A class protects its members. A class takes care of itself... and takes care of its own emergencies What you SHOULD do
More information7.1 Optional Parameters
Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default
More informationAn Introduction to Classes
Chapter 4: An Introduction to Classes The most fundamental principles of OO design: A class protects its members. A class takes care of itself... and takes care of its own emergencies What you SHOULD do
More informationContainers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science
Containers: Stack Jordi Cortadella and Jordi Petit Department of Computer Science The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list.
More informationSTACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:
STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The
More informationAbstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1
Abstract Data Types Stack January 26, 2018 Cinda Heeren / Geoffrey Tien 1 Abstract data types and data structures An Abstract Data Type (ADT) is: A collection of data Describes what data are stored but
More informationContainers: Stack. The Stack ADT. The Stack ADT. The Stack ADT
Containers: Stack The Stack ADT A stack is a list of objects in which insertions and deletions can only be performed at the top of the list. Also known as LIFO Last In, First Out) push insert an element
More information09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION
DATA STRUCTURES AND ALGORITHMS 09 STACK APPLICATION REVERSE POLISH NOTATION IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES
More informationStack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017
Stack Applications Lecture 27 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Wed, Mar 29, 2017 Robb T. Koether Hampden-Sydney College) Stack Applications Wed, Mar 29, 2017 1 / 27 1 Function
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main
More informationReview of the C Programming Language
Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the
More informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationLecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions
Lecture 4: Applications CS2504/CS4092 Algorithms and Linear Data Structures Dr Kieran T. Herley Department of Computer Science University College Cork Summary. Postfix notation for arithmetic expressions.
More informationSTACKS AND QUEUES. Problem Solving with Computers-II
STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationStack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5
What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion
More informationStacks and their Applications
Stacks and their Applications Lecture 23 Sections 18.1-18.2 Robb T. Koether Hampden-Sydney College Fri, Mar 16, 2018 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018
More informationIV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard
IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two
More informationProgramming Languages Third Edition. Chapter 9 Control I Expressions and Statements
Programming Languages Third Edition Chapter 9 Control I Expressions and Statements Objectives Understand expressions Understand conditional statements and guards Understand loops and variation on WHILE
More information-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.
Topic 15 Implementing and Using "stack n. The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted
More informationStack and Its Implementation
Stack and Its Implementation Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Definition of Stack Usage of Stack Outline
More informationProgramming Languages Third Edition. Chapter 7 Basic Semantics
Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol
More informationC Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,
More informationCS 211 Programming Practicum Fall 2018
Due: Wednesday, 11/7/18 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a C++ program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example
More informationStacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)
Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria
More informationMarch 13/2003 Jayakanth Srinivasan,
Statement Effort MergeSort(A, lower_bound, upper_bound) begin T(n) if (lower_bound < upper_bound) Θ(1) mid = (lower_bound + upper_bound)/ 2 Θ(1) MergeSort(A, lower_bound, mid) T(n/2) MergeSort(A, mid+1,
More informationQUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:
QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the
More informationEC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3
UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between
More informationCS W3134: Data Structures in Java
CS W3134: Data Structures in Java Lecture #10: Stacks, queues, linked lists 10/7/04 Janak J Parekh HW#2 questions? Administrivia Finish queues Stack/queue example Agenda 1 Circular queue: miscellany Having
More informationn Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:
Linear, time-ordered structures CS00: Stacks n Prichard Ch 7 n Data structures that reflect a temporal relationship order of removal based on order of insertion n We will consider: first come,first serve
More informationLecture 4 Stack and Queue
Lecture 4 Stack and Queue Bo Tang @ SUSTech, Spring 2018 Our Roadmap Stack Queue Stack vs. Queue 2 Stack A stack is a sequence in which: Items can be added and removed only at one end (the top) You can
More informationStack Abstract Data Type
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 how Java implements a stack To learn how to implement a
More informationExpressions and Assignment
Expressions and Assignment COS 301: Programming Languages Outline Other assignment mechanisms Introduction Expressions: fundamental means of specifying computations Imperative languages: usually RHS of
More informationStacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 5 Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced Delimiters in an Infix Algebraic Expression A Problem Solved:
More informationCS 171: Introduction to Computer Science II. Stacks. Li Xiong
CS 171: Introduction to Computer Science II Stacks Li Xiong Today Stacks operations and implementations Applications using stacks Application 1: Reverse a list of integers Application 2: Delimiter matching
More informationC: How to Program. Week /Mar/05
1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationThe Stack and Queue Types
The Stack and Queue Types Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html 2 Programming Principle of the Day Do the simplest thing that could possibly work A good
More informationHonu. Version November 6, 2010
Honu Version 5.0.2 November 6, 2010 Honu is a family of languages built on top of Racket. Honu syntax resembles Java. Like Racket, however, Honu has no fixed syntax, because Honu supports extensibility
More informationThe Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.
The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The
More informationCS 211. Project 5 Infix Expression Evaluation
CS 211 Project 5 Infix Expression Evaluation Part 1 Create you own Stack Class Need two stacks one stack of integers for the operands one stack of characters for the operators Do we need 2 Stack Classes?
More informationLecture 12 ADTs and Stacks
Lecture 12 ADTs and Stacks Modularity Divide the program into smaller parts Advantages Keeps the complexity managable Isolates errors (parts can be tested independently) Can replace parts easily Eliminates
More informationStack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015
Stack Applications Lecture 25 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Mon, Mar 30, 2015 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 1 / 34 1 The Triangle
More information[CS302-Data Structures] Homework 2: Stacks
[CS302-Data Structures] Homework 2: Stacks Instructor: Kostas Alexis Teaching Assistants: Shehryar Khattak, Mustafa Solmaz, Bishal Sainju Fall 2018 Semester Section 1. Stack ADT Overview wrt Provided Code
More informationChapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a
More informationCSC 222: Computer Programming II. Spring 2004
CSC 222: Computer Programming II Spring 2004 Stacks and recursion stack ADT push, pop, top, empty, size vector-based implementation, library application: parenthesis/delimiter matching run-time
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing
More informationPreface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS
Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...
More informationStacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 5 Copyright 2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced
More informationCS 211 Programming Practicum Spring 2018
Due: Thursday, 4/5/18 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a C++ program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More informationLinear Data Structure
Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal
More informationStacks Calculator Application. Alexandra Stefan
Stacks Calculator Application Alexandra Stefan Last modified 2/6/2018 1 Infix and Postfix Notation The standard notation we use for writing mathematical expressions is called infix notation. The operators
More informationAbstract Data Types. Stack ADT
Abstract Data Types Data abstraction consepts have been introduced in previous courses, for example, in Chapter 7 of [Deitel]. The Abstract Data Type makes data abstraction more formal. In essence, an
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationFirst Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...
First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationUNIT 3
UNIT 3 Presentation Outline Sequence control with expressions Conditional Statements, Loops Exception Handling Subprogram definition and activation Simple and Recursive Subprogram Subprogram Environment
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationData Structures Week #3. Stacks
Data Structures Week #3 Stacks Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications October 5, 2015 Borahan Tümer, Ph.D. 2 Stacks (Yığınlar)
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationArrays and Linked Lists
Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every
More informationIntroduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms
Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types
More informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationOperators. Java operators are classified into three categories:
Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.
More informationList, Stack, and Queues
List, Stack, and Queues R. J. Renka Department of Computer Science & Engineering University of North Texas 02/24/2010 3.1 Abstract Data Type An Abstract Data Type (ADT) is a set of objects with a set of
More informationProgramming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 07 Stack and Its Applications Welcome to module 5 of Programming
More informationDEEPIKA KAMBOJ UNIT 2. What is Stack?
What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you
More informationReview: Expressions, Variables, Loops, and more.
Review: Expressions, Variables, Loops, and more. 1 An Expression Evaluator Example [2] Wk02.1 Slide 1 2 Case Study : Parsing PostFix Expressions What is an expression? A series of symbols that return some
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 B Cynthia Lee Today s Topics HW Tips QT Creator dos & don ts ADTs Stack Example: Reverse-Polish Notation calculator Queue Event queues QT Creator A F E W W A R N I N
More informationLexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler
More informationStacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.
(Section 2) Linked list implementation of stack Typical Application of stacks Evaluation of expressions (infix, postfix, prefix) References and further details By: Pramod Parajuli, Department of Computer
More informationCS 106B Lecture 5: Stacks and Queues
CS 106B Lecture 5: Stacks and Queues Monday, July 3, 2017 Programming Abstractions Summer 2017 Stanford University Computer Science Department Lecturer: Chris Gregg reading: Programming Abstractions in
More informationBinghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program
Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other
More informationStack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.
STACK Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is
More informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management
More informationChapter 1 & 2 Introduction to C Language
1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History
More informationData Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1
Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Stack Operations on a stack Representing stacks Converting an expression
More informationCh. 18: ADTs: Stacks and Queues. Abstract Data Type
Ch. 18: ADTs: Stacks and Queues CS 2308 Fall 2011 Jill Seaman Lecture 18 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationModule 27 Switch-case statements and Run-time storage management
Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss
More informationTypes of Data Structures
DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data
More informationdouble d0, d1, d2, d3; double * dp = new double[4]; double da[4];
All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers
More informationSolution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.
1. What is the full form of LIFO? The full form of LIFO is Last In First Out. 2. Give some examples for stack application. The examples of stack application are reverse a string, post fix evaluation, infix
More informationCSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001
CSE143 Exam with answers Problem numbering may differ from the test as given. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to
More informationLanguage Reference Manual simplicity
Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna
More informationStacks, Queues (cont d)
Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1
More informationFull file at
Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class
More information! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:
Ch. 8: ADTs: Stacks and Queues Abstract Data Type A data type for which: CS 8 Fall Jill Seaman - only the properties of the data and the operations to be performed on the data are specific, - not concerned
More informationCHAD Language Reference Manual
CHAD Language Reference Manual INTRODUCTION The CHAD programming language is a limited purpose programming language designed to allow teachers and students to quickly code algorithms involving arrays,
More informationCS 211 Programming Practicum Spring 2017
Due: Tuesday, 3/28/17 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a JAVA program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 B Cynthia Lee Today s Topics ADTs Stack Example: Reverse-Polish Notation calculator Queue Example: Mouse Events Stacks New ADT: Stack stack.h template
More information