The Stack and Queue Types

Similar documents
Data Structures and Algorithms V Otávio Braga

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

DATA STRUCTURES USING C

Algorithms and Data Structures

Chapter 3: Restricted Structures Page 1

Module 2 Stacks and Queues: Abstract Data Types

Introduction to Stacks

7.1 Our Current Model

Common Data Structures

Data Structures and Algorithms Stacks and Queues

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

10CS35: Data Structures Using C

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Stacks. Data Structures and Data Types. Collections

Cpt S 223. School of EECS, WSU

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures UNIT III. Model Question Answer

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Data Structure [Question Bank]

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Linked Lists Linked Lists, Queues, and Stacks

PES Institute of Technology-BSC QUESTION BANK

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures and Algorithms

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Application of Stacks: Postfix Expressions Calculator (cont d.)

St S a t ck a ck nd Qu Q eue 1

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

14 Stacks, Queues, And Linked Lists

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Chapter 5 Instructor's Manual

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Introduction to Data Structures

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Introduction to Data Structures and Algorithms

STACKS,QUEUES, AND LINKED LISTS

DATA STRUCTURE - QUEUE

Universidad Carlos III de Madrid

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Algorithms and Data Structures Written Exam Proposed SOLUTION

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Java Software Structures

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Chapter One Introduction to Programming

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

D06 PROGRAMMING with JAVA

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations (ODE), v0.7.

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Administrative Issues

Comp151. Definitions & Declarations

Fast nondeterministic recognition of context-free languages using two queues

Chapter 11 I/O Management and Disk Scheduling

Stacks. Linear data structures

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

CompSci-61B, Data Structures Final Exam

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Operating Systems. Virtual Memory

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Cours de C++ Utilisations des conteneurs

Glossary of Object Oriented Terms

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

DATA STRUCTURE - STACK

Compiler I: Syntax Analysis Human Thought

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Data Structure with C

Sequential Data Structures

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Lecture Notes on Stacks & Queues

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

COMPUTER SCIENCE. Paper 1 (THEORY)

Cpt S 223. School of EECS, WSU

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Data Structures and Data Manipulation

CS3600 SYSTEMS AND NETWORKS

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

2) Write in detail the issues in the design of code generator.

Transcription:

The Stack and Queue Types Lecture 10 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/spring_2015/csc1254.html

Programming Principle of the Day Do the simplest thing that could possibly work A good question to ask one s self when programming is What is the simplest thing that could possibly work? This helps keep us on the path towards simplicity in the design. http://c2.com/xp/dothesimplestthingthatcouldpossiblywork.html 2

Abstract This lecture will focus on two other sequential data types, the stack and the queue. We will use stacks to implement conversion between normal expressions and the equivalent reverse polish notation. 3

Introduction to Stacks A stack is a last-in-first-out (LIFO) data structure Limited access vector (or list) Main operations: Adding an item Referred to as pushing it onto the stack Removing an item Referred to as popping it from the stack 4

Introduction to Stacks Definition: An ordered collection of data items Can be accessed at only one end (the top) Operations: Construct a stack (usually empty) Check if it is empty push: add an element to the top top: retrieve the top element pop: remove the top element size: returns number of elements in stack 5

Introduction to Stacks Useful for Reversing a sequence Managing a series of undo-actions Tracking history when browsing the web Function call hierarchy is implemented with a stack 6

Push Push means place a new data element at the top of the stack 11 5 3 17 7

Push (cont.) Push means place a new data element at the top of the stack 11 3 5 17 8

Push (cont.) Push means place a new data element at the top of the stack 3 11 5 17 9

Push (cont.) Push means place a new data element at the top of the stack 3 11 5 17 10

Pop Pop means take a data element off the top of the stack 3 11 5 17 11

Pop (cont.) Pop means take a data element off the top of the stack 3 11 5 17 12

Pop (cont.) Pop means take a data element off the top of the stack 11 3 5 17 13

Pop (cont.) Pop means take a data element off the top of the stack 11 5 3 17 14

Top Top means retrieve the top of the stack without removing it 3 11 5 17 15

Top (cont.) Top means retrieve the top of the stack without removing it 3 11 5 3 17 16

Top (cont.) Top means retrieve the top of the stack without removing it 3 11 5 3 17 17

Linked-List Stack Stacks can also be implemented with a linked list The front node is the top of the stack In fact, there is std::stack<> which is an adaptor usable with different types of underlying containers (std::list is one possibility) std::stack<t>: implements a stack of T s T top(); void push(t const&); void pop(); std::stack<t>::size_type size() const; bool empty(); 18

