Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Similar documents
STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Lecture No.04. Data Structures

Infix to Postfix Conversion

Stack. 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

Lecture Data Structure Stack

Data Structures Week #3. Stacks

Formal Languages and Automata Theory, SS Project (due Week 14)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. Revised based on textbook author s notes.

Stack and Its Implementation

BBM 201 DATA STRUCTURES

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

BBM 201 DATA STRUCTURES

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

STACKS AND QUEUES. Problem Solving with Computers-II

The Stack and Queue Types

Stack Abstract Data Type

Content: Learning Objectives

Lab 7 1 Due Thu., 6 Apr. 2017

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Stacks and their Applications

Linear Data Structure

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Types of Data Structures

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

DEEPIKA KAMBOJ UNIT 2. What is Stack?

March 13/2003 Jayakanth Srinivasan,

Top of the Stack. Stack ADT

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Fall, 2015 Prof. Jungkeun Park

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

Top of the Stack. Stack ADT

Stacks, Queues (cont d)

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

CS 211 Programming Practicum Spring 2018

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Operators & Expressions

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

12 Abstract Data Types

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

CS 206 Introduction to Computer Science II

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

CSE 214 Computer Science II Stack

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

The program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.

BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering

Data Structure - Stack and Queue-

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

CS 211 Programming Practicum Spring 2017

Lecture 12 ADTs and Stacks

Application of Stack (Backtracking)

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Project 3: RPN Calculator

IV. 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

CS 211 Programming Practicum Fall 2018

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

An Introduction to Trees

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Unit 3. Operators. School of Science and Technology INTRODUCTION

The time and space are the two measure for efficiency of an algorithm.

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Chapter 4: Basic C Operators

Introduction to Computer and Program Design 2. Lesson 6. Stacks. James C.C. Cheng Department of Computer Science National Chiao Tung University

Stacks Calculator Application. Alexandra Stefan

DATA STRUCTURES AND ALGORITHMS

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

Monday, November 13, 2017

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Associate Professor Dr. Raed Ibraheem Hamed

CS 206 Introduction to Computer Science II

Lecture 4 Stack and Queue

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CHAPTER 8: Central Processing Unit (CPU)

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Chapter 2 Working with Data Types and Operators

1. Stack Implementation Using 1D Array

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

INSTITUTE OF AERONAUTICAL ENGINEERING

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Transcription:

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 from index to postfix Infix, Postfix, and prefix Expressions 1

Stack A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. The definition of the stack provides for the insertion and deletion of items, of items so that a stack is a dynamic constantly changing object. The definition specifies that a single and of the stack is designated as the stack top. New items may be put on top of the stack, or items which are at the top of the stack may be removed. In the figure below, F is physically higher on the page then all the other items in the stack, so F is the current top element of the stack, If any new items are added to the stack they are placed on top of F, and if any items are deleted, F is the first to be deleted. Therefore, a stack, is collected a last in first out. (LIFO) list. Operations on a stack The two main operations which can be applied to a stack are given spatial names, when an item is added to a stack, it is pushed onto the stack, and when an item is removed, it is poped from the stack. Given a stack s, and an item I, performing the operation push (s,i) adds the item I to the top of stack s. similarity, the operation pop(s) removes the top element and returns ir as a function value. Thus the assignment operation I = pop(s); removes the element at the top below is a motion picture of a stack s as it expands (items pushed) and shrinks (item poped) with the passage of item. 2

