Data Structures Using C++ 2E. Chapter 7 Stacks

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

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

Linked List as an ADT (cont d.)

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

Unordered Linked Lists

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

Data Structures and Algorithms V Otávio Braga

DATA STRUCTURES USING C

DATA STRUCTURE - STACK

Module 2 Stacks and Queues: Abstract Data Types

7.1 Our Current Model

Chapter 3: Restricted Structures Page 1

Stacks. Linear data structures

Algorithms and Data Structures

10CS35: Data Structures Using C

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

Introduction to Stacks

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

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

Glossary of Object Oriented Terms

Classes and Pointers: Some Peculiarities (cont d.)

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

PES Institute of Technology-BSC QUESTION BANK

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

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

Converting a Number from Decimal to Binary

Data Structure [Question Bank]

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

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

The C Programming Language course syllabus associate level

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

Common Data Structures

Chapter 5 Instructor's Manual

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

C++ INTERVIEW QUESTIONS

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

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.

Data Structure with C

Object Oriented Software Design II

Data Structures and Algorithms Stacks and Queues

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

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

STACKS,QUEUES, AND LINKED LISTS

Data Structures and Data Manipulation

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

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

Data Structures UNIT III. Model Question Answer

PROBLEMS (Cap. 4 - Istruzioni macchina)

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

BCS2B02: OOP Concepts and Data Structures Using C++

CHAPTER 7: The CPU and Memory

Java Interview Questions and Answers

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

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 and Algorithms(5)

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

14 Stacks, Queues, And Linked Lists

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

Linked Lists Linked Lists, Queues, and Stacks

Stack Allocation. Run-Time Data Structures. Static Structures

DATA STRUCTURE - QUEUE

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

Cpt S 223. School of EECS, WSU

El Dorado Union High School District Educational Services

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

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

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

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

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

University of Twente. A simulation of the Java Virtual Machine using graph grammars

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

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

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

Introduction to Data Structures

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

Stacks. Data Structures and Data Types. Collections

Data Structures and Algorithms C++ Implementation

Sequential Data Structures

Recursive Implementation of Recursive Data Structures

Chapter 7D The Java Virtual Machine

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

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

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

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Java Software Structures

Data Structures and Algorithms

Compilers I - Chapter 4: Generating Better Code

An Overview of Stack Architecture and the PSC 1000 Microprocessor

Analysis of Micromouse Maze Solving Algorithms

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

C++FA 3.1 OPTIMIZING C++

Analysis of a Search Algorithm

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

1 The Java Virtual Machine

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

CmpSci 187: Programming with Data Structures Spring 2015

1 Classical Universal Computer 3

Transcription:

Data Structures Using C++ 2E Chapter 7 Stacks

Stacks Data structure Elements added, removed from one end only Last In First Out (LIFO) FIGURE 7-1 Various examples of stacks Data Structures Using C++ 2E 2

Stacks (cont d.) push operation Add element onto the stack top operation Retrieve top element of the stack pop operation Remove top element from the stack Data Structures Using C++ 2E 3

Stacks (cont d.) FIGURE 7-2 Empty stack FIGURE 7-3 Stack operations Data Structures Using C++ 2E 4

Stacks (cont d.) Stack element removal Occurs only if something is in the stack Stack element added only if room available isfullstack operation Checks for full stack isemptystack operation Checks for empty stack initializestack operation Initializes stack to an empty state Data Structures Using C++ 2E 5

Stacks (cont d.) Review code on page 398 Illustrates class specifying basic stack operations FIGURE 7-4 UML class diagram of the class stackadt Data Structures Using C++ 2E 6

Implementation of Stacks as Arrays First stack element Put in first array slot Second stack element Put in second array slot, and so on Top of stack Index of last element added to stack Stack element accessed only through the top Problem: array is a random access data structure Solution: use another variable (stacktop) Keeps track of the top position of the array Data Structures Using C++ 2E 7

Implementation of Stacks as Arrays (cont d.) Review code on page 400 Illustrates basic operations on a stack as an array FIGURE 7-5 UML class diagram of the class stacktype Data Structures Using C++ 2E 8

Implementation of Stacks as Arrays (cont d.) FIGURE 7-6 Example of a stack Data Structures Using C++ 2E 9

Initialize Stack Value of stacktop if stack empty Set stacktop to zero to initialize the stack Definition of function initializestack FIGURE 7-7 Empty stack Data Structures Using C++ 2E 10

Empty Stack Value of stacktop indicates if stack empty If stacktop = zero: stack empty Otherwise: stack not empty Definition of function isemptystack Data Structures Using C++ 2E 11

Full Stack Stack full If stacktop is equal to maxstacksize Definition of function isfullstack Data Structures Using C++ 2E 12

Push Two-step process Store newitem in array component indicated by stacktop Increment stacktop FIGURE 7-8 Stack before and after the push operation Data Structures Using C++ 2E 13