Linked-List Stack (cont.) top To pop, we remove the node at the front of the linked list, and return the element to the client 19

Linked-List Stack (cont.) top To pop, we remove the node at the front of the linked list, and return the element to the client 20

Linked-List Stack (cont.) top To push, we place the new element in a node and insert it at the front of the linked list 21

Linked-List Stack (cont.) top To push, we place a new element in a node and insert it at the front of the linked list 22

Example: Implementing a Stack Implementing a stack on top of a list is trivial Live coding 23

Application of Stacks Consider the arithmetic statement in the assignment: x = a * b + c Compiler must generate machine instructions: LOAD a MULT b ADD c STORE x Note: this is "infix" notation The operators are between the operands 24

Reverse Polish Notation Postfix Notation Most compilers convert an expression in infix notation to postfix The operators are written after the operands So a * b + c becomes a b * c + Advantage: Expressions can be written without parentheses 25

Postfix and Prefix Examples Infix RPN (Postfix) Prefix A + B A B + + A B A * B + C A B * C + + * A B C A * (B + C) A B C + * * A + B C A (B (C D)) A B C D A B C D A B C D A B C D A B C D Prefix: Operators come before the operands 26

Evaluating RPN Expressions "By hand" (Underlining technique): 1. Scan the expression from left to right to find an operator. 2. Locate ("underline") the last two preceding operands and combine them using this operator. 3. Repeat until the end of the expression is reached. Example: 2 3 4 + 5 6 - - * 27

Evaluating RPN Expressions 2 3 4 + 5 6 - - * 2 3 4 + 5 6 - - * 2 7 5 6 - - * 2 7 5 6 - - * 2 7-1 - * 2 7-1 - * 2 8 * 2 8 * 16 2 * ((3 + 4) (5 6)) 28

Evaluating RPN Expressions By using a stack algorithm Initialize an empty stack Repeat the following until the end of the expression is encountered Get the next token (const, var, operator) in the expression Operand push onto stack Operator do the following Pop 2 values from stack Apply operator to the two values Push resulting value back onto stack Note: if only 1 value on stack, this is an invalid RPN expression When end of expression encountered, value of expression is the (only) number left in stack 29

Evaluating of Postfix Note the changing status of the stack 30

Converting between Notations By hand: Represent infix expression as an expression tree: A * B + C + A * (B + C) * ((A + B) * C) / (D - E) / * C A + * - A B B C + C D E A B 31

Converting RPN (Postfix) Traverse the tree in Left-Right-Parent order (post-order) to get RPN: / * - A B + C * D E - / + C D E A B 32

Converting to Prefix Traverse tree in Parent-Left-Right order (pre-order) to get prefix: / * - / * + A B C - D E + C D E A B 33

Converting to Infix Traverse tree in Left-Parent-Right order (in-order) to get infix (must insert parenthesis) / + * C D - E ((A + B)* C)/(D E)) A B 34

Another RPN Conversion Method By hand: "Fully parenthesize-move-erase" method: 1. Fully parenthesize the expression. 2. Replace each right parenthesis by the corresponding operator. 3. Erase all left parentheses. A * B + C ((A * B) + C) A * (B + C) (A * (B + C ) ) ((A B * C + (A (B C + * A B * C + A B C + * 35

Introduction to Queue A queue is a first-in-first-out (FIFO) data structure Limited access vector (or list) Main operations: Adding an item Referred to as pushing it onto the queue (enqueue an item, adding to the end) Removing an item Referred to as popping it from the queue (dequeue an item, remove from the front) 36

Introduction to Queues Definition: An ordered collection of data items Can be read at only one end (the front) and written to only at the other end (the back) Operations: Construct a queue (usually empty) Check if it is empty push: add an element to the back front: retrieve the front element pop: remove the front element size: returns number of elements in queue 37

Introduction to Queues Useful for All kind of simulations (traffic, supermarket, etc.) Computers use queues for scheduling Handling keyboard events Handling mouse events Scrolling the screen Printer spooler C++ I/O streams are queues Even if we only access one end, the other end is managed by the operating system 38

Example: Queue of Strings void manage_queue() { queue<string> waiting_strings; while (true) { cout << "?> "; string response; getline(cin, response); // queue of 'waiting' strings // ask for next line of text if (response.empty()) break; } } if (response == "next") { // try to dequeue if (waiting_strings.empty()) cout << "No one waiting!" << endl; else { cout << waiting_strings.front() << endl; waiting_strings.pop(); } } else { // enqueue the line read waiting_strings.push(response); } 39

Example: Implementing a Queue Implementing a queue on top of a list is equally trivial Live coding 40