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

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

Unordered Linked Lists

Linked List as an ADT (cont d.)

DATA STRUCTURE - QUEUE

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

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

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

DATA STRUCTURES USING C

Data Structures and Algorithms Stacks and Queues

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

Data Structures and Algorithms V Otávio Braga

Chapter 3: Restricted Structures Page 1

Module 2 Stacks and Queues: Abstract Data Types

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

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

Data Structure [Question Bank]

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.

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

Common Data Structures

Glossary of Object Oriented Terms

10CS35: Data Structures Using C

Queue ADT. March 16, 2000

DATA STRUCTURE - STACK

PES Institute of Technology-BSC QUESTION BANK

Introduction to Stacks

Converting a Number from Decimal to Binary

Data Structures and Algorithms

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

Algorithms and Data Structures

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Data Structures and Data Manipulation

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

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

Programming with Data Structures

Curriculum Map. Discipline: Computer Science Course: C++

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)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

What Is Recursion? Recursion. Binary search example postponed to end of lecture

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

STACK Data Structure:

Cpt S 223. School of EECS, WSU

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

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

STACKS,QUEUES, AND LINKED LISTS

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

7.1 Our Current Model

C++ INTERVIEW QUESTIONS

Data Structures UNIT III. Model Question Answer

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

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

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

Computer Programming using C

Node-Based Structures Linked Lists: Implementation

PROBLEMS (Cap. 4 - Istruzioni macchina)

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

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Linked Lists Linked Lists, Queues, and Stacks

BCS2B02: OOP Concepts and Data Structures Using C++

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

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

Object Oriented Software Design II

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

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

Record Storage and Primary File Organization

University of Pune Revised Structure for the B. Sc. (Computer Science) Course (Second Year to be implemented from Academic Year )

14 Stacks, Queues, And Linked Lists

Stacks. Linear data structures

Analysis of a Search Algorithm

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

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structures. Level 6 C Module Descriptor

The C Programming Language course syllabus associate level

Data Structures and Algorithms(5)

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 13: Query Processing. Basic Steps in Query Processing

Introduction to Data Structures

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

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

INDEX. 1 Write recursive program which computes the nth Fibonacci number,

Heaps & Priority Queues in the C++ STL 2-3 Trees

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Thread Synchronization and the Java Monitor

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!

Introduction to Data Structures and Algorithms

COMPUTER SCIENCE. Paper 1 (THEORY)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

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

Classes and Pointers: Some Peculiarities (cont d.)

Lecture 12 Doubly Linked Lists (with Recursion)

Introduction to Programming System Design. CSCI 455x (4 Units)

Binary Search Trees CMPSC 122

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Abstract Data Types. Chapter 2

CmpSci 187: Programming with Data Structures Spring 2015

Stacks. Data Structures and Data Types. Collections

Sequences in the C++ STL

Universidad Carlos III de Madrid

Transcription:

Application of Stacks: Postfix Expressions Calculator (cont d.) Postfix expression: 6 3 + 2 * = FIGURE 7-15 Evaluating the postfix expression: 6 3 + 2 * = Data Structures Using C++ 2E 1

Application of Stacks: Postfix Expressions Calculator (cont d.) Main algorithm pseudocode Broken into four functions for simplicity Function evaluateexpression Function evaluateopr Function discardexp Function printresult Data Structures Using C++ 2E 2

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Backward Stack Used to design nonrecursive algorithm Print a linked list backward Use linked implementation of stack FIGURE 7-16 Linked list Data Structures Using C++ 2E 3

FIGURE 7-17 List after the statement current = first; executes FIGURE 7-18 List and stack after the statements stack.push(current); and current = current- >link; execute Data Structures Using C++ 2E 4

FIGURE 7-19 List and stack after the while statement executes FIGURE 7-20 List and stack after the statements current = stack.top(); and stack.pop(); execute Data Structures Using C++ 2E 5

STL class stack Standard Template Library (STL) library class defining a stack Header file containing class stack definition stack TABLE 7-3 Operations on a stack object Data Structures Using C++ 2E 6

Summary Stack Last In First Out (LIFO) data structure Implemented as array or linked list Arrays: limited number of elements Linked lists: allow dynamic element addition Stack use in compliers Translate infix expressions into some form of postfix notation Translate postfix expression into machine code Standard Template Library (STL) Provides a class to implement a stack in a program Data Structures Using C++ 2E 7

Data Structures Using C++ 2E Chapter 8 Queues