Push (cont d.) Definition of push operation Data Structures Using C++ 2E 14

Return the Top Element Definition of top operation Data Structures Using C++ 2E 15

Pop Remove (pop) element from stack Decrement stacktop by one FIGURE 7-9 Stack before and after the pop operation Data Structures Using C++ 2E 16

Pop (cont d.) Definition of pop operation Underflow Removing an item from an empty stack Check within pop operation (see below) Check before calling function pop Data Structures Using C++ 2E 17

Copy Stack Definition of function copystack Data Structures Using C++ 2E 18

Constructor and Destructor Data Structures Using C++ 2E 19

Copy Constructor Definition of the copy constructor Data Structures Using C++ 2E 20

Overloading the Assignment Operator (=) Classes with pointer member variables Assignment operator must be explicitly overloaded Function definition to overload assignment operator for class stacktype Data Structures Using C++ 2E 21

Stack Header File mystack.h Header file name containing class stacktype definition Data Structures Using C++ 2E 22

Stack Header File (cont d.) Stack operations analysis Similar to class arraylisttype operations TABLE 7-1 Time complexity of the operations of the class stacktype on a stack with n elements Data Structures Using C++ 2E 23

Linked Implementation of Stacks Disadvantage of array (linear) stack representation Fixed number of elements can be pushed onto stack Solution Use pointer variables to dynamically allocate, deallocate memory Use linked list to dynamically organize data Value of stacktop: linear representation Indicates number of elements in the stack Gives index of the array Value of stacktop 1 Points to top item in the stack Data Structures Using C++ 2E 24

Linked Implementation of Stacks (cont d.) Value of stacktop: linked representation Locates top element in the stack Gives address (memory location) of the top element of the stack Review program on page 415 Class specifying basic operation on a stack as a linked list Data Structures Using C++ 2E 25

Linked Implementation of Stacks (cont d.) Example 7-2 Stack: object of type linkedstacktype FIGURE 7-10 Empty and nonempty linked stacks Data Structures Using C++ 2E 26

Default Constructor When stack object declared Initializes stack to an empty state Sets stacktop to NULL Definition of the default constructor Data Structures Using C++ 2E 27

Empty Stack and Full Stack Stack empty if stacktop is NULL Stack never full Element memory allocated/deallocated dynamically Function isfullstack always returns false value Data Structures Using C++ 2E 28

Initialize Stack Reinitializes stack to an empty state Because stack might contain elements and you are using a linked implementation of a stack Must deallocate memory occupied by the stack elements, set stacktop to NULL Definition of the initializestack function Data Structures Using C++ 2E 29

Initialize Stack (cont d.) Data Structures Using C++ 2E 30

Push newelement added at the beginning of the linked list pointed to by stacktop Value of pointer stacktop updated FIGURE 7-11 Stack before the push operation Data Structures Using C++ 2E 31

Push (cont d.) FIGURE 7-12 Push operation Data Structures Using C++ 2E 32

Push (cont d.) Definition of the push function Data Structures Using C++ 2E 33

Return the Top Element Returns information of the node to which stacktop pointing Definition of the top function Data Structures Using C++ 2E 34

Pop Removes top element of the stack Node pointed to by stacktop removed Value of pointer stacktop updated Data Structures Using C++ 2E 35

Pop (cont d.) FIGURE 7-14 Pop operation Data Structures Using C++ 2E 36

Pop (cont d.) Definition of the pop function Data Structures Using C++ 2E 37

Copy Stack Makes an identical copy of a stack Definition similar to the definition of copylist for linked lists Definition of the copystack function Data Structures Using C++ 2E 38

Data Structures Using C++ 2E 39

Constructors and Destructors Definition of the functions to implement the copy constructor and the destructor Data Structures Using C++ 2E 40

Overloading the Assignment Operator (=) Definition of the functions to overload the assignment operator Data Structures Using C++ 2E 41

Overloading the Assignment Operator (=) (cont d.) TABLE 7-2 Time complexity of the operations of the class linkedstacktype on a stack with n elements Data Structures Using C++ 2E 42

Stack as Derived from the class unorderedlinkedlist Stack push function, list insertfirst function Similar algorithms initializestack and initializelist, isemptylist and isemptystack, etc. Shows that class linkedstacktype is derived from class linkedlisttype Functions pop and isfullstack implemented for linked stack class linkedlisttype: abstract class class unorderedlinkedlisttype: derived from class linkedlisttype Data Structures Using C++ 2E 43

Application of Stacks: Postfix Arithmetic notations Expressions Calculator Infix notation: operator between operands Prefix (Polish) notation: operator precedes operands Reverse Polish notation: operator follows operands Stack use in compliers Translate infix expressions into some form of postfix notation Translate postfix expression into machine code Data Structures Using C++ 2E 44

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 45

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 46

Removing Recursion: Nonrecursive Algorithm to Print a Linked List Stack Backward 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 47

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 48

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 49

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 50

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 51