The original idea of the stack is that there is no upper limit on the number of items that may be kept in a stack. Pushing another item onto a stack produces a larger collection of items. When the stack been implemented in a program, it can be represented as an array, therefore we need to give the maximum size of this stack to be shrieked. So, we need as operation called Full (S) (which determines whether or not a stack is full (overflow) to be applied before push operation. On the other hand, if a stack contains a single item and the stack is poped, the resulting stack contains no item and is called an empty stack. Therefore, before applying the pop operator to a stack, we must ensure that a stack is not empty. The operation empty (s) determine whether or not a stack is empty. Another operation that can be performed on a stack is to determine what the top item on a stack is without removing it. This operation is written stack top (s) and return the top element of stack s. I = stacktop (s) Representing stacks: Before programming a problem solution uses a stack, we must decide how to represent a stack using the data structures that exit in our programming language (C or C++). the simplest method to represent a stack is to use an array to be home of the stack. During the program execution, the stack can grow and shrink within the space reserved for it. One end of the array is the fixed bottom of the stack, while the top of the stack, Thus another field is needed that, at each point during program execution, keeps tracks of the current position of the top of the stack. The stack may therefore be declared as a structure containing two objects: an array to hold the elements of the stack, and an integer to indicate the position of the current stack top within the array. # define stacksize 100 Struct stack int top ; ; int item [stacksize] ; 3

The empty stack contain s no elements and can therefore, be indicated by top equaling -1. To initialize a stack s to the empty stack, we may execute. S top = -1; The function empty(s) that is used to test whether a stack is empty, may be written as follow: int empty (struct stack *st) If (st top = = -1) Return (TRUE); Else Return (FALSE) The function full (s) that is used to test whether a stack is full, may be written as fellow: int full (struct stack *ST) If (st top = = maximize -1) Return (TRUE); Else return (FALSE); The function pop that is used to remove the element in the top of the stack, must perform the following three actions: 1- If the stack is empty, print a warning message and halt execution. 2- Remove the top element from the stack. 3- Return this element to the calling program. int pop (srtuct stack *ST) If (empty (ST)) Cout << "stack underflow" ; Exit (q) ; Return (ST item [st top P-] ; The function push is used to add a new element to the top of the stack. This function must perform the following operations: 1- If the stack if full, print a warning message and halt execution. 2- Add a new element into the top of the stack. Void push (struct stack *ST, int x) If (full (S+)) Cout << "stack is overflow"; Exit (1); 4

Else ST item [+ + (ST top)] = x; Return ; The function stacktop (), which returns the top element of a stack without removing it from the stack, may be written as follows: int stacktop (struct stack *ST) If (empty (ST)) Cout << "stack is underflow"; Exit (1); else return (ST item ST top); Convert the following infix expression to postfix using stack: A + (B /C) # Example Convert the following infix expression to postfix using stack: (( A ( B + C)) * D) / (E +F) # 5

Infix, Postfix, and prefix Expressions: Consider the sum of A and B. we think of applying the operator t" to the operands A and B and write the Sum as A+B. This particular representation is called infix. There are two alternate notations for expressing the Sam of A and Busing the symbols A, B and t. These are T A B prefix A B + postfix In prefix notation the operator precedes the two operands, in postfix notation the operator follows the two operands, and in infix notation the operand. Is between the two operands. The evaluation of the expression A+B*c, as written in standard infix notation, require Knowledge of which of the two operations + or * is to be performed first. In the case of + 6

and * we Know that mutilation is to be done before addition. Thus A+B*c is interpreter As a (B*C). Foe the Same example, if we want need to write the expression as (A+B)*C.Therefore, Infix notation may require parentheses to specify a decimal or of operations. Using postfix and prefix notation, the notation, the need for parentheses is eliminated because the operator is placed directly after (before ) the two operands to which it applied. To convent infix expression to postfix forms,we use the following algorithm : 1- Completely parenthesize the infix expression. 2- Move each operator to the space hold by its corresponding right parenthesis. 3- Remove all parentheses. We can apply this algorithm to the expression : A / B$C + D*E A*C ((( A / (B $ C )) + ( D*E )) (A * C )) ABC$ / DE*+AC*_ The conversion algorithm for infix to prefix specify that, after compactly parenthesizing the infix expression with order of priority, we move each operator to its corresponding left parent. And after that, delimiting all parentheses For example :- ((( A / (B $ C )) + ( D * E)) (A*C)) ((( A / (B $ C )) + ( D * E)) (A*C)) - + / A$BC *DE*AC In the above example, we have consider five binary operations : addition, subtraction,, multiplication division, and exponentiation. The first four are available in C and C+ + and are dented by the usual operators +,-, * and / > The fifth, expunction is demotion by $. The value of the expression A $ B is a raised to the B power, So that 3$ 2 is of For these binary operators the following is the order of precedence (highest to lowest ) : Exponentiation Multiplication / division Addition / Subtraction When un parenthesize operators of the Same precedence are scanned, the order is assume to be left to right except in the case of the exponentiation, where the order is assumed to be from right to left. Thus A+B+C means (A+B) +C, whereas A $ B $C means A $ (B$C). Evaluating a postfix Expression Stacks con be used to evaluate the different expression notation in sedition to converting from one expression notation to another as we will see later. Here, we consider evaluating postfix expression using stack. As an example, consider the postfix expansion 623 + - 382 / + * 2 $ 3 + To evaluate such expression, we reputedly read characters from the postfix expression. If the character read is can operand, push the value associational with it onto the stack. If it is an operator, pop two values from the stack, copy the operator to them, and push the result back onto the stack. 7

8