Objectives Learn about queues Examine various queue operations Learn how to implement a queue as an array Learn how to implement a queue as a linked list Discover queue applications Become aware of the STL class queue Data Structures Using C++ 2E 9

Introduction Queue data structure Elements added at one end (rear), deleted from other end (front) First In First Out (FIFO) Middle elements inaccessible Data Structures Using C++ 2E 10

Queue Operations Two key operations addqueue deletequeue Additional operations initializequeue, isemptyqueue, isfullqueue, front, back queuefront, queuerear pointers Keep track of front and rear See code on pages 453-454 Data Structures Using C++ 2E 11

Implementation of Queues as Arrays Four member variables Array to store queue elements Variables queuefront, queuerear Variable maxqueuesize Using queuefront, queuerear to access queue elements queuefront: first queue element index queuerear: last queue element index queuefront changes after each deletequeue operation queuerear changes after each addqueue operation Data Structures Using C++ 2E 12

Implementation of Queues as Arrays (cont d.) Execute operation addqueue(queue,'a'); Execute addqueue(queue,'b'); addqueue(queue,'c'); Execute deletequeue(); Data Structures Using C++ 2E 13

FIGURE 8-1 Queue after the first addqueue operation FIGURE 8-2 Queue after two more addqueue operations FIGURE 8-3 Queue after the deletequeue operation Data Structures Using C++ 2E 14

Implementation of Queues as Arrays (cont d.) Consider the sequence of operations: AAADADADADADADADA... Eventually index queuerear points to last array position Looks like a full queue Reality: queue has two or three elements, array empty in the front FIGURE 8-4 Queue after the sequence of operations AAADADADADADA... Data Structures Using C++ 2E 15

Implementation of Queues as Arrays (cont d.) First solution Upon queue overflow to the rear Check value of queuefront If room in front: slide all queue elements toward first array position Works if queue size very small Second solution: assume circular array FIGURE 8-5 Circular queue Data Structures Using C++ 2E 16

Implementation of Queues as Arrays (cont d.) queuerear = (queuerear + 1) % maxqueuesize; Advances queuerear (queuefront) to next array position FIGURE 8-6 Queue before and after the add operation Data Structures Using C++ 2E 17

Implementation of Queues as Arrays (cont d.) If queuerear < maxqueuesize 1 queuerear + 1 <= maxqueuesize 1 (queuerear + 1) % maxqueuesize = queuerear + 1 If queuerear == maxqueuesize 1 queuerear + 1 == maxqueuesize (queuerear + 1) % maxqueuesize = 0 queuerear set to zero First array position Data Structures Using C++ 2E 18

Implementation of Queues as Arrays (cont d.) Two cases with identical queuefront, queuerear values Figure 8-7(b) represents an empty queue Figure 8-8(b) represents a full queue FIGURE 8-7 Queue before and after the delete operation FIGURE 8-8 Queue before and after the add operation Data Structures Using C++ 2E 19

Implementation of Queues as Arrays (cont d.) First solution: use variable count Incremented when new element added Decremented when element removed Functions initializequeue, destroyqueue initialize count to zero Data Structures Using C++ 2E 20

Implementation of Queues as Arrays (cont d.) Second solution queuefront indicates index of array position preceding first element of the queue Assume queuerear indicates index of last element Empty queue if queuefront == queuerear Slot indicated by index queuefront is reserved Queue is full If next available space represents special reserved slot Data Structures Using C++ 2E 21

Implementation of Queues as Arrays (cont d.) See code on pages 459-460 Uses first solution FIGURE 8-9 Array to store the queue elements with a reserved slot Data Structures Using C++ 2E 22

Empty Queue and Full Queue Empty queue If count == 0 Full queue If count == maxqueuesize Data Structures Using C++ 2E 23

Initialize Queue Initializes queue to empty state First element added at the first array position Initialize queuefront to zero, queuerear to maxqueuesize - one, count to zero FIGURE 8-10 Empty queue Data Structures Using C++ 2E 24

Front Returns first queue element If the queue nonempty Element indicated by index queuefront returned Otherwise Program terminates Data Structures Using C++ 2E 25

Back Returns last queue element If queue nonempty Returns element indicated by index queuerear Otherwise Program terminates Data Structures Using C++ 2E 26

Add Queue Data Structures Using C++ 2E 27

Delete Queue Data Structures Using C++ 2E 28

Constructors and Destructors Data Structures Using C++ 2E 29

Constructors and Destructors (cont d.) Array storing queue elements Created dynamically When queue object goes out of scope Destructor deallocates memory occupied by the array storing queue elements Data Structures Using C++ 2E 30